[英]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.