tags:

views:

403

answers:

3

Hello guys,

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.

+3  A: 

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>
Rich Seller
Glad to help. This works for any properties set at filter time
Rich Seller
sorry, just now I was checking the wrong file, and thought it was working.I added:<resource> <directory>src/main/webapp</directory> <filtering>true</filtering> </resource>and under src/main/resources, there is a test.jsp, which with ${project.version}.The version number is not assigned to ${...} after I run mvn clean package. Did I do something wrong?
Kent
In your comment you are filtering src/main/webapp but the JSP is in src/main/resources, are you sure the configuration matches up?
Rich Seller
Thanks for the comment.our project structure is:src/main/webapp/css, images... |src/main/webapp/WEB-INF/jsp/here are jsps |src/main/resource/property files, xml conf files etc. |src/main/java/java.packages.and.sourcecodes |now I found the problem is, if I look jsp pages as resources too, there are two resources directories, and two different target outputs for each. :src/main/resources/*.* -> target/../WEB-INF/classes/*.* |.../jsp/*.jsp ->target/../WEB-INF/jsp/*.jsp.maybe resource plugin 'copy-resources' can help?
Kent
I updated my answer with details for setting up filtering in the war plugin
Rich Seller
+1  A: 

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

Pascal Thivent
Well, finally I did in this way. I added an entry in an existing property file and let mvn fill the version. and added an autostarting servlet. When the application context starts, the servlet read prop file and set a system property, say, "myVersion", so that jsp can retrieve by name. using maven resources plugin copy-resources goal works as well. However I also feel filtering jsp as resources not so clean. we used apache-tiles for layout, that is, in some jsps, there are already ${ ...} for tiles. It can be confusing if some ${} for maven added.Thank you guys for those comments.Kent
Kent
A: 

I use this plugin,

http://code.google.com/p/maven-substitute-plugin/

You can do something like this in Java,

   public final static String projectVersion = "@PROJECT_VERSION@";

and it's trivial to pass this value to JSP.

ZZ Coder