Generate Build Number Maven

Generate Build Number Maven

While working with maven project and if you are using any SCM as repository which is mandatory in any company then it’s required to generate build number dynamically when build new jar of the project. As this is very simple by using org.codehaus.mojo plugin in your maven pom.xml. I will be creating sample project where I am using git as reposiroty and javahonk Git is public repository you can use it without any issue. Sample maven project structure:

Generate Build Number Maven

  • You could download this project form Git here
  • Please add below to your maven pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javahonk</groupId>
	<artifactId>GenerateMavenBuildNumber</artifactId>
	<version>${version}</version>
	<name>GenerateMavenBuildNumber</name>

	<properties>
		<version>1.0</version>
	</properties>

	<build>
		<finalName>${project.artifactId}-${project.version}_${buildNumber}</finalName>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>buildnumber-maven-plugin</artifactId>
				<version>1.1</version>
				<executions>
					<execution>
						<phase>validate</phase>
						<goals>
							<goal>create</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<doCheck>false</doCheck>
					<doUpdate>false</doUpdate>
					<revisionOnScmFailure>true</revisionOnScmFailure>
					<format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
					<items>
						<item>timestamp</item>
						<item>${user.name}</item>
					</items>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<scm>
		<connection>scm:git:git://github.com/javahonk/GenerateMavenBuildNumber.git</connection>
	</scm>

</project>
  • Use maven command mvn install and once build is done you will see below jar with unique version in target folder:

Generate Build Number Maven

  • For more information please visit maven tutorial here

Leave a Reply

Your email address will not be published. Required fields are marked *