簡體   English   中英

如何獲取 Maven 項目的直接(非傳遞)依賴項?

[英]How do I get the direct (non transitive) dependencies of a Maven project?

目前,在處理MavenProject時,我正在使用getDependencyArtifacts來獲取直接(非傳遞)依賴項。 但是,此方法已標記為已棄用。 那么,我應該用什么來代替呢? 我看到有方法getDependencies ,但它獲取傳遞依賴項,而且我看不到任何過濾直接依賴項的方法。

我是否應該使用RepositorySystem及其方法collectDependencies並導航結果圖,並僅獲取根的子項,我猜這實際上是直接依賴項? 難道我沒有更直接、未棄用的方法嗎?

這是基於快速搜索,有一個名為 Aether 的現有 API。 它的文檔不是很好。

其中的方法稱為: getDependencies()並根據 javadoc 檢索直接依賴項。

https://maven.apache.org/resolver/maven-resolver-api/apidocs/org/eclipse/aether/resolution/ArtifactDescriptorResult.html

github 上有一個例子:

public class GetDirectDependencies
{

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

        RepositorySystem system = Booter.newRepositorySystem();

        RepositorySystemSession session = Booter.newRepositorySystemSession( system );

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

        ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
        descriptorRequest.setArtifact( artifact );
        descriptorRequest.setRepositories( Booter.newRepositories( system, session ) );

        ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor( session, descriptorRequest );

        for ( Dependency dependency : descriptorResult.getDependencies() )
        {
            System.out.println( dependency );
        }
    }

}

https://github.com/1984shekhar/fuse-examples-6.2/blob/master/aether-demo-snippets/src/main/java/org/eclipse/aether/examples/GetDirectDependencies.java

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM