Scott Drew
Scott Drew

Reputation: 718

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.

Upvotes: 44

Views: 64852

Answers (6)

Randakar
Randakar

Reputation: 1035

Nowadays I would recommend avoiding the Maven Release plugin, as well as the various handcrafted hacks mentioned in other answers.

Anything that results in file changes on a source code level is something that creates new commits and therefore needlessly pollutes your version history.

The only thing that should be responsible for managing version numbers for source code is your version control management system. Git, in other words.

The good news is that there are a few very good options available, tools that will put your git tags in control of the version numbering instead of the other way around.

The one I would recommend in your case is jgitver, which supports both Maven and Gradle setups in the form of plugins and is very very configurable.

All you need to do is add some files to the root of your project, configure it to taste, and set your version number in pom.xml to 0. Done. See jgitver usage, here.

Upvotes: 5

TheJeff
TheJeff

Reputation: 4101

Use the versions maven plugin via your CI/CD tool prior to the use case where you need your build incremented!

https://www.mojohaus.org/versions-maven-plugin/usage.html

Upvotes: 0

davidxxx
davidxxx

Reputation: 131326

You can do it with without pom modification with the help of maven plugins and scripting.

For example to increment automatically the second digit of a version that doesn't have snapshot as prefix like in your case, on a linux system you can do :

#get the current pom version and store it into a variable
version=$(mvn -q -U -Dexpression=project.version -DforceStdout maven-help-plugin:evaluate)
#increment the second digit of the version and overwrite the variable
version=$(echo ${version} |  awk -F'.' '{print $1"."$2+1"."$3}' |  sed s/[.]$//)
#update the pom with the new version
mvn -U versions:set -DnewVersion=${version}

It works for versions with 3 digits but also 2 digits thanks to the sed.
Of course move the default increment part : "+1" on the digit that suits to your requirements :

# increment the first digit
awk -F'.' '{print $1+1"."$2"."$3}'

# increment the second digit
awk -F'.' '{print $1"."$2+1"."$3}'

# increment the last digit
awk -F'.' '{print $1"."$2"."$3+1}'

Upvotes: 6

Steffen
Steffen

Reputation: 2642

I have the some problem and using the answer of "Dave Jarvis". This adds the correct version and build number to the localy generated jar file like: myProject-3.1.1234.jar. But in the maven repository the artifact is installed as version "3.1" instead of (I have expected) "3.1.1234". The build number ist still missing there...

To install the artifact into the maven repository with the correct version and build number ("3.1.1234") you must do (additionally to the answer of "Dave Jarvis"):

  1. Adding a major/minor version property:
<properties>
  <!-- !!!!!!!!! SETTING HERE THE CORRECT MAJOR/MINOR VERSION NUMER !!!!!!!!!! -->
  <major.minor.version>1.0</major.minor.version>
</properties>
  1. Setting the correct version number to the artifact by using the nashorn-maven-plugin:
<!-- set "major.minor.revision" version -->
<plugin>
  <groupId>io.github.michaldo</groupId>
  <artifactId>nashorn-maven-plugin</artifactId>
  <version>0.0.1</version>
  <executions>
    <execution>
      <phase>validate</phase>
      <goals>
        <goal>eval</goal>
      </goals>
      <configuration>
        <script>
          $project.artifact.version = "${major.minor.version}.${buildNumber}";
        </script>
      </configuration>
    </execution>
  </executions>
</plugin>    

After rebuilding/installing the project the artifact will be in the maven repository as version "3.1.1234" :-)

Please note: to increment the major or minor version you must only change the major.minor.version property!

Upvotes: 2

Hieu Rocker
Hieu Rocker

Reputation: 1070

You can use git commit count of the current branch as the build number. Use this command line to get the commit count: git rev-list HEAD --count.

Integrating with the build tools will be quite straightforward. If you are using Gradle then you can use this plugin: https://github.com/rockerhieu/Versionberg/

Upvotes: 4

Dave Jarvis
Dave Jarvis

Reputation: 31171

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

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.

Upvotes: 36

Related Questions