Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically list all transitive dependencies, including overridden ones in Maven using DependencyGraphBuilder?

This is similar to other questions (like this), but I want to be able to do this with the latest API's. The maven-dependency-plugin:tree verbose option has been deprecated and does nothing in the latest (2.5.1) code, so there is no good example of how to do it.

like image 514
Ben Avatar asked Nov 02 '12 17:11

Ben


People also ask

Where can I find Maven transitive dependencies?

You can get this information in the Maven Tool Window. First go to View → Tool Windows → Maven, to make sure that the Maven window is visible. The top-level elements in the tree are your direct dependencies, and the child elements are the transitive dependencies.

How do you find transitive dependency?

If A, B, and C are attributes of relation R, such that A → B, and B → C, then C is transitively dependent on A, unless either B or C is a candidate key or part of a candidate key. Equivalently, a transitive dependency exists when a nonprime attribute determines another nonprime attribute.

Which command is used to find out which POM has transitive dependency missing?

We can list all dependencies including transitive dependencies in the project using mvn dependency:tree command.

How do you find transitive dependencies in Java?

We can use mvn dependency:tree command to see the structure of the dependencies we included in our project. A transitive dependency, simply put, is a dependency that a child dependency depends on. If A depends on B and B depends on C, C is the transitive dependency of A.


1 Answers

I believe Aether utility class from jcabi-aether can help you to get a list of all dependencies of any Maven artifact, for example:

File repo = this.session.getLocalRepository().getBasedir();
Collection<Artifact> deps = new Aether(this.getProject(), repo).resolve(
  new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
  JavaScopes.RUNTIME
);

If you're outside of Maven plugin:

File repo = new File("/tmp/local-repository");
MavenProject project = new MavenProject();
project.setRemoteProjectRepositories(
  Arrays.asList(
    new RemoteRepository(
      "maven-central",
      "default",
      "http://repo1.maven.org/maven2/"
    )
  )
);
Collection<Artifact> deps = new Aether(project, repo).resolve(
  new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
  "runtime"
);

The only dependency you need is:

<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-aether</artifactId>
  <version>0.7.5</version>
</dependency>
like image 57
yegor256 Avatar answered Oct 25 '22 18:10

yegor256