0

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.

Sajith Eshan
  • 696
  • 4
  • 17

1 Answers1

1

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

jgitter
  • 3,396
  • 1
  • 19
  • 26
  • I actually tried this but with mvn dependency:resolve -DincludeTransitive=false -outputFile=out.txt. But is there a way to avoid reading a intermediate file and directly resolve the versions from the Java code it self – Sajith Eshan Feb 11 '14 at 04:30
  • I don't know what tools you're using, but in the past I've also used the maven plugin for eclipse. If you open up a pom.xml, you can see the fully resolved version, including all of the dependencies that it inherited. There are instructions for installing it here: http://theopentutorials.com/tutorials/eclipse/installing-m2eclipse-maven-plugin-for-eclipse/ You can download it from here if you'd rather drop it in yourself: https://eclipse.org/m2e/download/ – jgitter Feb 11 '14 at 15:02