Wednesday, June 27, 2012

Maven Build Number Plugin - Sample Usage

Suppose we need to add a build number to some artifact (jar, war, etc.). Here I'd like to demonstrate the usage of buildnumber-maven-plugin.

This post is based on:

We have some project and need to include into jar manifest file sequential build number which isn't based on VCS (SVN, Git, Mercurial, etc.) revision number. Let's create appropriate pom.xml file and implement small demo to verify the result.

Generate maven project
$ mvn archetype:generate -DgroupId=org.halyph -DartifactId=buildNoTest\
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false

Create the pom.xml

 4.0.0
 org.halyph
 buildNoTest
 jar
 1.0-SNAPSHOT
 buildNoTest
 http://maven.apache.org

 
  
   junit
   junit
   3.8.1
   test
  
 

 
  UTF-8
 

 <!-- 
 If you have access to scm then you can place actual url's. 
 Otherwise with <revisionOnScmFailure /> you can give some fake URLs as follows. 
 -->
 
  scm:svn:http://none
  scm:svn:https://none
  scm:svn:https://none
 

 
  
   
    src/main/resources
   
   
    src/main/filtered-resources
    true
   
  
  
   
    org.codehaus.mojo
    buildnumber-maven-plugin
    1.1
    
     
      generate-resources
      
       create
      
     
    
    
     <!-- 
      doCheck and doUpdate actually talk to repository if it's true,
      Check would check that there are no local changes. 
      Update would update it 
     -->
     false
     false
     <!-- 
      This ensures that even if we are not connected to scm than also
      take the version from local .svn file 
     -->
     
     
     <!--
      Generate sequence build number based on:
      build number and timestamp      
     -->
     Build: #{0} ({1,date})
     
      buildNumber\d*
      timestamp
     
    
   
   
    org.apache.maven.plugins
    maven-jar-plugin
    2.1
    
     
      <!-- will put the entries into META-INF/MANIFEST.MF file -->
      
       ${project.version}
       ${buildNumber}
      
     
    
   
  
 



Create demo application to verify the results
package org.halyph;

import java.io.IOException;
import java.util.ResourceBundle;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

public class App
{
    public static void main( String[] args ) throws IOException
    {
        System.out.println("Verify Resource bundle" );
  
 // Check filtered resources based on generated build number
        ResourceBundle bundle = ResourceBundle.getBundle( "build" );
        String msg = bundle.getString( "build.message" );
        System.out.println(msg);

        System.out.println("\nVerify Generated MANIFEST.MF Properties" );

 // Check Manifest file based on generated build number
        Manifest mf = new Manifest();
        mf.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"));

        Attributes atts = mf.getMainAttributes();

        System.out.println("Implementation-Versio: " + atts.getValue("Implementation-Version"));
        System.out.println("Implementation-Build: " + atts.getValue("Implementation-Build"));
    }
}

Build application several time and Run
$ mvn install 
$ mvn install 
$ mvn install  
$ java -cp target\buildNoTest-1.0-SNAPSHOT.jar org.halyph.App
Verify Resource bundle
Build: #3 (Jun 27, 2012)

Verify Generated MANIFEST.MF Properties
Implementation-Versio: 1.0-SNAPSHOT
Implementation-Build: Build: #3 (Jun 27, 2012)

Summary
  1. We should inform buildnumber-maven-plugin that we won't use version control revision as build number via adding fake <scm> section into pom.xml and <revisionOnScmFailure /> into buildnumber-maven-plugin <configuration>
  2. Implemented custom build number format, see buildnumber-maven-plugin <configuration>/<format> and <configuration>/<items>.
  3. Added build number into jar manifest, see maven-jar-plugin pom.xml section
  4. Tested if generated build number can be properly added in filtered resources
    • created src\main\filtered-resources\build.properties file
    • build.message=${buildNumber}
      
    • added resource filtering, see section <resource> flag <filtering>true</filtering>
  5. Demo application verifying the filtered resources and build number in jar manifest file
 You can git clone this project github

2 comments:

  1. Wοw, ѕuреrb blog struсture!

    Ηow lеngthy haѵe yоu been runnіng a blοg fоr?

    you maκe blogging glancе easу. Thе οveгаll glance of
    yοur site iѕ magnіficent, as wеll аs the content material!


    Feel frеe to viѕіt my ωeb site ::
    cheap shopping

    ReplyDelete
  2. I tried the same thing....but got buildNumber as null.
    Implementation-Build:

    ReplyDelete