简体   繁体   中英

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!

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.

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)

I highly recommend reading Maven Releases on Steroids ( part 2 , part 3 ) by Axel Fontaine. It is great, and I'm quite happy using it.

It not only details how you con do what you ask, but also contains good advice how you can tie your build versions with your CI server.

In a nutshell, here are the main points:

  1. Maven Release is slow, needs to be done faster
  2. You parametarize your project version like

     <version>${VERSION_NUMBER}</version> ... <properties> ... <VERSION_NUMBER>1.0-SNAPSHOT</VERSION_NUMBER> ... </properties> 
  3. Local builds get that version: 1.0-SNAPSHOT
  4. Release builds are done only from your CI server
  5. In your Jenkins/Hudson project configuration you use

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

That way you get a new release with each Jenkins build, not only nightly.


You can change the configuration to use

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

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

You could 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 drive the version number from command line using

mvn package -Dmy.project.version=NIGHTLY

Although this is possible, Maven 3 discourages it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM