[英]download artifact from remote repository using java and aether
我试图使用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() );
}
我还使用Eclipse Eether来下载工件。 我发现以日食以太币比旧的sonatype以太币要难一点,但这是我作为开源项目的一部分创建的一个小样本项目: MavenPP
还有一个很好的演示项目,显示了Eclipse以太的许多功能: aether-demo
希望这有所帮助;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.