Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java和以太从远程存储库下载工件_Java_Maven 3_Artifactory_Sonatype - Fatal编程技术网

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

使用java和以太从远程存储库下载工件,java,maven-3,artifactory,sonatype,Java,Maven 3,Artifactory,Sonatype,我试图使用java和aethor库连接到远程存储库,通过代码手动下载工件,即jar/zip/war。但是我发现这些文档不是很有用,有人有什么想法吗 这是我的 public static void main( String[] args ) throws Exception { DefaultServiceLocator locator = new DefaultServiceLocator(); locator.addService( RepositoryConnectorFac

我试图使用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() );
}   

我还使用EclipseAether下载工件。我发现从eclipse Ether开始比旧的sonatype Ether要难一些,但这里有一个我作为开源项目的一部分创建的小样本项目:

还有一个很好的演示项目,展示了eclipse Ether的许多功能:


希望这有帮助;)

谢谢,我来看看。从使用HTTPS的存储库下载工件怎么样,