Track every Build Number with Maven
Problem
You need to differentiate between build numbers. For example, you’ve just redeployed your application and need to ensure that the new version is what you are viewing. Or, you need to keep track of how many times you build. There could be many reasons why you want to know the build number that you are on. I use it for reporting bugs against specific builds.
Solution
maven-buildnumber-plugin. This Maven2 plugin will generate a unique build number each time your build your project. You can even configure which maven phase triggers the increment of the number. This plugin can also fetch data from SVN to ensure that a team of developers all get unique build numbers.
As a bonus, it generates a buildNumber.properties file so that you can read in this build number from anywhere in your project. Here is how I use the plugin.
First, update your pom.xml. You need to setup the build trigger.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>false</doUpdate>
<format>${version}.{0,number}</format>
<items>
<item>buildNumber0</item>
</items>
</configuration>
</plugin>
</plugins>
</build>

Now, you’ll need the build number and append it to your artifact’s final name. Add this to your pom.xml
<build>
<finalName>
${project.artifactId}-${project.version}.{buildNumber}
</finalName>
</build>
Now you’re package goal will output a file named
projectname-1.0.1.war
This is a great start and now you can differentiate each and every build. However, I need to take this a step further.
I need to see the version on my application’s index page. To do this, I use an ant filter to write the version and timestamp to a version.html file, and then copy it to my project’ web app directory. Add this to your pom.xm.
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<tasks>
<!-- versioning -->
<echo message="[build version]"/>
<delete file="target/projectname/version.html"/>
<tstamp>
<format property="rightNow" pattern="d MMM yyyy" locale="en"/>
</tstamp>
<copy todir="target/projectname">
<fileset dir="src/main/webapp">
<include name="version.html"/>
</fileset>
<filterset>
<filter token="VERSION" value="${buildNumber}"/>
<filter token="BUILTON" value="${rightNow}"/>
</filterset>
</copy>
<echo message=" version is ${buildNumber}"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Version.html is simple.
<p class="version">Version: @VERSION@</p> <p class="built">Built on: @BUILTON@</p>
Finally, in my index page…
<jsp:include page="/version.html"/>
Almost everything you want to know about this plugin can be found on the plugin’s site: http://mojo.codehaus.org/buildnumber-maven-plugin/
Alternatives
As an alternative to writing and importing version.html, you can read buildNumber.properties in an MVC controller and put the version in your page model. This is what I for my login pages. I use a java class to read the build number, format it, and cache it. I start the first build number at 00100, then format that to 0.1.0. So the next build is 00101 which gets displayed as 0.1.1.
Update:Tips
Here is a usage tip. You can move the create goal into a build profile so that the build number only increments in specific situations. I use a profile for my Continuous Integration builds so that our version increments only when Hudson builds our apps. This also makes it possible to synchronize our Hudson build numbers to our module versions.
Feedback
What do you use to track build numbers? How do your format it?
Strange Loop 2009 – Day 2
My notes and thoughts about day two of strange loop 2009.
Also be sure to check out my Day one notes.
jQuery – Matt Taylor
- http://weblog.dangertree.net/
- Showed numerous examples how easy jQuery makes it to select and edit HTML elements. Made me realize I should use jQuery even in small apps.
- Also showed examples of the jQuery AJAX functionality
Mobile Development 101
- http://www.slideshare.net/michael.galpin
- http://fupeg.blogspot.com/
- This session was full which doesn’t surprise me since mobile development is about to get easier thanks to Android.
- Started off going how to develop apps for the iPhone
- The emulator and development software only run on a Mac
- Has to be written in Objective C
- I wanted to throw up when I saw the objective c examples
- No garbage collection. Memory is limited on phones so this is an important issue.
- Maybe I’m just to much of a simpleton, but I really don’t ever want to write any objective C
- MVC framework – cocoa touch
- No background processing on iphone
- Android
- Can run any language that is built on the JVM
- XML format for UI develoment
- Does not use standard MVC pattern. Uses something called Activities and Intents.
- There are multiple android emulators
- Mobile dev best practices (not specific to android or iphone)
- Need lean web services
- Limit network traffic
- Limit audio/video/images
- Mobile Web Application
- Alternative to writing an iPhone/Android app
- A lot of phones have the same browser now
- Some mobile browsers leak memory
Entrepreneur Talk – Panel Discussion
- This book was mentioned, Outliers: The Story of Success
- Someone said that you need irrational arrogance to start your own company.
- Another person said that start ups are all offense and no defense. Just keep putting stuff out, early and often
- The idea was presented that everyone has risk even the person working for a company because you can be fired at any time
- The current work culture is sort of like having one client at a time because developers change jobs so often.
Polyglot Grails – Jeff Brown
- http://www.nofluffjuststuff.com/conference/speaker/jeff_brown
- http://javajeff.blogspot.com/
- You can use many languages with Grails. Jeff even gave an example of Grails with a simple language he created
- methodMissing(string name, args) is a very interesting feature of Groovy
Minimalism – Alex Payne
- http://twitter.com/Al3X
- http://al3x.net/books_talks.html
- Interesting talk about little m minimalism and how it be applied to code
- Alex made a few comments about how programming is a young field
List of slides on strangeloop.com
Day two sessions

