Jul 10, 2008

Access SVN version and build date from maven

Sometimes we need to show or save information about application build. This information may include sources version (from VCS that you use), build date, database versions, etc. If you build your application with Maven there is easy way to access this information.

To do this check buildnumber-maven-plugin. This plugin is used to build extended build version. To access SVN version and build timestamp (not formatted date) you need to add following plugin configuration to maven pom.xml:

<plugins>
....
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<configuration>
<buildNumberPropertyName>sources.version</buildNumberPropertyName>
<timestampPropertyName>build.timestamp</timestampPropertyName>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

These variables may be used to build project version, to label project artifacts or just to provide this information to end user like in following example:

- Add file version.properties to you maven module resources:

application.sources.version=${sources.version}
application.build.date=${build.timestamp}

- Add filtering section for this file to the maven pom.xml:

<resources>
....

<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>version.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>

Now you have more control on application version. Develop with pleasure!

2 comments:

Anonymous said...

How does the version properties file get created? Wouldn't it be better to have Maven get the return value of svnversion and then put this number in as version or some component of the version number? What if it's CVS rather than subversion? Unfortunately, we use both in our shop.

Mikalai Alimenkou said...

Sorry for my late comments. Version properties file is just a sample how to use this VCS version. You may use it also to build Maven artifacts names or filter other sources like MANIFEST file. Described way is better from my point of view than using svnversion just because we delegate all work to existing plugin and use Maven architecture in natural way. Check if this plugin supports CVS (I hope yes).