Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

java - How can I in a jsp page get maven project version number?

I am working on a java web application, managed by maven2. From time to time, we did some changes, and want to do new releases, of course with new version number. In the homepage (jsp), there is text like

<b>version:</b> 2.3.3... 

Is it possible, every time I do a new release, I only change the <version/> in pom.xml, and version number in jsp can be automatically filled by this maven ${project.version}?

I tried maven profile, however it doesn't seem to work.

any ideas?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use project filtering to process the JSP as it is copied to the target location. If the JSP is specified with ${project.version}, and the containing folder is specified as a filter location the value should be substituted into the JSP as it is packaged.

For example, adding this to your POM enables filtering for src/main/resources:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

Update: for war packaging, you may need to configure the war plugin to do its filtering. See the Filtering section of the war-plugin's documentation for more details and examples.

Essentially the process is the same, but it is defined below the war plugin, so you'd have something like this:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.0</version>
    <configuration>
      <webResources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
        </resource>
      </webResources>
    </configuration>
  </plugin>
</plugins>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...