I've a Java project using Spring Framework and Git and I wanted to display a build number. I found the Build Number Maven plugin. With Git the build number is a Git hash. I dislike that and I thought a date was much more expressive.
I found this excellent blog article explaining how to use build number plugin with a different profile for SVN and Git. Since I just use Git, instead of creating a new profile, I just copied the plugin part in my build tag.
When I run "mvn package" it tells me:
[INFO] --- buildnumber-maven-plugin:1.0:create (default) @ sherd ---
[INFO] Storing buildNumber: 2011-08-04_21-48_stivlo at timestamp: 1312487296631
Which looks ok, but I wonder, where is it stored? "git status" doesn't detect any new file and it seems it's not in target/ too (target/ is in my .gitignore).
Maybe I've to change the configuration to store the build number in a file? How can I use the build number value?
Thanks to the hint of Michael-O, I read the chapter about how to filter resource files in Maven Getting Started Guide. I've created a file application.properties in src/main/resources/properties/application.properties with the following contents:
# application properties
application.name=${pom.name}
application.version=${pom.version}
application.build=${buildNumber}
I've added the following XML snippet within my build section:
<resources>
<resource>
<directory>src/main/resources/properties</directory>
<filtering>true</filtering>
</resource>
</resources>
Now when I call from command line "mvn package", this property file gets saved in target/classes/properties/application.properties, for instance with the following contents:
# application properties
application.name=Sherd Control Panel
application.version=1.0.1-SNAPSHOT
application.build=2011-08-05_05-55_stivlo
Everything works fine from command line, but, sigh, m2eclipse gives Build errors:
05/08/11 6.05.03 CEST: Build errors for obliquid-cp;
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal
org.codehaus.mojo:buildnumber-maven-plugin:1.0:create (default) on project
sherd: Cannot get the branch information from the scm repository :
Exception while executing SCM command.
For some reason m2eclipse tries to connect to my repository, but it can't because it's a Git repository accessed with SSH and a private key. I wonder if I can tell m2eclipse to not connect to Git.
After more digging I found about revisionOnScmFailure option, set it to true and now also m2eclipse works. For reference, here is the full configuration of buildnumber maven plugin that I used (it goes in pom.xml in the build / plugins section).
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<revisionOnScmFailure>true</revisionOnScmFailure>
<format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
<items>
<item>timestamp</item>
<item>${user.name}</item>
</items>
</configuration>
</plugin>
Store it in a filtered properties file. See Using maven to output the version number to a text file