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:
- Open the project’s
pom.xml
file. - Include the plugin within the
build
section (after thedependencies
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>
- 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>
- Save the
pom.xml
file. - 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.