Maven: automatically create a version Class

Using a Maven to build your Java project, you can easily create a static Class containing Version and Release of your project; for example, you can then access to this class to show the version, for example, on your main project page. The important thing is that you don't need to maintain this class nor to commit it: any build will automatically regenerate, and then build, the class file.

Here the code to put in your maven pom.xml in the <build> tag:

                <property name="src.dir" value="${}" />
                <property name="package.dir" value="net/mornati/configuration" />
                <property name="" value="net.mornati.configuration" />
                <property name="buildtime" value="${}" />

                <echo file="${src.dir}/${package.dir}/" message="package ${};${line.separator}" />
                <echo file="${src.dir}/${package.dir}/" append="true" message="public final class Version {${line.separator}" />
                <echo file="${src.dir}/${package.dir}/" append="true"
                      message=" public static String VERSION="${project.version}-${buildtime}";${line.separator}" />
                <echo file="${src.dir}/${package.dir}/" append="true" message="}${line.separator}" />
                <echo message="BUILD ${buildtime}" />
If necessary, with a maven property you can control the timestamp format used to inject the "release" in your version file:
The result is a file containing a public method named VERSION like this:
public static final String VERSION = "2.0.1-20130627220534567"
that are the project version specified in your project pom and the build timestamp with the provided format. And then you can simply access to this property with a jsp/java/... file:
  <td class="exp-footer"> Project Version: <b><%= net.mornati.configuration.Version.VERSION %></b>
A thing to know is that if you want to use the Version class inside another java class, your development IDE shows you an error before the first build (the file is not present), but normally build should even work without problem and, once your file is created, the error will not be shown anymore. A good idea could be to add this file to ignore of your source repository. For git for example, put in your .gitignore file: