Maven version with a property

JavaMavenVersion

Java Problem Overview


I have big Maven (Tycho) project witch about 400 plug-ins.

We have specified version of application in each POM file.

Is there a way how to specify the version for all POM:s only on one place?

I would expect some think like:

<properties>
<buildVersion>1.1.2-SNAPSHOT</buildVersion>
</properties>

....

<version>${buildVersion}</version>

We have parent pom.xml:

<modelVersion>4.0.0</modelVersion>
<groupId>company</groupId>
<artifactId>build.parent</artifactId>
<version>1.1.2-SNAPSHOT</version>
<packaging>pom</packaging>

Then in each POM is reference to parent POM:

<parent>
  <artifactId>build.parent</artifactId>
  <groupId>company</groupId>
  <relativePath>../build.parent/pom.xml</relativePath>
  <version>1.1.2-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<groupId>company</groupId>
<artifactId>artifact</artifactId>
<version>1.1.2-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>

Java Solutions


Solution 1 - Java

If you have a parent project you can set the version in the parent pom and in the children you can reference sibling libs with the ${project.version} or ${version} properties.

If you want to avoid to repeat the version of the parent in each children: you can do this:

<modelVersion>4.0.0</modelVersion>
<groupId>company</groupId>
<artifactId>build.parent</artifactId>
<version>${my.version}</version>
<packaging>pom</packaging>

<properties>
<my.version>1.1.2-SNAPSHOT</my.version>
</properties>

And then in your children pom you have to do:

    <parent>
      <artifactId>build.parent</artifactId>
      <groupId>company</groupId>
      <relativePath>../build.parent/pom.xml</relativePath>
      <version>${my.version}</version>
    </parent>
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>company</groupId>
    <artifactId>artifact</artifactId>
    <packaging>eclipse-plugin</packaging>

    <dependencies>
        <dependency> 
           <groupId>company</groupId>
           <artifactId>otherartifact</artifactId>   
           <version>${my.version}</version>
or
           <version>${project.version}</version>
        </dependency>
    </dependencies>

hth

Solution 2 - Java

The correct answer is this (example version):

  • In parent pom.xml you should have (not inside properties):

      <version>0.0.1-SNAPSHOT</version>
    
  • In all child modules you should have:

      <parent>
          <groupId>com.vvirlan</groupId>
          <artifactId>grafiti</artifactId>
          <version>0.0.1-SNAPSHOT</version>
      </parent>
    

So it is hardcoded.

Now, to update the version you do this:

mvn versions:set -DnewVersion=0.0.2-SNAPSHOT
mvn versions:commit # Necessary to remove the backup file pom.xml

and all your 400 modules will have the parent version updated.

Solution 3 - Java

Using a property for the version generates the following warning:

[WARNING]
[WARNING] Some problems were encountered while building the effective model for xxx.yyy.sandbox:Sandbox:war:0.1.0-SNAPSHOT
[WARNING] 'version' contains an expression but should be a constant. @ xxx.yyy.sandbox:Sandbox:${my.version}, C:\Users\xxx\development\gwtsandbox\pom.xml, line 8, column 14
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]

If your problem is that you have to change the version in multiple places because you are switching versions, then the correct thing to do is to use the Maven Release Plugin that will do this for you automatically.

Solution 4 - Java

See the Maven - Users forum 'version' contains an expression but should be a constant. Better way to add a new version?:

> here is why this is a bad plan.

> the pom that gets deployed will not have the property value resolved, so anyone depending on that pom will pick up the dependency as being the string uninterpolated with the ${ } and much hilarity will ensue in your build process.

> in maven 2.1.0 and/or 2.2.0 an attempt was made to deploy poms with resolved properties... this broke more than expected, which is why those two versions are not recommended, 2.2.1 being the recommended 2.x version.

Solution 5 - Java

With a Maven version of 3.5 or higher, you should be able to use a placeholder (e.g. ${revision}) in the parent section and inside the rest of the POM, you can use ${project.version}.

Actually, you can also omit GAV properties outside of <parent> which are the same, as they will be inherited. The result would look something like this:

<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>build.parent</artifactId>
        <groupId>company</groupId>
        <version>${revision}</version>  <!-- use placeholder -->
        <relativePath>../build.parent</relativePath>
    </parent>

    <artifactId>artifact</artifactId>
    <!-- no 'version', no 'groupId'; inherited from parent -->
    <packaging>eclipse-plugin</packaging>

    ...
</project>

For more information, especially on how to resolve the placeholder during publishing, see Maven CI Friendly Versions | Multi Module Setup.

Solution 6 - Java

If you're using Maven 3, one option to work around this problem is to use the versions plugin http://www.mojohaus.org/versions-maven-plugin/

Specifically the commands,

mvn versions:set -DnewVersion=2.0-RELEASE
mvn versions:commit

This will update the parent and child poms to 2.0-RELEASE. You can run this as a build step before.

Unlike the release plugin, it doesn't try to talk to your source control

Solution 7 - Java

I have two recommendation for you

  1. Use CI Friendly Revision for all your artifacts. You can add -Drevision=2.0.1 in .mvn/maven.config file. So basically you define your version only at one location.
  2. For all external dependency create a property in parent file. You can use Apache Camel Parent Pom as reference

Solution 8 - Java

The version of the pom.xml should be valid

<groupId>com.amazonaws.lambda</groupId>
<artifactId>lambda</artifactId>
<version>2.2.4 SNAPSHOT</version>
<packaging>jar</packaging>

This version should not be like 2.2.4. etc

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJan PeštaView Question on Stackoverflow
Solution 1 - JavaFrederic CloseView Answer on Stackoverflow
Solution 2 - JavaACVView Answer on Stackoverflow
Solution 3 - JavaConstantino CronembergerView Answer on Stackoverflow
Solution 4 - JavaGerold BroserView Answer on Stackoverflow
Solution 5 - JavaKariemView Answer on Stackoverflow
Solution 6 - JavarinceView Answer on Stackoverflow
Solution 7 - JavaHemangView Answer on Stackoverflow
Solution 8 - JavaSandun SusanthaView Answer on Stackoverflow