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

后端 未结 1 570
滥情空心
滥情空心 2021-02-09 21:06

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

相关标签:
1条回答
  • 2021-02-09 21:55

    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>
    
    0 讨论(0)
提交回复
热议问题