Эфир, чтобы получить артефакт из хранилища во время выполнения
Это пример кода, который я взял из примеров кодов эфира, чтобы получить артефакт.
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
Пожалуйста, кто-нибудь подскажет, какие изменения должны быть там. Это пример кода, который я взял из примера кода эфира.
1 ответ
Решение
Это общая проблема с последней версией (1.13.1) org.apache.maven.wagon: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());
}
}