使用java和aether从远程存储库下载工件

时间:2015-10-06 16:11:52

标签: java maven-3 artifactory sonatype

我尝试使用java和aethor库连接到远程存储库,通过代码手动下载文件,即jar / zip / war。但我发现文档不是很有帮助,任何人都有任何想法?

这是我的

public static void main( String[] args ) throws Exception {
    DefaultServiceLocator locator = new DefaultServiceLocator();
    locator.addService( RepositoryConnectorFactory.class, AsyncRepositoryConnectorFactory.class );
    locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
    locator.addService( VersionResolver.class, DefaultVersionResolver.class );
    locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
    locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class );
    locator.setServices( WagonProvider.class, new WagonProvider() {
        public Wagon lookup( String roleHint ) throws Exception {
            if( "http".equals( roleHint ) ) {
                return new LightweightHttpWagon();
            }
            return null;
        }

        public void release( Wagon wagon ) {}
    } );

    RepositorySystem system = locator.getService( RepositorySystem.class );

    MavenRepositorySystemSession session = new MavenRepositorySystemSession();

    LocalRepository localRepo = new LocalRepository( "target/local-repo" );
    session.setLocalRepositoryManager( system.newLocalRepositoryManager( localRepo ) );

    Artifact artifact = new DefaultArtifact( "junit:junit:4.8.2" );

    // RemoteRepository repo = new RemoteRepository("central", "default", "http://repo1.maven.org/maven2/");
    Authentication authentication = new Authentication( "atestuser", "apassword" );
    RemoteRepository repo = new RemoteRepository( ).setUrl( "https://somerepository/repo/" ).setAuthentication( authentication );


    RepositoryConnector connector = new AsyncRepositoryConnectorFactory().newInstance( session, repo );

    ArtifactDownload artDown = new ArtifactDownload( artifact, null, new File("C:\\test\\junit.jar"), null );
    connector.get( Arrays.asList( artDown ), null );

    connector.close();

    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact( artifact );
    artifactRequest.addRepository( repo );

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

    artifact = artifactResult.getArtifact();

    System.out.println( artifact + " resolved to  " + artifact.getFile() );
}   

1 个答案:

答案 0 :(得分:2)

我也使用eclipse以太来下载文物。我发现从eclipse aether开始比旧的sonatype aether更难,但这是我作为开源项目的一部分创建的一个小示例项目:MavenPP

还有一个很好的演示项目,展示了eclipse以太的许多功能:aether-demo

希望这有帮助;)