423

I have a multi-module maven project. We intend to version all these modules together. But as of now I am ending up hard-coding version in each of the module pom.xml as below

<parent>
    <artifactId>xyz-application</artifactId>
    <groupId>com.xyz</groupId>
    <version>2.50.0.g</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>xyz-Library</artifactId>
<version>2.50.0.g</version>

and the main parent module has the below configuration

<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>xyz-application</artifactId>
<version>2.50.0.g</version>
<packaging>pom</packaging>
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
sandeepkunkunuru
  • 6,150
  • 5
  • 33
  • 37
  • 3
    Your question is misstated and confuses people who have true multi-module ("aggregate") POMs. From your example and from the answers it appears you're really talking about a parent POM, not a multi-module, aggregate POM. See https://maven.apache.org/pom.html#Aggregation . – Garret Wilson Dec 16 '16 at 17:05

12 Answers12

803

Use versions:set from the versions-maven plugin:

mvn versions:set -DnewVersion=2.50.1-SNAPSHOT

It will adjust all pom versions, parent versions and dependency versions in a multi-module project.

If you made a mistake, do

mvn versions:revert

afterwards, or

mvn versions:commit

if you're happy with the results.


Note: this solution assumes that all modules use the aggregate pom as parent pom also, a scenario that was considered standard at the time of this answer. If that is not the case, go for Garret Wilson's answer.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Thanks... tried it in a sample, it works :-)...will try it in my build environment. – sandeepkunkunuru Apr 20 '11 at 08:32
  • 7
    It would have been great if there was a solution that does not require you to actually change each module. The only alternative I can think of is to always use a snapshot version for the parent-pom. – AmanicA Jul 02 '11 at 21:49
  • 71
    Additionally to the `versions:set` one can specify `-DgenerateBackupPoms=false`, as by default this plugin back ups original pom files. – Maksim Sorokin Nov 15 '12 at 08:49
  • 30
    That's the point of the `versions:commit` : "Removes the initial backup of the pom, thereby accepting the changes." – Michael Laffargue Apr 28 '14 at 09:52
  • 2
    A new plugin solves the issue described in this question differently: http://mojo.codehaus.org/flatten-maven-plugin/examples/example-multiple-versions.html – Stephan Jun 12 '14 at 14:51
  • using mvn versions:set on the parent worked for me and applied the version to the reactor modules. In my case one of the module's had version not matching the parent, it was not effected. The others got the new version. – Cris Rockwell Aug 30 '15 at 17:37
  • 1
    @MichaelLaffargue mvn versions:commit seems to remove the backup files generated of the previous pom.xml – Cris Rockwell Aug 30 '15 at 17:39
  • 1
    @ChristopherRockwell is right. It is nothing to do with `commit` in svn, git or whatever. *You still need to commit (and push) to git* – vikingsteve Aug 15 '16 at 12:47
  • @vikingsteve I never said otherwise. commit is a word that can mean a lot of different things, and only a few of those meanings are related to SCM systems like git or SVN – Sean Patrick Floyd Aug 15 '16 at 12:51
  • @SeanPatrickFloyd no worries, I'm just clarifying for the benefit of people who work in a "git centric" world like I do. Thank you so much for your answer, very helpful! – vikingsteve Aug 15 '16 at 13:10
  • @vikingsteve I also work in a "git-centric" world. and btw, the word "commit" applies to all SCMs, not just git. But still: it's an English word, that has meaning beyond SCMs. Failing to realize that will cause an awkward situation if your S.O. ever asks you to make a commitment :-) – Sean Patrick Floyd Aug 15 '16 at 14:33
  • @SeanPatrickFloyd hey relax :) now you're just being pendantic (not adding value to the question nor the answer) – vikingsteve Aug 15 '16 at 18:35
  • This doesn't work in a multi-module (that is aggregate POM) situation. The question was misstated; it seems to have confused a parent POM with an aggregate POM. I'm guessing this will work with a parent POM (or an aggregate POM that is also a parent POM). – Garret Wilson Dec 16 '16 at 17:00
  • @GarretWilson true. There is no sane, working way to set the version for a project hierarchy if the parent pom is not part of that hierarchy. – Sean Patrick Floyd Dec 16 '16 at 17:05
  • 1
    @SeanPatrickFloyd actually, after months of searching, there seems to be a way to update the modules in an aggregation hierarchy that does not use inheritance. See my answer https://stackoverflow.com/a/49246337/421049 below. – Garret Wilson Mar 13 '18 at 00:12
  • @GarretWilson great job, nice to see they are addressing problems. Note: this option is new in plugin version 2.5, which was released 6 months ago, i.e. 6.5 years after my answer :-) – Sean Patrick Floyd Mar 13 '18 at 00:22
  • Hahaha!! OK, no wonder I couldn't find it months ago!! Yeah, it's great to see progress on these things. – Garret Wilson Mar 13 '18 at 02:52
  • 1
    I come to this page before every release i have to make just to copy the command.. – NuttLoose Aug 31 '18 at 14:16
  • @SeanPatrickFloyd It seems to work in a parent-child relation when the child is also declared as a module in the parent pom. but when the child is not declared in the parent then the version update happens only in the parent and not in the child. is there any solution for that? – Daniel Aug 07 '21 at 21:07
  • @Daniel that sounds like it is working as intended. If a child is not declared in the parent, then it isn't part of the reactor, and hence also not part of an execution. Basically: Maven has no way of knowing how many nested sub projects exist unless they are declared as modules. So the only solution I can think of is to have a shell script that calls the version:set command first on the parent and then on all the children. Or just declare the children as modules in a dedicated profile you only use for updating versions. – Sean Patrick Floyd Aug 12 '21 at 22:12
