How to get git build id using maven

There are times when git's build number is important for a release. Specially in development mode, when there are frequent releases. So if we want to append the build number on our page, how do we automate it?

For us to achieve this we will need 2 maven plugins: org.codehaus.mojo:buildnumber-maven-plugin and com.google.code.maven-replacer-plugin:replacer.

And this is how to define these plugins in your war project.

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>buildnumber-maven-plugin</artifactId>
 <version>1.3</version>
 <executions>
  <execution>
   <phase>validate</phase>
   <goals>
    <goal>create</goal>
   </goals>
  </execution>
 </executions>
 <configuration>
  <doCheck>false</doCheck>
  <doUpdate>false</doUpdate>
  <shortRevisionLength>41</shortRevisionLength>
 </configuration>
</plugin>
This plugin, produces buildNumber variable, which can be an id, date or a git build no. We will use the last.

With the following assumptions:
1.) The xhtml pages that we want to process for replacement are inside template folder.
2.) We will replace 2 strings: VERSION_NUMBER and BUILD_NUMBER

<plugin>
 <groupId>com.google.code.maven-replacer-plugin</groupId>
 <artifactId>replacer</artifactId>
 <version>1.5.3</version>
 <executions>
  <execution>
   <phase>prepare-package</phase>
   <goals>
    <goal>replace</goal>
   </goals>
  </execution>
 </executions>
 <configuration>
  <includes>
   <include>${project.build.directory}/**/layout/*.xhtml</include>
  </includes>
  <replacements>
   <replacement>
    <token>@VERSION_NUMBER@</token>
    <value>${project.version}</value>
   </replacement>
   <replacement>
    <token>@BUILD_NUMBER@</token>
    <value>[${buildNumber}]</value>
   </replacement>
  </replacements>
 </configuration>
</plugin>
How to get git build id using maven How to get git build id using maven Reviewed by Edward Legaspi on Tuesday, November 11, 2014 Rating: 5

No comments:

Powered by Blogger.