如何确保Aether'下载'本地仓库中的所有依赖项?

时间:2015-04-23 13:13:12

标签: maven dependencies artifacts aether

我正在尝试解决项目的直接依赖关系,基本上是POM中的运行时和编译时依赖关系以及传递。为此,我有以下代码

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 );
    }
}

}

这可以正常工作,但我如何获得“已解决”的依赖项列表。我需要能够在我的本地仓库中下载这个已解决的工件。基本上我需要一个List来返回,我可以从中找到本地磁盘上的jar。

1 个答案:

答案 0 :(得分:1)

如果您需要jar文件和该jar的物理路径,您应该“解决”它。我通过以下方式实现了它:

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

    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact( artifact );
    artifactRequest.setRepositories( Booter.newRepositories( system, session ) );

    ArtifactResult artifactResult = system.resolveArtifact( session, artifactRequest );

    artifact = artifactResult.getArtifact();
To get the file:
    artifact.getFile();