Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically incrementing a build number in a Java project

I'm using a versioning system that is represented by a.b.build where a is the overall version (will be 0 for prototype, alpha and beta versions, 1 for major release), b is the milestone version (along the lines of representing the proto, alpha, beta stages) and build represents literally the amount of times the project has been compiled.

At the moment, I have the app read from a text file, increment the number, and save to a text file when the app is run with a debug flag set.

I'm looking for a more "correct" way to do this using Java and Netbeans. Is there some way I can inject a build numberer into the build process somewhere? preferably saving the number into a source file that is shipped with the project - instead of relying on the existence of a nearby file.

like image 558
Scott Drew Avatar asked Jan 24 '12 14:01

Scott Drew


1 Answers

There are a couple of popular Maven plugins that accomplish this feat:

  • Maven Release Plugin
  • Build Number Maven Plugin

The Maven Release Plugin from the Apache Maven Project is a bit overkill for simply updating the version number. Therefore use the latter plugin to create a version number (in the form of MAJOR.MINOR.BUILD; e.g., 3.1.4 where 4 is auto-incremented) as follows:

  1. Open the project's pom.xml file.
  2. Include the plugin within the build section (after the dependencies section):
  <scm>     <connection>scm:svn:http://127.0.0.1/dummy</connection>     <developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>     <tag>HEAD</tag>     <url>http://127.0.0.1/dummy</url>   </scm>   <build>     <plugins>       <plugin>         <groupId>org.codehaus.mojo</groupId>         <artifactId>buildnumber-maven-plugin</artifactId>         <version>1.4</version>         <executions>           <execution>             <id>buildnumber</id>             <phase>validate</phase>             <goals>               <goal>create</goal>             </goals>           </execution>         </executions>         <configuration>           <format>{0,number}</format>           <items>             <item>buildNumber</item>           </items>                               <doCheck>false</doCheck>           <doUpdate>false</doUpdate>           <revisionOnScmFailure>unknownbuild</revisionOnScmFailure>            </configuration>       </plugin>         </plugins>     <finalName>${project.artifactId}-${project.version}.${buildNumber}</finalName>   </build> 
  1. Ensure that the pom.xml defines the major and minor version in the version element near the top of the file. For example:
<version>3.1</version> 
  1. Save the pom.xml file.
  2. Rebuild the project.

The version number should increase.


The plugin requires a configured source code management repository (<scm>) element. If you don't care about the repository check-in number use a dummy scm instead. This can be used to include the revision number from the repository, which is an exercise for the reader.

like image 57
Dave Jarvis Avatar answered Oct 15 '22 10:10

Dave Jarvis