【问题标题】:Aether to fetch the artifact from repository at runtimeAether 在运行时从存储库中获取工件
【发布时间】:2015-02-17 09:01:42
【问题描述】:

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

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 central 获取工件,但它抛出了这个错误:

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

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

【问题讨论】:

  • 您确定 sonatype aether 适用于最新的 Maven 版本吗? Maven 现在使用 eclipse aether...(我建议您进行调试,并检查您的 session 或 artifactRequest 是否为空...)
  • @mylenereiners 你是对的,我用旧版本的 sonatype aether 试过了。
  • @GauravBansal:对于下一个有类似问题的人:请回答您自己的问题并发布有效的依赖项(包括版本)。理想情况下,发布您在 POM 中使用的所有 Maven 依赖项。

标签: maven aether


【解决方案1】:

这是 org.apache.maven.wagon:wagon-http-lightweight 的最新版本 (1.13.1) 的普遍问题。当与其他以太和 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());
    }
}

【讨论】:

  • 我刚刚花了 2 个小时试图让它工作。感谢您的回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
相关资源
最近更新 更多