13

I have a multi-module (aggregator) Maven project that also inherits properties from the parent pom. So in my parent pom.xml I have this:

<groupId>com.something.project</groupId>
<artifactId>MyProject</artifactId>
<packaging>pom</packaging>
<version>1.0.123</version>

<modules>
    <module>ModuleA</module>
    <module>ModuleB</module>
    <module>ModuleC</module>
</modules>

I would like to have all the child modules inherit the parent version number 1.0.123, but the child pom.xml requires me to go ahead and hardcode the version number of the parent anyway (ModuleA pom.xml example):

<artifactId>ModuleA</artifactId>
<name>Some Amazing Thing</name>

<parent>
    <relativePath>../</relativePath>
    <artifactId>MyProject</artifactId>
    <version>1.0.123</version>
    <groupId>com.something.project</groupId>
</parent>

How can I just put the version number in the parent pom, and have the modules inherit it without having to update it in multiple places? So far I've tried ${project.version} and ${project.parent.version} and Maven seems very unhappy with that. Nor can I remove the version reference entirely.

Does anyone have a good answer for this?

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
user3120173
  • 1,758
  • 7
  • 25
  • 39
  • 1
    _aven seems very unhappy with that_ please explain – jmj Jun 25 '14 at 18:33
  • 2
    It's the classic chicken and egg scenario. You want the version numbers to be inherited from the parent, but without specifying which version of the parent you want the version numbers to be inherited from! – Mark Peters Jun 25 '14 at 18:37
  • The specific error I get is this (note the paths have been edited out): [ERROR] Non-resolvable parent POM: Could not transfer artifact com.something.project:MyProject:pom:${project.version} from/to internal (repo path): Illegal character in path at index 91: http://myserver/blah/blah/blah/${project.version}/MyProject-${project.version}.pom and 'parent.relativePath' points at wrong local POM @ line 11, column 13 -> [Help 2] – user3120173 Jun 25 '14 at 18:40
  • @MarkPeters - Understood, but in the case where I'm building the whole project using the parent pom, shouldn't it run FIRST and have that information available? I could understand that if I was going directly to the child pom and saying "run now, with no parent information" but in this case Maven should be accessing the child pom from INSIDE the parent pom, shouldn't it? Seems very strange to me. – user3120173 Jun 25 '14 at 18:48
  • i don't think that's how maven builds work (which is why the child has to reference the parent pom in the first place). each module build is somewhat independent. – jtahlborn Jun 25 '14 at 18:51
  • @jtahlborn - ugh, in that case we may not be able to get there from here, as the saying goes. If that is the case this may simply not be possible. I'll leave this open for a while and see if anybody knows a good hack. I really hate having the same information hard-coded in two places - it's just asking for trouble. – user3120173 Jun 25 '14 at 18:53
  • does the child pom have the version number specified? – jtahlborn Jun 25 '14 at 18:53

2 Answers2

7

In general, if you are using maven to manage the versions, it isn't a problem to put the parent version in the sub-module pom. when you run the maven release plugin, it will update all the versions in all the sub-modules for you.

UPDATE:

if you want to manage versions on your own, then you will probably be in for a bit of pain. i'm pretty sure you will have to specify the version number in each pom at least once. you can probably simplify things for yourself by writing a few scripts which do the recursive search and replace for you (instead of doing it manually).

As a bit of side context: maven is built with a "best(maven) way of doing things" mentality. if you follow that way, things generally tend to be really simple and work really well. doing things differently is often possible, but the further you get from the "best way", the more painful (or impossible) things will be.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • We unfortunately are not using the maven release plugin, we do all our version management manually. I don't know why. :( – user3120173 Jun 25 '14 at 18:44
  • I have to agree with your comment about the "best (maven) way of doing things" - it does seem that as long as you do it maven's way, you're OK but if you step away from that, there's trouble. Thank you for your help. – user3120173 Jun 25 '14 at 19:03
  • use `mvn versions:set` to set a version throughout a hierarchy. Maven has always required a parent version in order to resolve the pom correctly, but I thought it had changed..?? – Niels Bech Nielsen Jun 26 '14 at 11:53
  • re: `maven's way of doing things` unfortunately, some of maven's documentation specifically recommends things that don't actually work. perhaps they used to work, but things evolved away from that style. E.g., the docs say if the parent uses the `...` style, then the sub-modules do not even need to specify the parent section at all. Not true anymore. – Jesse Chisholm May 26 '17 at 00:10
2

I think this is already been answered @ Can you inherit the version from the parent POM in Maven?

I tried the solution, it worked for me (using Apache Maven 3.0.5). But, it has limitation, that if we go directly to child pom and build, it won't resolve the version-variable.

Sridivakar
  • 163
  • 10