Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Maven child poms have same version number as parent? [duplicate]

Tags:

java

maven

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?

like image 855
user3120173 Avatar asked Jun 25 '14 18:06

user3120173


1 Answers

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.

like image 142
jtahlborn Avatar answered Nov 10 '22 18:11

jtahlborn