Enterprise Java

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

1
2
3
$ mvn archetype:generate -DgroupId=org.halyph -DartifactId=buildNoTest\
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false

Create the pom.xml

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 <modelversion>4.0.0</modelversion>
 <groupid>org.halyph</groupid>
 <artifactid>buildNoTest</artifactid>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>buildNoTest</name>
  
 <dependencies>
  <dependency>
   <groupid>junit</groupid>
   <artifactid>junit</artifactid>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
  
 <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 fake URLs as follows.
 -->
 <scm>
  <connection>scm:svn:http://none</connection>
  <developerconnection>scm:svn:https://none</developerconnection>
  <url>scm:svn:https://none</url>
 </scm>
  
 <build>
  <resources>
   <resource>
    <directory>src/main/resources</directory>
   </resource>
   <resource>
    <directory>src/main/filtered-resources</directory>
    <filtering>true</filtering>
   </resource>
  </resources>
  <plugins>
   <plugin>
    <groupid>org.codehaus.mojo</groupid>
    <artifactid>buildnumber-maven-plugin</artifactid>
    <version>1.1</version>
    <executions>
     <execution>
      <phase>generate-resources</phase>
      <goals>
       <goal>create</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <!--
      doCheck and doUpdate 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>
       
     <!--
      Generate sequence build number based on:
      build number and timestamp     
     -->
     <format>Build: #{0} ({1,date})</format>
     <items>
      <item>buildNumber\d*</item>
      <item>timestamp</item>
     </items>
    </revisiononscmfailure></configuration>
   </plugin>
   <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-jar-plugin</artifactid>
    <version>2.1</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>
  </plugins>
 </build>
</project>

Create demo application to verify the results

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

1
$ mvn install
1
2
3
4
5
6
7
$ 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
    • 1
      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 https://github.com/halyph/blog-sandbox/tree/master/Maven/blogpost_062712

Reference: Maven Build Number Plugin – Sample Usage from our JCG partner Orest Ivasiv at the Knowledge Is Everything blog.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Orest Ivasiv

Passionate software engineer interested in different programming languages, pragmatic approaches and performance boost practices.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
dharmendra prasad
dharmendra prasad
11 years ago

Hi,

I tried this and it works. Thanks for the tutorial. Please tell me how to add the latest revision number from svn repository when I have a successful repository connection in place

Back to top button