How to arrange dependency management for maven reactor if child modules are versioned independently?
Asked Answered
C

2

8

We have a reactor pom whose child modules are versioned independently, ie. child module declares it's own version that is independent of the version of the main pom. There is however a dependency between two child modules. How should be this dependency configured to always use the version that is declared by the actual module of the reactor pom?

I would expect that I can set up dependency management in the root pom and use some implicit properties to determine the version of child modules, but the best such option I can find :

${session.projectDependencyGraph.sortedProjects[0].version} 

or simply

${reactorProjects[0].version}

so that the dependency management in the root pom would look like this:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>mygroup</groupId>
      <artifactId>mySubmodule</artifactid>
      <version>${reactorProjects[0].version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>

looks unreliable because the reactor order may change when new modules or dependencies are added.

Maybe this usage scenario is discouraged by design and then I would like to know why.

Edit: As is suggested in comments, declaring a global properties with child versions in parent POM may be an option for newer versions of Maven. I would however like to see some analysis with respect to wider consequences like workflow, releasing, directory and repository settings, some plugins usage (e.g. version plugin) etc. For example I think that direct consequence of this approach is that I would have to release parent module whenever any of child modules are released. Although this is doable, it would impact the original idea of having an independent versioning.

Choragus answered 15/5, 2015 at 6:42 Comment(3)
Did you try to declare modules' versions as properties defined in the root pom?Dominicadominical
I can do that, the question is if it's the correct way. I'm not sure about current best practices but I think using properties in maven coordinates declaration has been formerly a bad practice or even not possible. If you are talking about declaring properties with manual duplicates of child versions then that's what I'm trying to avoid.Choragus
This answer claims that it wasn't allowed in Maven 2.x. With Maven 3.2.1 here this is still true for project/parent/version but it works for project/version. If it works and fits my needs I'd use it.Anett
C
0

I started a thread with this topic on Maven mailing list, I'm posting TL;DR answer here:

My expected solution with implicit properties is generaly not possible because it would get evaluated not only during build but also when using the built artifact as a dependency in other projects and it's evaluation can have unpredictable results then, because the context of implicit properties is completely different. (I'm stupid, I had the same issue already some time ago with ${project.version} property)

Generaly speaking, the question considers only one scenario in one project from one developer point of view. In the mailing list thread there are other points of view that leads to hardcoding the dependency version numbers either in dependency management or in explicit properties even if something else would be possible.

Choragus answered 7/7, 2015 at 11:38 Comment(0)
C
0

I would suggest using dependencyManagement in the parent pom. You can define all de versions of all dependencies there. You can then omit the version of the dependencies in the different modules

So in the parent:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>mygroup</groupId>
      <artifactId>myModule</artifactid>
      <version>1.2.3</version>
    </dependency>
  </dependencies>
</dependencyManagement>

In the module you then use

  <dependencies>
    <dependency>
      <groupId>mygroup</groupId>
      <artifactId>myModule</artifactid>
    </dependency>
  </dependencies>

The version as defined in dependencyManagement will be used.

I would recommend defining the version of all dependencies like this in the parent pom.

Creosol answered 1/7, 2015 at 10:46 Comment(1)
This is in fact the exact thing I would like to do, I just want to avoid duplicit version definition in dependencyManagement section itself. I've updated my question so that it's more clear.Choragus
C
0

I started a thread with this topic on Maven mailing list, I'm posting TL;DR answer here:

My expected solution with implicit properties is generaly not possible because it would get evaluated not only during build but also when using the built artifact as a dependency in other projects and it's evaluation can have unpredictable results then, because the context of implicit properties is completely different. (I'm stupid, I had the same issue already some time ago with ${project.version} property)

Generaly speaking, the question considers only one scenario in one project from one developer point of view. In the mailing list thread there are other points of view that leads to hardcoding the dependency version numbers either in dependency management or in explicit properties even if something else would be possible.

Choragus answered 7/7, 2015 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.