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

后端 未结 1 571
滥情空心
滥情空心 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 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 deps = new Aether(project, repo).resolve(
      new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
      "runtime"
    );
    

    The only dependency you need is:

    
      com.jcabi
      jcabi-aether
      0.7.5
    
    

    0 讨论(0)
提交回复
热议问题