2

I have a Maven project with 2 modules. I want the modules to inherit the version of that project, without defining it as a parent in the POM file of any submodules (the reason behind that is that the modules already have parents). What would be the best way to achieve that?

Importing the version from a properties file doesn't work because maven expects a constant value as a project version, not an expression. Maven plugins such as the version maven plugin or the maven release plugin are not solutions to my problem because I need something that would work in an IDE (I have to use Eclipse for packaging the projects, not my call).

Edit

To clarify things (apologies if my original post was not clear enough)

Main Project POM file

...
<groupId>org.mygroup</groupId>
<artifactId>parentproject</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
    <module>module1</module>
    <module>module2</module>
</modules>
...

Module POM file example

...
<groupId>org.mygroup</groupId>
<artifactId>module1</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
 <!-- Some parent that's NOT the main project, e.g. Spring Boot -->
</parent>
...

What I want is a solution that would allow me to set the version only ONCE (e.g. in the main project POM file) and having every module of that project to "inherit" that version.

GeorgeG
  • 362
  • 2
  • 16
  • 3
    It's a bit unclear what you're asking. If I got things right, you have 2 modules, implying that they are already part of a multi-module Maven project with a parent. And now you would want those 2 modules to inherit from a second parent? Can you post some sample POM of your config? – Tunaki Sep 10 '16 at 22:24
  • you can define a new maven project of type `pom` and have a dependency on it – sidgate Sep 10 '16 at 22:27
  • Added sample POM to my post – GeorgeG Sep 10 '16 at 22:40
  • As parent, you'll have to use your `parentproject`. You cannot have 2 parents, for Spring Boot, you do not need to use their parent POM, see http://stackoverflow.com/questions/20731158/maven-module-using-spring-boot – Tunaki Sep 10 '16 at 23:00
  • Yes, I know that I cannot have two parents and I have already read the responses to that question (Spring Boot was just an example as I stated in the sample above, there could be another project that is used as a parent, plus I'd like to use both the dependency and plugin management that the parent project offers without adding them "manually" in the POM file). I want to achieve version "inheritance", without actually inheriting from the `parentproject`. I realise that what I am asking for might not actually exist but I figured it's worth asking because I am not too familiar with Maven. – GeorgeG Sep 10 '16 at 23:15

2 Answers2

0

I really can't think of any Maven facility that fit 100% your necessities. Event if you could set the parentproject as an actual parent on each submodule, you'd need to specify its version in the parent declaration, so...

But I think of a trick to do the job through an authomatism, so that every time the parent version is changed, it will be automatically propagated to each submodule. It can be done like this:

Program a plugin in the parent project that writes the version id on each of its modules' pom files (for example, through an XSL transformation with the xml-maven-plugin).

Then, link this plugin to the package phase, so that every time the parent is build, the versions gets propagated to the submodules.

You only will have to refresh the submodules projects in Eclipse to make Eclipse be aware of the changes.

But if you don't want to refresh manually, there is still another alternative - fully based on Eclipse:

Make an Ant script to perform the copy-version-to-all-module-poms task. And, instead of calling it from a Maven phase, program an Eclipse builder to call it and, within this builder, program also a refresh of the specific modules. So, every time you execute a build of your project, it will copy its version to the submodules and make Eclipse be aware of this change.

Little Santi
  • 8,563
  • 2
  • 18
  • 46
0

You should probably set the parent back to your actual parent project. Version numbers between modules can be simultanuously updated using mvn versions:set. If you need the version number for cross-module dependencies, use ${project.version}. If you want to embed another Maven configuration file for its dependencies, consider using a Bill-of-material (BOM): https://stackoverflow.com/a/14876651 . Hope things brings you to your answer!

Community
  • 1
  • 1