[JAVA] Automatically assign date and build number for the day to Maven version

The version of Maven project is basically written in pom.xml, but ~~ basically you have to raise the version manually. ~~

** Addendum 2020/04/03 ** If you use the release plugin, z in x.y.z will be incremented automatically.

But what if you want to give it a date, a timestamp, and a build number?

Rewrite pom.xml with sh script? Isn't it too hard for multi-modules? Rewrite all at once with mvn versions: set -DnewVersion = whatever? It will take more time, right? Do you end up needing a script to build?

It seems that Jenkins and CI tools can be done with plugins ...

What I want to do here is not to manage the file name that is the product of Arfict, but to attach the version of Artifact itself.

Goal

Automatically assign the date and build number to the module's version with mvn install etc. Example) 0.0.1.20200401-2

Realize it without writing a script by yourself.

It is permissible to use some CI functions. ... What that means is that you can define a template for version for the time being, and if you can pass some values from the CI tool with a build command, it's OK.

environment

Tools etc. Version etc.
MacbookPro macOS Mojave 10.14.5
IntelliJ IDEA Ultimate 2019.3.3
Java AdoptOpenJDK 11
apache maven 3.6.3
Spring Framework 5.2.4.RELEASE
JUnit 5.6.0
Tomcat apache-tomcat-8.5.51

Realization method

The bottom line is that you can give a date and build number by ** allowing some CI features to be used **. In addition, this project has a multi-module configuration, and all modules use the parent version and the versions are completely matched. Simply put, in submodules

pom.xml


	<artifactId>openfhir-api</artifactId>
	<packaging>war</packaging>

Just do not put the version tag. Put it only in the root pom.xml. Then all the submodules will be the same version.

1. Setting <version> to root pom

root/pom.xml


    <version>0.0.1.${buildDate}-${revision}</version>

2. Setting <properties> to the root pom

root/pom.xml


    <properties>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--Date used for versioning-->
        <buildDate>${maven.build.timestamp}</buildDate>
        <!--Date format used for versioning-->
        <timestamp>${maven.build.timestamp}</timestamp>
        <maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>

        <!--The revision number used for versioning.
        CI/With a CD tool-Drevision=Overwrite with buildNumber-->
        <revision>SNAPSHOT</revision>
    </properties>

As you can see in the comments, <revision> can be specified from the command line. If omitted, SNAPSHOT will be added.

3. Also change the <version> of the submodule <parent>

submodule/pom.xml


	<parent>
		<groupId>jp.example.myapp</groupId>
		<artifactId>testsample</artifactId>
		<version>0.0.1.${buildDate}-${revision}</version>
	</parent>

Don't forget to do it for all submodules.

4. Build

Try building with mvn clean install and mvn install -Drevision = 2. You can check it by the pom.properties in the target folder of each module and the file name of the generated jar / war file. (If not overwritten with <build> / <finalName>)

(1) No revision specified

$ mvn clean install
$ ls -la **/target/*.?ar
17:02 submodule/target/submodule-0.0.1.20200402-SNAPSHOT.war
17:02 core/target/core-0.0.1.20200402-SNAPSHOT.jar

submodule/target/maven-archiver/pom.properties


#Generated by Maven
#Thu Apr 02 17:02:10 JST 2020
groupId=jp.example.myapp
artifactId=testsample
version=0.0.1.20200402-SNAPSHOT

(2) With revision specified

$ mvn clean install -Drevision=2
$ ls -la **/target/*.?ar
17:29 submodule/target/openfhir-api-0.0.1.20200402-2.war
17:29 core/target/openfhir-core-0.0.1.20200402-2.jar

submodule/target/maven-archiver/pom.properties


#Generated by Maven
#Thu Apr 02 17:29:47 JST 2020
groupId=jp.example.myapp
artifactId=testsample
version=0.0.1.20200402-2

5. Give the build number in CI

I think that the execution command in CI is mvn install etc., so you should add the -Drevision = option there. Please refer to the CI tool for the "build number of the day", as there are different ways to get it.

For the time being, with Jenkins, if you use a plugin called Version Number, you can use BUILDS_TODAY. (However, the time zone to be reset is unknown ...)

For Azure Pipelines, go to the Configure run or build numbers page According to it, you can go by using $ (Rev: r).

reference

It was a reference for how to do -Dreivision. https://stackoverflow.com/questions/18456111/what-is-the-maven-way-for-automatic-project-versions-when-doing-continuous-deliv

It was helpful for versioning management in multi-module. https://qiita.com/ms0_mtRiver/items/85593b17d6ebb8fb2cfd

Recommended Posts

Automatically assign date and build number for the day to Maven version
[CentOS] Download and build the specified version of Git
How to add another project as Maven library with CircleCI and use it for build
Import the date string automatically converted to Excel with java.time.LocalDate
Command to check the number and status of Java threads
I want to call a method and count the number
Difficult to handle minimum values for Java8 LocalDateTime and Java6 Date
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
I tried to express the result of before and after of Date class with a number line
Learn the Maven 3.5 build lifecycle
Method to add the number of years and get the end of the month
How to install the language used in Ubuntu and how to build the environment
"Wait for the process to finish." And kill the process because it remains.
Install Ubuntu20.04 on RaspberryPi 4 and build Kubernetes to run the container