Is there a way to configure the Maven POM version from the command line?

Is there a way to change the version number without editing the POM?

<groupId>org.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> 

We have a CI system in which we want to release nightly builds, but without using the -SNAPSHOT Maven solution, so if 1.0.0 is the current version, we just want to have CI-NIGHTLY-BIULD-20120426 .

I suggested that this is possible with something like mvn deploy -Dversion=CI-NIGHTLY-BIULD-20120426 , but obviously not. A bad solution would be to let the CI server edit pom.xml every time, but I think it is very inconvenient.

Thanks!

+6
source share
4 answers

I suggest using a classifier.

 <groupId>foo</groupId> <artifactId>bar</artifactId> <version>1.0</version> <properties> <!-- default classifier is empty --> <my.project.classifier></my.project.classifier> </properties> <build> ... <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <classifier>${my.project.classifier}</classifier> </configuration> <executions>...</executions> </plugin> </plugins> </build> 

and

 mvn package -Dmy.project.classifier=NIGHTLY-2012-04-26_02-30 

Maven documentation says about the classifier:

class: you can sometimes find the fifth element by coordinate, and this is a classifier. We will visit the classifier later, but for now it’s enough to know that such projects are displayed as groupId: artifactId: packaging: classifier: version.

and

The classifier allows you to distinguish between artifacts that were built from the same POM, but differ in their content. These are some optional and arbitrary strings that - if present - are added to the artifact name immediately after the version number. As a motivation for this element, consider, for example, a project that offers an artifact intended for JRE 1.5, but at the same time also an artifact that still supports JRE 1.4. The first artifact can be equipped with the jdk15 classifier and the second with jdk14, so that customers can choose which one to use.

Another common use of classifiers is the need to connect secondary artifacts to the main project artifact. If you are browsing Maven, you will notice that classifier and javadoc sources are used to deploy project source code and API documents along with packaged class files.

+6
source

I think you can also use versions maven plugin. I find this very useful for such things.

You can do this in 2 steps:

  • install the required version: mvn versions:set -DnewVersion=CI-NIGHTLY-BIULD-20120426
  • deploy: mvn deploy
  • if you need to mvn versions:revert , use mvn versions:revert (as Mark points out)
+2
source

I highly recommend reading Maven releases on steroids ( part 2 , part 3 ) Axel Fontaine. This is great and I am very pleased with it.

It not only details how you do what you ask, but also provides good advice on how you can link your build versions to your CI server.

In short, here are the highlights:

  • Maven release is slow, needs to be done faster
  • You parameterize your version of the project, for example

     <version>${VERSION_NUMBER}</version> ... <properties> ... <VERSION_NUMBER>1.0-SNAPSHOT</VERSION_NUMBER> ... </properties> 
  • Local builds get this version: 1.0-SNAPSHOT
  • Release builds are only from your CI server
  • In your Jenkins / Hudson project configuration, you use

     clean deploy scm:tag -DVERSION_NUMBER=${BUILD_NUMBER} 

This way you get a new release with every Jenkins build, not just at night.


You can change the configuration to use

 clean deploy scm:tag -DVERSION_NUMBER=1.0.0-CI-NIGHTLY-BIULD-${BUILD_ID} 

and you will get versions like 1.0.0-CI-NIGHTLY-BIULD-2012-04-26_12-20-24

+1
source

You can parameterize the version number as

 <groupId>foo</groupId> <artifactId>bar</artifactId> <version>${my.project.version}</version> <properties> <my.project.version>1.0</my.project.version> </properties> 

and enter the version number from the command line using

 mvn package -Dmy.project.version=NIGHTLY 

Although it is possible, Maven 3 scares him away.

-1
source

Source: https://habr.com/ru/post/914208/


All Articles