Tuesday, November 8, 2011

Maven Build Number Plugin

For web application usually it's a requirement to pull the latest change set number from source code control and show it up somewhere in application. For the same we can use buildnumber-maven-plugin. To put the plugin in the pom, follow the following XML. Also note that I have tested it with svn only. For others you might want to play with provider implementations.
 
<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>buildnumber-maven-plugin</artifactId>
   <version>1.0</version>
   <executions>
 <execution>
           <phase>validate</phase>
    <goals>
       <goal>create</goal>
    </goals>
 </execution>
   </executions>
   <configuration>
        <!-- do Check and do update actually talk to repository if it's true,
          Check would check that there are no local changes. Update would update it -->
 <doCheck>false</doCheck>
        <doUpdate>false</doUpdate>
        <!-- This ensures that even if we are not connected to scm than also take
          the version from local .svn file -->
 <revisionOnScmFailure />
   <providerImplementations>
 <svn>javasvn</svn>
   </providerImplementations>
   </configuration>
</plugin>
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-war-plugin</artifactId>
   <version>${maven.war.plugin.version}</version>
   <configuration>
      <archive>
       <!-- will put the entries into META-INF/MANIFEST.MF file -->
 <manifestEntries>
     <Implementation-Version>${project.version}</Implementation-Version>
     <Implementation-Build>${buildNumber}</Implementation-Build>
 </manifestEntries>
      </archive>
   </configuration>
</plugin>
The above configuration gives an error that "The scm url cannot be null." This comes even after you have given <revisionOnScmFailure />. To workaround this issue, you need to provide an scm entry in pom.xml as follows
<properties>  
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- If you have access to scm then you can place actual url's. Otherwise with
   <revisionOnScmFailure /> you can give some false urls as follows. -->
<scm>
 <connection>scm:svn:http://none</connection>
 <developerConnection>scm:svn:https://none</developerConnection>
 <url>scm:svn:https://none</url>
</scm>
Once this is done you will see the MANIFEST.MF file containing two entries as Implementation-Version and Implementation-Build. You can read that from the manifest file and inject that into your pages.

No comments:

Post a Comment