/ ant run

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:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
    <execution>
        <goals>
            <goal>run</goal>
        </goals>
        <phase>generate-sources</phase>
        <configuration>
            <tasks>
                <property name="src.dir" value="${project.build.sourceDirectory}" />
                <property name="package.dir" value="net/mornati/configuration" />
                <property name="package.name" value="net.mornati.configuration" />
                <property name="buildtime" value="${maven.build.timestamp}" />

                <echo file="${src.dir}/${package.dir}/Version.java" message="package ${package.name};${line.separator}" />
                <echo file="${src.dir}/${package.dir}/Version.java" append="true" message="public final class Version {${line.separator}" />
                <echo file="${src.dir}/${package.dir}/Version.java" append="true"
                      message=" public static String VERSION="${project.version}-${buildtime}";${line.separator}" />
                <echo file="${src.dir}/${package.dir}/Version.java" append="true" message="}${line.separator}" />
                <echo message="BUILD ${buildtime}" />
            </tasks>
        </configuration>
    </execution>
</executions>
</plugin>

If necessary, with a maven property you can control the timestamp format used to inject the "release" in your version file:

<properties>
 <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>

The result is a Version.java 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:

 <tr>
  <td class="exp-footer">
    Mornati.net Project Version: <b><%= net.mornati.configuration.Version.VERSION %></b>
  </td>
</tr>

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:

.svn
.idea
target
*.iml
*.iws
net/mornati/configuration/Version.java