Download artifact from Maven repository in Java program
One can do the following thing with Aether (Version 1.1.0):
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
import org.eclipse.aether.impl.DefaultServiceLocator;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
import org.eclipse.aether.spi.connector.transport.TransporterFactory;
import org.eclipse.aether.transport.file.FileTransporterFactory;
import org.eclipse.aether.transport.http.HttpTransporterFactory;
public class ArtifactDownload
{
private static RepositorySystem newRepositorySystem()
{
DefaultServiceLocator locator = MavenRepositorySystemUtils
.newServiceLocator();
locator.addService(RepositoryConnectorFactory.class,
BasicRepositoryConnectorFactory.class);
locator.addService(TransporterFactory.class, FileTransporterFactory.class);
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
return locator.getService(RepositorySystem.class);
}
private static RepositorySystemSession newSession(RepositorySystem system,
File localRepository)
{
DefaultRepositorySystemSession session = MavenRepositorySystemUtils
.newSession();
LocalRepository localRepo = new LocalRepository(localRepository.toString());
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session,
localRepo));
return session;
}
public static File getArtifactByAether(String groupId, String artifactId,
String version, String classifier, String packaging, File localRepository)
throws IOException
{
RepositorySystem repositorySystem = newRepositorySystem();
RepositorySystemSession session = newSession(repositorySystem,
localRepository);
Artifact artifact = new DefaultArtifact(groupId, artifactId, classifier,
packaging, version);
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
List<RemoteRepository> repositories = new ArrayList<>();
RemoteRepository remoteRepository = new RemoteRepository.Builder("public",
"default", "http://somerepo").build();
repositories.add(remoteRepository);
artifactRequest.setRepositories(repositories);
File result;
try
{
ArtifactResult artifactResult = repositorySystem.resolveArtifact(session,
artifactRequest);
artifact = artifactResult.getArtifact();
if (artifact != null)
{
result = artifact.getFile();
}
else
{
result = null;
}
}
catch (ArtifactResolutionException e)
{
throw new IOException("Artefakt " + groupId + ":" + artifactId + ":"
+ version + " konnte nicht heruntergeladen werden.");
}
return result;
}
}
using the following artifacts
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-spi</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-file</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-http</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>${mavenVersion}</version>
</dependency>
with
<aetherVersion>1.1.0</aetherVersion>
<mavenVersion>3.3.3</mavenVersion>
I recently found myself in the situation to have the requirement to download files from a maven repository. I stumbled over (at: https://www.hascode.com/2017/09/downloading-maven-artifacts-from-a-pom-file-programmatically-with-eclipse-aether/#comment-5602) following https://github.com/shrinkwrap/resolver library which is much easier to use than the Eclipse Aether project.
String artifactoryUrl = "https://your-repo:port/repository";
String[] requiredTypes = new String[] { "zip", "xml" };
for (String requiredType : requiredTypes) {
MavenResolvedArtifact[] result = Maven.configureResolver()
.withMavenCentralRepo(false)
.withRemoteRepo("default", artifactoryUrl, "default")
.useLegacyLocalRepo(true) // You can also set to ignore origin of artifacts present in local repository via useLegacyLocalRepo(true) method.
.resolve(
group + ":" +
id + ":" +
requiredType + ":" +
version
)
.withTransitivity()
.asResolvedArtifact();
for (MavenResolvedArtifact mavenResolvedArtifact : result) {
System.out.println(mavenResolvedArtifact);
System.out.println(mavenResolvedArtifact.asFile().getAbsolutePath());
}
}