33

What is the best practice for specifying version of a multimodule maven project?

I would like to have one version string across all modules. Even if I can have only one version definition in the root parent pom, I need to specify the parent pom version in each pom's. Which means, if I need to change version, I need to change all poms. Practically defeats the purpose. Any ideas??

anoopelias
  • 9,240
  • 7
  • 26
  • 39
  • To my understanding this was supposed to come in Maven 3.1 back in November but apparently did not make it. I agree it would be nice, but for now it appear we must have it in every Pom. – Thorbjørn Ravn Andersen Jun 26 '13 at 06:16
  • 1
    A new plugin adresses this issue: http://mojo.codehaus.org/flatten-maven-plugin/examples/example-multiple-versions.html – Stephan Jun 12 '14 at 14:51
  • Not sure why this was marked as a duplicate - because, in the pointed question, all answers indicate having to specify the parent's version in the `` section of the child POM... while the OP clearly states "I would like to have one version string across all modules"! – Janaka Bandara Oct 09 '19 at 02:53

3 Answers3

46

Have you tried the versions-maven plugin ?

with mvn versions:set -DnewVersion="1.1-SNAPSHOT" you are able to set in all underlying maven projects the given version.

Afterwards you have to do a mvn versions:commit to remove temporary files and commit to your VCS

François Maturel
  • 5,884
  • 6
  • 45
  • 50
smsnheck
  • 1,563
  • 3
  • 21
  • 33
  • 2
    Thank you. Yes this works for me. The only drawback being, I need to commit all poms to source control even for a minor-version change. – anoopelias Jun 26 '13 at 10:35
  • 1
    A commit is the right way to save the changes to the pom file which is part of the project and of course of the history which is the intention to use a version control tool. – khmarbaise Jun 26 '13 at 18:11
9

The better way is define your version in parent pom.xml as follows.

<groupId>com.my.code</groupId>
<artifactId>my_pro</artifactId>
<version>${YOUR_VERSION}</version>

<properties>
    <JDK_VERSION>1.7</JDK_VERSION>        
    <YOUR_VERSION>1.0-SNAPSHOT</YOUR_VERSION>// here you define your version
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
</properties>

Then you don't want to change your version number one by one in all child pom.xml.

child pom.xml can add dependency as follows

<version>${YOUR_VERSION}</version>
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • 14
    This way is deprecated since maven 3 – Ilya Jun 26 '13 at 06:28
  • 13
    @Ilya So what is the alternative ? It's a shame to have to change ALL child poms when you just chnage the version number ! – Stephan Jun 12 '14 at 13:36
  • This is what I ended up doing and seems to work for "keep version number value in one place" requirement. – Naymesh Mistry Jan 15 '16 at 00:10
  • 1
    You could use ${project.version} instead – Adriaan Koster Dec 01 '16 at 14:22
  • 1
    @Ilya can you provide source for this? I agree but can't find it anywhere besides warning in the console. – zeratul021 May 04 '17 at 12:58
  • 1
    This works when building the whole project, but fails when building or resuming from (`-rf :`) a module (say `b`) if it has a dependency on another module (say `a`) - with a "cannot resolve parent POM version of `a`" kind of error; any ideas? Using Maven 3.6.1 – Janaka Bandara Oct 09 '19 at 02:29
  • This is possible with ${revision}, but I am not sure if this concept is correct. – Peter S. Apr 01 '20 at 19:37
9

one other "best" way is to create a parent pom beside the multimodule pom which will provide a dependency management containing the multimodule project reference with the version of the parent pom it self. If the multimodule project need one other multimodule project as dependency this will be the best for you. The parent pom have to be part of the multimodule pom:

<modules>
    <module>my.parent.Pom</module>
    <module>my.first.project</module>
    <module>my.second.project</module>
    <module>my.third.project</module>
</modules>

And the parent pom should contains the dependency management for your multimodule project

<version>VersionNumber</version>
<packaging>pom</packaging>
...
...
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.first.project</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.second.project</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.third.project</artifactId>
            <version>${project.version}</version>
        </dependency>

All your multimodule project will not define the groupId and the version number because they will got those information from the parent pom and additionally if the project "third" need the dependency my.first.project then the dependency in the project "third" pom file will be:

<dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>my.first.project</artifactId>
        </dependency>

This is the way or we are dealing with the multi module projects and version management.

ozOli
  • 1,414
  • 1
  • 18
  • 26
user3240094
  • 99
  • 1
  • 1
  • 8
    Interesting! Though, I am wondering how can I specify a parent pom without specifying its version (required inside one of the child poms?). – anoopelias Jan 27 '14 at 11:25
  • 3
    @anoopelias yes this irritates me too, and does not solve your question at all - and mine as well. Because you end up with this with the same problem. How did you solve it? – Mejmo Jan 22 '17 at 18:40