简体   繁体   English

以太,如何获得与层次结构的依赖关系

[英]Aether, how to get dependencies with hierarchy

I'm using eather to re-create the same structure of maven dependency:tree within my code. 我正在使用eather重新创建相同的maven依赖结构:我的代码中的树。 Following the documentation I found this useful example which correctly list the same depdendencys as the plugin but it lacks any "tree" information, it's just a flat list. 根据文档,我发现这个有用的例子正确地列出了与插件相同的depdendencys,但它没有任何“树”信息,它只是一个平面列表。

 public static void main( String[] args )
    throws Exception
{
    System.out.println( "------------------------------------------------------------" );
    System.out.println( ResolveTransitiveDependencies.class.getSimpleName() );

    RepositorySystem system = Booter.newRepositorySystem();

    RepositorySystemSession session = Booter.newRepositorySystemSession( system );

    Artifact artifact = new DefaultArtifact( "org.eclipse.aether:aether-impl:1.0.0.v20140518" );

    DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter( JavaScopes.COMPILE );

    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot( new Dependency( artifact, JavaScopes.COMPILE ) );
    collectRequest.setRepositories( Booter.newRepositories( system, session ) );

    DependencyRequest dependencyRequest = new DependencyRequest( collectRequest, classpathFlter );

    List<ArtifactResult> artifactResults =
        system.resolveDependencies( session, dependencyRequest ).getArtifactResults();

    for ( ArtifactResult artifactResult : artifactResults )
    {
        System.out.println( artifactResult.getArtifact() + " resolved to " + artifactResult.getArtifact().getFile() );
    }
}

I then wrote another piece of code to retrieve non-transitive dependencies and, invoking it recurively, was able to get a full dependency graph, but without any limitation, so I should implement some filtering/limit routine. 然后我写了另一段代码来检索非传递依赖项,并且反复调用它,能够获得完整的依赖图,但没有任何限制,所以我应该实现一些过滤/限制例程。

Since I'd like to keep as much as possible the same plugin logic, without introducing "my" filtering, is there any way to adapt the fist example in order to retrieve hierarchy information as well? 既然我想尽可能多地保留相同的插件逻辑,而不引入“我的”过滤,那么有没有办法调整第一个例子以便检索层次结构信息呢?

If you want exactly the same behavior, as plugin implements, I suggest to look into the plugin's source code here , so you will not miss any nuances. 如果你想要exactly the same行为,就像插件实现的那样,我建议你在这里查看插件的源代码,这样你就不会错过任何细微差别。 But the basic approach is to use collectRequest to collectDependencies , like this: 但基本方法是使用collectRequestcollectDependencies ,如下所示:

public static void main(String[] args) throws Exception {
    System.out.println("------------------------------------------------------------");

    RepositorySystem system = Booter.newRepositorySystem();

    RepositorySystemSession session = Booter.newRepositorySystemSession(system);

    Artifact artifact = new DefaultArtifact("org.apache.maven.plugins:maven-shade-plugin:2.3");

    DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE);

    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE));
    collectRequest.setRepositories(Booter.newRepositories(system, session));

    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFlter);

    List<ArtifactResult> artifactResults =
            system.resolveDependencies(session, dependencyRequest).getArtifactResults();

//        for (ArtifactResult artifactResult : artifactResults) {
//            System.out.println(artifactResult.getArtifact() + " resolved to " + artifactResult.getArtifact().getFile());
//        }

    //use collectDependencies to collect
    CollectResult collectResult = system.collectDependencies(session, collectRequest);
    DependencyNode node = collectResult.getRoot();
    node.accept(new TreeDependencyVisitor(new DependencyVisitor() {
        String indent = "";
        @Override
        public boolean visitEnter(DependencyNode dependencyNode) {
            System.out.println(indent + dependencyNode.getArtifact());
            indent += "    ";
            return true;
        }

        @Override
        public boolean visitLeave(DependencyNode dependencyNode) {
            indent = indent.substring(0, indent.length() - 4);
            return true;
        }
    }));

}

[EDIT:] To include test dependencies, you will need to customize session's depSelector, like this: [编辑:]要包含测试依赖项,您需要自定义会话的depSelector,如下所示:

    DependencySelector depFilter =
            new AndDependencySelector(
                    new ScopeDependencySelector( "provided" ),
                    new OptionalDependencySelector(),
                    new ExclusionDependencySelector()
            );

And add classpathFilter to traversing: 并将classpathFilter添加到遍历:

DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter(JavaScopes.TEST);

node.accept(new TreeDependencyVisitor(new FilteringDependencyVisitor(new DependencyVisitor() {
    ...
}, classpathFlter)));

Still, this will not be exactly as dependency:tree , because it will actually list all the jars, that will go to test classpath. 但是,这不会完全dependency:tree ,因为它实际上会列出所有的jar,它们将用于测试类路径。 But with this you can actually further customize NodeVisitor to filter whatever you want. 但有了这个,您实际上可以进一步自定义NodeVisitor来过滤您想要的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM