简体   繁体   English

使用Java和Aether从远程存储库下载工件

[英]download artifact from remote repository using java and aether

Im trying to connect to a remote repository using java and aethor library, to download artifact ie jar/zip/war manually through code. 我试图使用java和aethor库连接到远程存储库,以通过代码手动下载工件,例如jar / zip / war。 But im finding the documentation not very helpful, anybody got any ideas? 但是我发现文档不是很有帮助,有人有想法吗?

here is what i have 这是我所拥有的

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

I'm also using eclipse aether to download artifacts. 我还使用Eclipse Eether来下载工件。 I find starting with eclipse aether is a bit harder than the older sonatype aether but here is a small sample project I have created as part of an open-source project: MavenPP 我发现以日食以太币比旧的sonatype以太币要难一点,但这是我作为开源项目的一部分创建的一个小样本项目: MavenPP

There is also a nice demo project which shows many features of eclipse aether: aether-demo 还有一个很好的演示项目,显示了Eclipse以太的许多功能: aether-demo

Hope this helped ;) 希望这有所帮助;)

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

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