Sajith Eshan
Sajith Eshan

Reputation: 696

How to find the exact version of a maven dependency programmatically when version is inherited?

I have a maven project where many of the version of child pom's dependencies are listed under <dependencyManagement> tag of the parent pom. This pom tree can have any depth.

My problem is, when a pom.xml is given how can I resolve the exact(inherited) version of it's dependencies using java?

The desired output is what is given when we run mvn dependency:resolve -DincludeTransitive=false on a pom.xml file

For an example, let's say parent pom has following dependencies defined in it under <dependencyManagement>

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-a</artifactId>
        <version>1.0</version>
      </dependency>
      <dependency>
        <groupId>group-a</groupId>
        <artifactId>artifact-b</artifactId>
        <version>1.0</version>
        <type>bar</type>
        <scope>runtime</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

and child pom.xml has following dependencies

<dependencies>
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-a</artifactId>
    </dependency>
    <dependency>
      <groupId>group-a</groupId>
      <artifactId>artifact-b</artifactId>
    </dependency>
  </dependencies>

How can i retrieve the versions of group-a:artifact-a and group-b:artifact-b using Java programmatically?

Thanks.

Upvotes: 0

Views: 790

Answers (1)

jgitter
jgitter

Reputation: 3414

Try running

mvn dependency:tree -Doutput=/path/to/file

or

mvn dependency:tree -DoutputFile=/path/to/file

From maven docs: http://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html

Upvotes: 1

Related Questions