Strange Loop 2009 – Day 1
A new developer conference has started in St Louis this year named Strange Loop. Normally I don’t go to developer conferences because they are either in a different country or on the coasts. This one was close by in St Louis, MO. And from the quality of speakers and diverse sessions I predict next years conference will sell out very quickly unless they increase the capacity. Below are my overall topics/themes I took away from the conference and some interesting points from each talk I witnessed along with links to the speaker’s site and slide show if available.
Day two
Strange Loop Thoughts Overall
- open source, open source, open source
- every talk envolved open source. The FUD around open source is finally withering away.
- DRY constant theme
- Mobile development is upon us and will be for maybe ever
- Scala, Clojure, Groovy are HOT languages on the JVM
- Great conference that covered many relevant topics
- All the talks were technical, no boring management oriented sales pitches
Functional Ruby – Dean Wampler
- http://deanwampler.com/
- Link to slide show
- I atteneded this talk because I’m interested in the functional programming line of thought. And I don’t get to see much ruby code.
- Recommends learning a functiona programming language: Scala, Hascal, Cojure
- Multiprocessor systems are making it more important to use threads. Threads and synchronizaton is hard to get right. So functional languages that have immutable objects/values are easier because immutable objects/values don’t require synchronization.
- Try to avoid a bloated domain objects that have numerous properties to fullfill multiple requirements for different use cases. Have smaller objects for seperate uses.
Polyglot Programming – Dean Wampler
- Link to slide show
- polyglot programming: using more than one programming language. Not like using CSS, JS. But like using Groovy and Java or Groovy and Clojure.
- Dean thinks that Scala could become very popular in the future
- Hybrid object and functional language
- Multiple cpus is driving the need for functional languages
- Mentioned the famous quote: “Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.”
– Antoine de Saint-Exuper
Griffon (Swing just got fun again) – James Williams
- http://jameswilliams.be/blog/entry/index
- http://groovy.codehaus.org/Griffon
- Mentioned that the swring application framework is dead
- Griffon is the unofficial grails for the desktop
- It really did looks rails like from the project layout to the build process
- Suggested Groovy In Action for anyone that wanted to earn Groovy
Future of Java – Bob Lee
- http://crazybob.org
- Writes his talks in Java. I’m serious. Here is the SVN repo for his talks
- Talked about how/why he wanted better resource management in Java.
- Said he searched the JDK and found that 74 out of 110 the JDK has leaks for a certain IO functionality.
- Talked about his work on JSR-330 dependency injection in JDK
- @Inject annotation
- New quicksort that was developed for Android JVM (Harmony)
