One of the nicest features of Git is that it’s possible to use it locally against a Subversion server. For many this is a crucial selling point as it allows individual developers to take advantage of Git while not requiring that the entire project switches version control from Subversion. On your local Git repository you’re then free to do as many branches as you like, or otherwise take advantage of the many nice features in Git.
However, if your project is set up to use the Maven automatic build versioning as provided by the buildnumber-maven-plugin you will run into problems if it is set to get the build numbers from the Subversion revision number. Since your local checkout is against Git, and not Subversion, the build will fail since the Subversion commands won’t work.
One solution for this could be to alter the plugin configuration to get the build number from the date or something similar, but that’s not always possible. You might have a build server setup that depends on the build number matching the revision number, and imposing such a change might break other functionality or processes.
Instead you can add an additional profile to the Maven POM which will only be triggered in the absence of a .svn directory (i.e. when not using Subversion). In this profile you can override the default configuration for the build number generator to instead use the date for build numbers. This means that your builds will have different build numbers from the integration builds, but at least it will compile on your git checkout.
The profile will look like this:
<profile> <!-- This profile is here for triggering when another scm than svn is used (for example git). Instead of getting the version build number from svn we will use the build date and the user name. --> <id>buildnumber-git</id> <activation> <file> <missing>.svn</missing> </file> </activation> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>create</goal> </goals> </execution> </executions> <configuration> <doCheck>false</doCheck> <doUpdate>false</doUpdate> <format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format> <items> <item>timestamp</item> <item>${user.name}</item> </items> </configuration> </plugin> </plugins> </build> </profile>
This way Subversion and Git users can live happily together in the same project.
This is an excellent suggestion. I’ll share it with some students in my next Maven class on Git + SVN dual-use environments.
Great tip! I will try it when I have installed Git.
thanks for sharing the great tip..i will follow ur blog for future help
awesome tip. Thank you so much!
Thank you, that is useful!
You forgot to credit ehcache for this tip as it has been copied from the ehcache-core main pom: http://svn.terracotta.org/svn/ehcache/trunk/ehcache/ehcache-core/pom.xml
I’m sure I got inspiration for it from somewhere, but I can’t remember exactly from where. It might very well be from EHCache, but the fact that you added it to the core pom in commit 2678 which is dated 2010-08-25, i.e. almost a year after this blog post was written, seems to contradict that assertion.