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.
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 |
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.
<version>
to root pomroot/pom.xml
<version>0.0.1.${buildDate}-${revision}</version>
<properties>
to the root pomroot/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.
<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.
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>
)
$ 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
$ 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
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)
.
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