以太在运行时从存储库中获取工件

时间:2015-02-17 09:01:43

标签: maven aether

这是我从以太样本代码中获取工件的示例代码。

public class Main2 {
    public static void main(String[] args) throws Exception {
        DefaultServiceLocator locator = new DefaultServiceLocator();
        locator.addService(RepositoryConnectorFactory.class, FileRepositoryConnectorFactory.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("com.hazelcast:hazelcast:LATEST");

        RemoteRepository repo = new RemoteRepository("central", "default", "http://repo1.maven.org/maven2/");

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

此代码用于从maven中心获取工件,但它会抛出此错误:

Exception in thread "main" org.sonatype.aether.resolution.ArtifactResolutionException: Could not transfer artifact com.hazelcast:hazelcast:jar:3.4.1 from/to central (http://repo1.maven.org/maven2/): NullPointerException
    at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:541)
    at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:220)
    at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:197)
    at org.sonatype.aether.impl.internal.DefaultRepositorySystem.resolveArtifact(DefaultRepositorySystem.java:323)
    at maven.test.poc1.Main2.main(Main2.java:69)
Caused by: org.sonatype.aether.transfer.ArtifactTransferException: Could not transfer artifact com.hazelcast:hazelcast:jar:3.4.1 from/to central (http://repo1.maven.org/maven2/): NullPointerException
    at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$4.wrap(WagonRepositoryConnector.java:951)
    at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$4.wrap(WagonRepositoryConnector.java:941)
    at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$GetTask.run(WagonRepositoryConnector.java:669)
    at org.sonatype.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:60)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at org.apache.maven.wagon.providers.http.LightweightHttpWagon.openConnectionInternal(LightweightHttpWagon.java:266)
    at org.apache.maven.wagon.AbstractWagon.openConnection(AbstractWagon.java:105)
    at org.apache.maven.wagon.AbstractWagon.connect(AbstractWagon.java:207)
    at org.sonatype.aether.connector.wagon.WagonRepositoryConnector.connectWagon(WagonRepositoryConnector.java:345)
    at org.sonatype.aether.connector.wagon.WagonRepositoryConnector.pollWagon(WagonRepositoryConnector.java:385)
    at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$GetTask.run(WagonRepositoryConnector.java:571)
    ... 4 more

请有人建议必须进行哪些更改。 这是我从以太样本代码中获取的示例代码。

1 个答案:

答案 0 :(得分:2)

这是org.apache.maven.wagon的最新版本(1.13.1)的一般问题:wagon-http-lightweight。当与最新版本的其他以太和maven依赖关系一起使用时,它会抛出错误。

所以不要使用最新版本,请使用旧版本。这些是我使用的依赖项,以及我在问题中提到的代码。

    <dependency>
        <groupId>org.sonatype.aether</groupId>
        <artifactId>aether-util</artifactId>
        <version>1.13.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-aether-provider</artifactId>
        <version>3.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.sonatype.aether</groupId>
        <artifactId>aether-connector-file</artifactId>
        <version>1.13.1</version>
    </dependency>
    <dependency>
        <groupId>org.sonatype.aether</groupId>
        <artifactId>aether-connector-wagon</artifactId>
        <version>1.13.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-http-lightweight</artifactId>
        <version>1.0</version>
    </dependency>

从存储库下载工件的代码如下所述

public class Main2 {
    public static void main(String[] args) throws Exception {
        DefaultServiceLocator locator = new DefaultServiceLocator();
        locator.addService(RepositoryConnectorFactory.class, FileRepositoryConnectorFactory.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("com.hazelcast:hazelcast:LATEST");

        RemoteRepository repo = new RemoteRepository("central", "default", "http://repo1.maven.org/maven2/");

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