This is a sample code that i took from the aether sample codes to fetch an artifact.
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());
}
}
This code is to fetch the artifact from maven central but it is throwing this error:
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
Please someone suggest what change has to be there. This is a sample code that i took from aether sample codes.
This is a general problem with the latest version (1.13.1) of the org.apache.maven.wagon:wagon-http-lightweight. When used with the latest version of the other aether and maven dependencies it is throwing error.
So don't use the latest version, use the older version. These are the dependencies that i used which worked for me along with the code mentioned in my question.
<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>
and the code to download the artifact from the repository is as mentioned below
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());
}
}