• About
Triona Weblog

Software Development and much more

Generating build numbers in web applications using maven

19.02.2012 by Anton Autor

Problem:
You want to create a build number in the web front end of your application (e.g. to have it on screenshots appended to bug reports during user acceptance test).
You are using maven for your builds and your code is under source control (e.g. using subversion).

Solution:
All you need is some maven plugins and a place to write the build number to.
In this case I’d like to have the maven version number followed by the subversion revision number and a timestamp of the build date like this:

Version: 1.0-SNAPSHOT r1 Built on: 2012-02-19 13:41

First I create a version.html in src/main/resources or src/main/templates or something like that:

Version: @VERSION@
Built on: @BUILTON@

As you can see there are two placeholders (@VERSION@ and @BUILTON@)
Next I insert and configure the plugins to create the version info and the timestamp and to replace the placeholders with them in my pom.xml:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>maven-buildnumber-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>false</doUpdate>
</configuration>
</plugin>

This creates a property named buildNumber with the revision number of the checked out code. The configuration doCheck=true causes a check whether there are any uncommitted changes. If there were any, the build would fail. With doUpdate you could instruct the build to execute a svn update prior to execution of the build.
For more info about this plugin see here:
http://mojo.codehaus.org/buildnumber-maven-plugin/
We could also use this plugins goal “create-timestamp” to create the timestamp, but since maven itself provides a property for that we just need to set that properties format in the properties section of our pom:

<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
</properties>

Now having all we need, we need to replace the placeholders in our html and copy the replacement to our target folder.

<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>src/main/templates/version.html</file>
<outputFile>target/version.html</outputFile>
<regex>false</regex>
<replacements>
<replacement>
<token>@VERSION@</token>
<value>${version} r${buildNumber}</value>
</replacement>
<replacement>
<token>@BUILTON@</token>
<value>${maven.build.timestamp}</value>
</replacement>
</replacements>
</configuration>
</plugin>

This replaces the placeholders in src/main/templates/version.html with the values and writes the result into the specified target folder.
More info about this plugin is provided here:
http://code.google.com/p/maven-replacer-plugin/
Having that, you just need to include that into your pages. That’s it.

Conclusion:
We now have a build number on our pages.
There for sure is room to improve this solution and it is for sure not the only solution. But it did the job for me and I hope it will help you.
If I should have forgotten something in this description, do not hesitate to give me feedback via the comments form.

Posted in: Development, Java, JEE Tagged: Build, build number, HTML, Java, JSF, JSP, Maven, Maven Plugins, Subversion, web application
September 2023
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  
« Nov    

Tags

API Architecture CDI Collections Comparable Comparator Database EA Eclipse EJB Enterprise Architect Excel Hessian HTML Iteration Java Java 8 JavaEE Java EE Java Enterprise Development javascript Javascript Canvas HTML5 JEE JEE 6 JPA jQuery JSF linux Makro Map MariaDB Maven Oracle Plugins Relation relationship Richfaces Service Service Facade Set SOA Subversion Tutorial VBA XML

Recent Posts

  • Domain Driven Design und Event Storming
  • NESTJS BEYOND „HELLO WORLD“
  • Jakarta EE 9 – An upheaval with difficulties
  • Jakarta EE 9 – Ein Umbruch mit Schwierigkeiten
  • Erste Schritte mit Hibernate Spatial

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2023 Triona Weblog.

Impressum | Datenschutzerklärung