98

The given answer assumes that the project in question use project inheritance in addition to module aggregation. In fact those are distinct concepts:

https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance_vs_Project_Aggregation

Some projects may be an aggregation of modules, yet not have a parent-child relationship between aggregator POM and the aggregated modules. (There may be no parent-child relationship at all, or the child modules may use a separate POM altogether as the "parent".) In these situations the given answer will not work.

After much reading and experimentation, it turns out there is a way to use the Versions Maven Plugin to update not only the aggregator POM but also all aggregated modules as well; it is the processAllModules option. The following command must be done in the directory of the aggregator project:

mvn versions:set -DnewVersion=2.50.1-SNAPSHOT -DprocessAllModules

The Versions Maven Plugin will not only update the versions of all contained modules, it will also update inter-module dependencies!!!! This is a huge win and will save a lot of time and prevent all sorts of problems.

Of course don't forget to commit the changes in all modules, which you can also do with the same switch:

mvn versions:commit -DprocessAllModules

You may decide to dispense with the backup POMS altogether and do everything in one command:

mvn versions:set -DnewVersion=2.50.1-SNAPSHOT -DprocessAllModules -DgenerateBackupPoms=false
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • How do we automate the next version as well just like the build-helper plugin ? – lostintranslation Sep 14 '18 at 06:52
  • Using Maven 3.5.0 I cannot get this to work. I have project aggregation and only the parent pom got updated. I also tried project inheritance (together with aggregation - "all three rules" from the provided link), and again only the parent pom got updated. – SiKing May 17 '19 at 18:24
  • 3
    Found the secret make-it-work switch: the starting version of the parent pom and the modules has to be the same! My parent pom was starting with "1-SNAPSHOT" and the modules had "1.0.0-SNAPSHOT". :) – SiKing May 17 '19 at 19:13
  • 5
    With an aggregator project, the version of the aggregator and versions of the submodules do *not* have to be the same. (E.g. your aggregator pom may only change rarely and can stay at a particular version, while individual submodules can have their own release cycles). The key property to specify to the `versions:set` plugin is `-DoldVersion='*'`, on https://www.mojohaus.org/versions-maven-plugin/set-mojo.html it explicitly says this property should be specified when processing an aggregator project. – Matthew Wise May 23 '19 at 16:08
  • 3
    Under what conditions does `-DprocessAllModules` actually work? It doesn't work for me. – Alex R Jun 23 '19 at 17:59
  • 1
    @Garret , Even you are assuming that aggregator pom and sub module pom version has to be same ! – Number945 Dec 12 '19 at 01:35
  • 1
    Indeed it *only* seems to work if the version in aggregator pom and all submodules are identical, otherwise it doesn't update them. – Kris Sep 09 '21 at 20:41
  • tip - If this command feels risky, simply comment out most child modules from the modules (except may be 1 ) section in reactor / aggregator before running the command and try it out first. Once comfortable this command can save a lot of time. – Gautam Jul 24 '23 at 02:30
50

If you want to fully automate the process (i.e. you want to increment the version number without having to know what the current version number is), you can do this:

mvn build-helper:parse-version versions:set -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion} versions:commit
Malcolm Crum
  • 4,345
  • 4
  • 30
  • 49
31

You may want to look into Maven release plugin's release:update-versions goal. It will update the parent's version as well as all the modules under it.


Update: Please note that the above is the release plugin. If you are not releasing, you may want to use versions:set

mvn versions:set -DnewVersion=1.2.3-SNAPSHOT
Nishant
  • 54,584
  • 13
  • 112
  • 127
  • 1
    mvn version:set doesn't affect modules. – 9ilsdx 9rvj 0lo Sep 12 '17 at 12:52
  • It does, I use it. [Sets the current project's version and based on that change propagates that change onto any child modules as necessary.](http://www.mojohaus.org/versions-maven-plugin/set-mojo.html) – Nishant Sep 13 '17 at 17:53
  • OK it does when the reactor build is in the same time parent. It's confusing when the structure looks otherwise... – 9ilsdx 9rvj 0lo Sep 14 '17 at 07:01
  • 1
    `mvn release:update-versions -DautoVersionSubmodules` worked fine for me, even if I am not releasing :-) – msa Mar 02 '20 at 15:06
