2

I'm facing a situation that I need to specify one of transitive dependency's version.

With following dependency tree,

A <- B <- C

I need to specify A's version in C's pom.xml

Is there any way to do this? So that I can filter some file like this?

A's version is @{...a.version}
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

3 Answers3

2

If you want to specify the version of a (possible) transitive dependency, put the dependency into the dependencyManagement section of your POM. Then it is used if the dependency occurs transitively (and silently ignored if no such transitive dependency is found).

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
1

Use <optional>true</optional>:

C -> B -> A

pom(B) :

<dependencies>
    <dependency>
      <groupId></groupId>
      <artifactId>A</artifactId>
      <version></version>
      <optional>true</optional>
    </dependency>
    ...
</dependencies>

pom(C):

<dependencies>
    <dependency>
      <groupId></groupId>
      <artifactId>B</artifactId>
      <version></version> 
    </dependency>
    <dependency>
      <groupId></groupId>
      <artifactId>A</artifactId>
      <version></version> 
    </dependency>
    ...
</dependencies>
halfer
  • 19,824
  • 17
  • 99
  • 186
question_maven_com
  • 2,457
  • 16
  • 21
1

It is not possible to directly reference the version of some arbitrary dependency (transient or not).

However, in your parent pom you can define a property:

<properties>
    ...
    <yourCdep.version>
    ...
</properties>

and add the dependency in to a dependencyManagement section:

<dependencyManagement>
    <dependencies>
        ...
        <dependency>
            <groupId>yourCdep.group</groupId>
            <artifactId>yourCdep</artifactId>
            <version>${yourCdep.version}</version>
        </dependency>
        ...
    </dependencies>
</dependencyManagement>

Remove the version from the dependency in module B as it is now "managed".

The property value in the parent pom will be accessible in both modules A and B.

In particular, this property value can now be applied when resource filtering.

Steve C
  • 18,876
  • 5
  • 34
  • 37