0

This is a version of the question posted in Maven: retrieving the main module version from a sub-module, but I can't figure out how to apply the answer. I'm trying this on Maven 3.6.3. I've posted the repository this can be played around with at this GitHub project

I have a top-level project:

<groupId>com.vps</groupId>
<artifactId>main-module</artifactId>
<version>5.0</version>
<packaging>pom</packaging>

There is another multi-module project that needs to be parented by this top-level project, and it has independent versioning from the top-level project.

The projects are not expected to be in the same repository, and are generally released independently of each other.

I want to declare a property that I can further use to refer to the version of this top-level project parent (say to pull in artifacts with the correct version):

<artifactId>auxiliary</artifactId>
<version>1.0</version>
<parent>
    <groupId>com.vps</groupId>
    <artifactId>main-module</artifactId>
    <version>5.0</version>
</parent>
<properties>
  <main.version>${project.parent.version}</main.version>
</properties>
<modules><module>corelib</module></modules>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.vps</groupId>
      <artifactId>main-module-1</artifactId>
      <version>${main.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>

Now, the sub-module is to pull and use the dependency:

<parent>
  <groupId>com.vps</groupId>
  <artifactId>auxiliary</artifactId>
  <version>1.0</version>
</parent>
<artifactId>corelib</artifactId>
<properties>
<!-- this doesn't work 
  <main.version>${project.parent.parent.version}</main.version>
-->
</properties>
<dependencies>
  <dependency>
     <groupId>com.vps</groupId>
     <artifactId>main-module-1</artifactId>
  </dependency>
</dependencies>

If I set main.version as a reference to ${project.parent.version} in the auxiliary POM, compilation of corelib fails because it tries to pull com.vps:main-module-1:1.0, which doesn't exist. I guess this is because the properties are resolved based on the effective POM being processed at the moment, and from there, the ${project.parent.version} is the version of corelib's parent.

If I override main.version in the corelib sub-module as ${project.parent.parent.version}, I get an error saying that effective version computed for com.vps:main-module-1 is '${project.parent.parent.version}', and is invalid. I guess this means that the property cannot be resolved all together, but I can't quite understand why.

So, how do I reasonably (i.e. without hardcoding the top-level in both the parent definition and another property) refer to that top-level version value from descendant submodules?

Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
  • Does [this](https://stackoverflow.com/a/51969067/424903) answer help you? Modern versions of maven have CI-friendly features, a revision version is one of those. It seems like that is what you are after. – Gimby Feb 19 '21 at 13:35
  • @Gimby I'm sorry, I don't see how this could help me here. Use of `${revision}` is great for avoiding repeating the project versions within modules, but I can't see how it helps accessing arbitrary (and unpredictable) version values up the project ancestry chain. – Pawel Veselov Feb 19 '21 at 13:41
  • Could you please make an example project on github or a like ...that would make it easier to discuss about the things. The first question is: Should all the modules released separately or in one go? Do those modules exist within a single directory structure (same git repository?) ? – khmarbaise Feb 19 '21 at 15:07
  • ... by making a version which is actually not unpredictable. The chosen solution is not going to work because there simply is no property project.parent.parent.version. So the quest is for a built-in maven feature that allows you to do this. – Gimby Feb 19 '21 at 15:12
  • 1
    I was able to get the root POM version with gmaven as described [here](https://stackoverflow.com/a/15962647/944849). I was not trying to use that in dependency management though, so I'm not sure it would work in this situation. Might be worth a shot. – user944849 Feb 19 '21 at 18:24
  • @khmarbaise I've updated the question with these details. – Pawel Veselov Feb 19 '21 at 22:55

0 Answers0