11

I encourage you to read the Maven Book about multi-module (reactor) builds.

I meant in particular the following:

<parent>
    <artifactId>xyz-application</artifactId>
    <groupId>com.xyz</groupId>
    <version>2.50.0.g</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>xyz-Library</artifactId>
<version>2.50.0.g</version>

should be changed into. Here take care about the not defined version only in parent part it is defined.

<modelVersion>4.0.0</modelVersion>

<parent>
    <artifactId>xyz-application</artifactId>
    <groupId>com.xyz</groupId>
    <version>2.50.0.g</version>
</parent>
<groupId>com.xyz</groupId>
<artifactId>xyz-Library</artifactId>

This is a better link.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • 11
    and look for what specifically? – Thorbjørn Ravn Andersen Jan 03 '13 at 15:56
  • 2
    +1 for bringing up the proper formatting for `pom.xml` files, but I agree (with @ThorbjørnRavnAndersen) that reading a whole book for this information is overkill. :p – Priidu Neemre Jan 27 '16 at 09:09
  • 12
    Unfortunately inheriting version information from the parent doesn't remove the burden of having to modify **all pom files** in the project - because they all reference the parent by *version* number. – Steven the Easily Amused Aug 05 '16 at 21:41
  • 1
    You could use versions-maven-plugin which handles all this stuff or you can use the maven-release-plugin and so you don't need to handle this manually... – khmarbaise Aug 06 '16 at 09:45
  • It still boggles my mind that people still just drop links here without any regards of the link ever expiring. This renders this answer essentially useless. – TheRealChx101 Jun 28 '21 at 08:39
7

The solution given above worked for us as well for a long time:

mvn versions:set -DnewVersion=2.50.1-SNAPSHOT

However it stopped working yesterday, and we found it due to a recent bug in a the versions-maven-plugin

Our (temporary) workaround was to change the parent/pom.xml file as follows:

--- jackrabbit/oak/trunk/oak-parent/pom.xml 2020/08/13 13:43:11 1880829
+++ jackrabbit/oak/trunk/oak-parent/pom.xml 2020/08/13 15:17:59 1880830
@@ -329,6 +329,13 @@
           <artifactId>spotbugs-maven-plugin</artifactId>
           <version>3.1.11</version>
         </plugin>
+        
+        <plugin>
+          <groupId>org.codehaus.mojo</groupId>
+          <artifactId>versions-maven-plugin</artifactId>
+          <version>2.7</version>
+        </plugin>
+        
Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
4

The best way is, since you intend to bundle your modules together, you can specify <dependencyManagement> tag in outer most pom.xml (parent module) direct under <project> tag. It controls the version and group name. In your individual module, you just need to specify the <artifactId> tag in your pom.xml. It will take the version from parent file.

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
murali
  • 51
  • 1
  • I can't find the tag dependencyManagement on [pom.xml](http://maven.apache.org/pom.html). Are you thinking on [something else](http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management)? – ArturoTena Sep 03 '14 at 17:58
4

To update main pom.xml and parent version on submodules:

mvn versions:set -DnewVersion=1.3.0-SNAPSHOT -N versions:update-child-modules -DgenerateBackupPoms=false
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
4

versions:update-child-modules sounds like what you're looking for. You could do versions:set as mentioned, but this is a light-weight way to update the parent version numbers. For the child modules, it's my opinion that you should remove the <version> definitions, since they will inherit the parent module's version number.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Jon Onstott
  • 13,499
  • 16
  • 80
  • 133
4

Hint:
If your multi-modules maven project uses a project version as a property inside your main POM file for example

<properties>
        <revision>1.0.0</revision> <!-- project version/revision -->
        ...
</properties>

In this case, you can bump your project version by using set-property instead of set:

Example:

mvn versions:set-property -Dproperty=revision -DnewVersion="2.0.0"  

mvn versions:commit
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
0

the easiest way is to change version in every pom.xml to arbitrary version. then check that dependency management to use the correct version of the module used in this module! for example, if u want increase versioning for a tow module project u must do like flowing:

in childe module :

    <parent>
       <artifactId>A-application</artifactId>
       <groupId>com.A</groupId>
       <version>new-version</version>
    </parent>

and in parent module :

<groupId>com.A</groupId>
<artifactId>A-application</artifactId>
<version>new-version</version>
0

I was looking for this:

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>
parsecer
  • 4,758
  • 13
  • 71
  • 140