Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

maven-2

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.

like image 492
Kent Avatar asked Oct 06 '09 10:10

Kent


People also ask

What is Project version?

Project versions - these are basically snapshots of a project at different stages of a project. Based on how the system is set up, a version could be created everytime the system status of a project object is changed, for example.

How do you find the value of pom files?

The plugin is part of the Maven Super Pom and executed during the process-resources phase of the Jar Default Lifecyle. The only thing you have to do is to active filtering. How you make this property then available to your Java application is up to you - reading it from the classpath would work.


2 Answers

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> 
like image 145
Rich Seller Avatar answered Oct 04 '22 01:10

Rich Seller


It's maybe stupid but I'd use a .properties file like in this example instead of filtering directly the JSP.

like image 40
Pascal Thivent Avatar answered Oct 04 '22 02:10

Pascal Thivent