Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to configure the version of the Maven POM from 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 where we want to release nightly builds, but without using the -SNAPSHOT solution of Maven, so if 1.0.0 is the current version, we just want to have CI-NIGHTLY-BIULD-20120426.

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

Thank you!

like image 455
Christopher Klewes Avatar asked Apr 26 '12 09:04

Christopher Klewes


2 Answers

I suggest to use 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 classifier:

classifier: You may occasionally find a fifth element on the coordinate, and that is the classifier. We will visit the classifier later, but for now it suffices to know that those kinds of projects are displayed as groupId:artifactId:packaging:classifier:version.

and

The classifier allows to distinguish artifacts that were built from the same POM but differ in their content. It is some optional and arbitrary string that - if present - is appended to the artifact name just after the version number. As a motivation for this element, consider for example a project that offers an artifact targeting JRE 1.5 but at the same time also an artifact that still supports JRE 1.4. The first artifact could be equipped with the classifier jdk15 and the second one with jdk14 such that clients can choose which one to use.

Another common use case for classifiers is the need to attach secondary artifacts to the project's main artifact. If you browse the Maven central repository, you will notice that the classifiers sources and javadoc are used to deploy the project source code and API docs along with the packaged class files.

like image 99
amra Avatar answered Oct 02 '22 12:10

amra


I think you could also use versions maven plugin. I find it quite useful for things like this.

You could do it in 2 steps:

  1. set necessary version: mvn versions:set -DnewVersion=CI-NIGHTLY-BIULD-20120426
  2. deploy: mvn deploy
  3. in case you need to revert back the changes, use mvn versions:revert (as Mark suggests)
like image 26
Andrew Logvinov Avatar answered Oct 02 '22 13:10

Andrew Logvinov