在 Java 程序中从 Maven 存储库下载工件
Download artifact from Maven repository in Java program
我需要编写 Java 代码来从给定的 Maven 存储库下载给定的工件 (GAV)。过去我使用 Nexus 2 的 REST 接口(这很容易),但我想要一些独立于所用存储库(Nexus 2、Nexus 3、Artifactory)的东西。
在 Fetching Maven artifacts programmatically 中提出了类似的问题,但主要答案来自 2014 年,并建议使用自 2014 年以来未更新的库。
您描述的是 maven-dependency-plugin (get goal)
的特征
因此,在 Maven 插件的上下文中,Java 代码完全符合您的要求。
我只想
- 从github
获取get插件的源代码
- 删除所有 maven 插件内容(@Component 依赖注入、参数)
- 在你需要的地方集成它的要点(并添加对所需内部 maven 库的依赖)
可以使用 Aether(版本 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;
}
}
使用以下工件
<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>
与
<aetherVersion>1.1.0</aetherVersion>
<mavenVersion>3.3.3</mavenVersion>
您可以使用 aether API 下载 Maven 工件,它将下载给定 POM.xml 文件中的 jar
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.DefaultModelBuilderFactory;
import org.apache.maven.model.building.DefaultModelBuildingRequest;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingResult;
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.Proxy;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RemoteRepository.Builder;
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;
import org.eclipse.aether.util.repository.DefaultProxySelector;
public class DownloadingArtifactsByPomExample {
public static final String TARGET_LOCAL_REPOSITORY = "target/local-repository";
public static void main(String[] args) throws Exception {
File projectPomFile = new
File("C:\Users\user\Desktop\TEST_Aether\test\pom.xml");
//Paths.get("","pom.xml").toAbsolutePath().toFile();
System.out.printf("loading this sample project's Maven descriptor from %s\n",
projectPomFile);
System.out.printf("local Maven repository set to %s\n",
Paths.get("", TARGET_LOCAL_REPOSITORY).toAbsolutePath());
RepositorySystem repositorySystem = getRepositorySystem();
RepositorySystemSession repositorySystemSession =
getRepositorySystemSession(repositorySystem);
final DefaultModelBuildingRequest modelBuildingRequest = new
DefaultModelBuildingRequest()
.setPomFile(projectPomFile);
ModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();
ModelBuildingResult modelBuildingResult =
modelBuilder.build(modelBuildingRequest);
Model model = modelBuildingResult.getEffectiveModel();
System.out.printf("Maven model resolved: %s, parsing its dependencies..\n",
model);
for (Dependency d : model.getDependencies()) {
System.out.printf("processing dependency: %s\n", d);
Artifact artifact = new DefaultArtifact(d.getGroupId(), d.getArtifactId(),
d.getType(), d.getVersion());
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
artifactRequest.setRepositories(getRepositories(repositorySystem,
repositorySystemSession));
try {
ArtifactResult artifactResult =
repositorySystem.resolveArtifact(repositorySystemSession,
artifactRequest);
artifact = artifactResult.getArtifact();
// to load the artifact in current classloader
ClassLoader classLoader = new URLClassLoader(new URL[] {
artifact.getFile().toURI().toURL() });
URL u = artifact.getFile().toURI().toURL();
URLClassLoader urlClassLoader = (URLClassLoader)
ClassLoader.getSystemClassLoader();
Class urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod("addURL", new Class[] {
URL.class });
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[] { u });
System.out.printf("artifact %s resolved to %s\n", artifact,
artifact.getFile());
} catch (ArtifactResolutionException e) {
System.err.printf("error resolving artifact: %s\n", e.getMessage());
}
}
}
public static RepositorySystem getRepositorySystem() {
DefaultServiceLocator serviceLocator =
MavenRepositorySystemUtils.newServiceLocator();
serviceLocator.addService(RepositoryConnectorFactory.class,
BasicRepositoryConnectorFactory.class);
serviceLocator.addService(TransporterFactory.class,
FileTransporterFactory.class);
serviceLocator.addService(TransporterFactory.class,
HttpTransporterFactory.class);
serviceLocator.setErrorHandler(new DefaultServiceLocator.ErrorHandler() {
@Override
public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable
exception) {
System.err.printf("error creating service: %s\n",
exception.getMessage());
exception.printStackTrace();
}
});
return serviceLocator.getService(RepositorySystem.class);
}
public static DefaultRepositorySystemSession
getRepositorySystemSession(RepositorySystem system) {
DefaultRepositorySystemSession repositorySystemSession =
MavenRepositorySystemUtils.newSession();
LocalRepository localRepository = new LocalRepository(TARGET_LOCAL_REPOSITORY);
repositorySystemSession
.setLocalRepositoryManager(system.newLocalRepositoryManager(repositorySystemSession,
localRepository));
repositorySystemSession.setRepositoryListener(new
ConsoleRepositoryEventListener());
repositorySystemSession.setProxySelector(new DefaultProxySelector()
.add(new Proxy(Proxy.TYPE_HTTPS, "proxy.tcs.com", 8080),
Arrays.asList("localhost", "127.0.0.1")));
return repositorySystemSession;
}
public static List<RemoteRepository> getRepositories(RepositorySystem system,
RepositorySystemSession session) {
return Arrays.asList(getCentralMavenRepository());
}
private static RemoteRepository getCentralMavenRepository() {
// In "http://central.maven.org we can provide local m2 repository location to
// load the jars into local repo
// like file://C:user/m2/repository
RemoteRepository.Builder builder = new Builder("central", "default", "
http://central.maven.org/maven2/");
// for proxy
builder.setProxy(new Proxy("type", "host", 8080));// port 8080
RemoteRepository central = builder.build();
return central;
}
}
import org.eclipse.aether.AbstractRepositoryListener;
import org.eclipse.aether.RepositoryEvent;
public class ConsoleRepositoryEventListener extends AbstractRepositoryListener {
@Override
public void artifactInstalled(RepositoryEvent event) {
System.out.printf("artifact %s installed to file %s\n, event.getArtifact()",
event.getFile());
}
@Override
public void artifactInstalling(RepositoryEvent event) {
System.out.printf("installing artifact %s to file %s\n, event.getArtifact()",
event.getFile());
}
@Override
public void artifactResolved(RepositoryEvent event) {
System.out.printf("artifact %s resolved from repository %s\n", event.getArtifact(), event.getRepository());
}
@Override
public void artifactDownloading(RepositoryEvent event) {
System.out.printf("downloading artifact %s from repository %s\n", event.getArtifact(), event.getRepository());
}
@Override
public void artifactDownloaded(RepositoryEvent event) {
System.out.printf("downloaded artifact %s from repository %s\n", event.getArtifact(), event.getRepository());
}
@Override
public void artifactResolving(RepositoryEvent event) {
System.out.printf("resolving artifact %s\n", event.getArtifact());
}
}
<properties>
<aetherVersion>1.1.0</aetherVersion>
<mavenVersion>3.2.1</mavenVersion>
</properties>
<dependencies>
<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-util</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>
</dependencies>
我最近发现自己需要从 maven 存储库下载文件。我偶然发现了(在: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 比 Eclipse Aether 项目更容易使用的库。
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());
}
}
我需要编写 Java 代码来从给定的 Maven 存储库下载给定的工件 (GAV)。过去我使用 Nexus 2 的 REST 接口(这很容易),但我想要一些独立于所用存储库(Nexus 2、Nexus 3、Artifactory)的东西。
在 Fetching Maven artifacts programmatically 中提出了类似的问题,但主要答案来自 2014 年,并建议使用自 2014 年以来未更新的库。
您描述的是 maven-dependency-plugin (get goal)
的特征因此,在 Maven 插件的上下文中,Java 代码完全符合您的要求。
我只想
- 从github 获取get插件的源代码
- 删除所有 maven 插件内容(@Component 依赖注入、参数)
- 在你需要的地方集成它的要点(并添加对所需内部 maven 库的依赖)
可以使用 Aether(版本 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;
}
}
使用以下工件
<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>
与
<aetherVersion>1.1.0</aetherVersion>
<mavenVersion>3.3.3</mavenVersion>
您可以使用 aether API 下载 Maven 工件,它将下载给定 POM.xml 文件中的 jar
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.DefaultModelBuilderFactory;
import org.apache.maven.model.building.DefaultModelBuildingRequest;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingResult;
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.Proxy;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RemoteRepository.Builder;
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;
import org.eclipse.aether.util.repository.DefaultProxySelector;
public class DownloadingArtifactsByPomExample {
public static final String TARGET_LOCAL_REPOSITORY = "target/local-repository";
public static void main(String[] args) throws Exception {
File projectPomFile = new
File("C:\Users\user\Desktop\TEST_Aether\test\pom.xml");
//Paths.get("","pom.xml").toAbsolutePath().toFile();
System.out.printf("loading this sample project's Maven descriptor from %s\n",
projectPomFile);
System.out.printf("local Maven repository set to %s\n",
Paths.get("", TARGET_LOCAL_REPOSITORY).toAbsolutePath());
RepositorySystem repositorySystem = getRepositorySystem();
RepositorySystemSession repositorySystemSession =
getRepositorySystemSession(repositorySystem);
final DefaultModelBuildingRequest modelBuildingRequest = new
DefaultModelBuildingRequest()
.setPomFile(projectPomFile);
ModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();
ModelBuildingResult modelBuildingResult =
modelBuilder.build(modelBuildingRequest);
Model model = modelBuildingResult.getEffectiveModel();
System.out.printf("Maven model resolved: %s, parsing its dependencies..\n",
model);
for (Dependency d : model.getDependencies()) {
System.out.printf("processing dependency: %s\n", d);
Artifact artifact = new DefaultArtifact(d.getGroupId(), d.getArtifactId(),
d.getType(), d.getVersion());
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(artifact);
artifactRequest.setRepositories(getRepositories(repositorySystem,
repositorySystemSession));
try {
ArtifactResult artifactResult =
repositorySystem.resolveArtifact(repositorySystemSession,
artifactRequest);
artifact = artifactResult.getArtifact();
// to load the artifact in current classloader
ClassLoader classLoader = new URLClassLoader(new URL[] {
artifact.getFile().toURI().toURL() });
URL u = artifact.getFile().toURI().toURL();
URLClassLoader urlClassLoader = (URLClassLoader)
ClassLoader.getSystemClassLoader();
Class urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod("addURL", new Class[] {
URL.class });
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[] { u });
System.out.printf("artifact %s resolved to %s\n", artifact,
artifact.getFile());
} catch (ArtifactResolutionException e) {
System.err.printf("error resolving artifact: %s\n", e.getMessage());
}
}
}
public static RepositorySystem getRepositorySystem() {
DefaultServiceLocator serviceLocator =
MavenRepositorySystemUtils.newServiceLocator();
serviceLocator.addService(RepositoryConnectorFactory.class,
BasicRepositoryConnectorFactory.class);
serviceLocator.addService(TransporterFactory.class,
FileTransporterFactory.class);
serviceLocator.addService(TransporterFactory.class,
HttpTransporterFactory.class);
serviceLocator.setErrorHandler(new DefaultServiceLocator.ErrorHandler() {
@Override
public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable
exception) {
System.err.printf("error creating service: %s\n",
exception.getMessage());
exception.printStackTrace();
}
});
return serviceLocator.getService(RepositorySystem.class);
}
public static DefaultRepositorySystemSession
getRepositorySystemSession(RepositorySystem system) {
DefaultRepositorySystemSession repositorySystemSession =
MavenRepositorySystemUtils.newSession();
LocalRepository localRepository = new LocalRepository(TARGET_LOCAL_REPOSITORY);
repositorySystemSession
.setLocalRepositoryManager(system.newLocalRepositoryManager(repositorySystemSession,
localRepository));
repositorySystemSession.setRepositoryListener(new
ConsoleRepositoryEventListener());
repositorySystemSession.setProxySelector(new DefaultProxySelector()
.add(new Proxy(Proxy.TYPE_HTTPS, "proxy.tcs.com", 8080),
Arrays.asList("localhost", "127.0.0.1")));
return repositorySystemSession;
}
public static List<RemoteRepository> getRepositories(RepositorySystem system,
RepositorySystemSession session) {
return Arrays.asList(getCentralMavenRepository());
}
private static RemoteRepository getCentralMavenRepository() {
// In "http://central.maven.org we can provide local m2 repository location to
// load the jars into local repo
// like file://C:user/m2/repository
RemoteRepository.Builder builder = new Builder("central", "default", "
http://central.maven.org/maven2/");
// for proxy
builder.setProxy(new Proxy("type", "host", 8080));// port 8080
RemoteRepository central = builder.build();
return central;
}
}
import org.eclipse.aether.AbstractRepositoryListener;
import org.eclipse.aether.RepositoryEvent;
public class ConsoleRepositoryEventListener extends AbstractRepositoryListener {
@Override
public void artifactInstalled(RepositoryEvent event) {
System.out.printf("artifact %s installed to file %s\n, event.getArtifact()",
event.getFile());
}
@Override
public void artifactInstalling(RepositoryEvent event) {
System.out.printf("installing artifact %s to file %s\n, event.getArtifact()",
event.getFile());
}
@Override
public void artifactResolved(RepositoryEvent event) {
System.out.printf("artifact %s resolved from repository %s\n", event.getArtifact(), event.getRepository());
}
@Override
public void artifactDownloading(RepositoryEvent event) {
System.out.printf("downloading artifact %s from repository %s\n", event.getArtifact(), event.getRepository());
}
@Override
public void artifactDownloaded(RepositoryEvent event) {
System.out.printf("downloaded artifact %s from repository %s\n", event.getArtifact(), event.getRepository());
}
@Override
public void artifactResolving(RepositoryEvent event) {
System.out.printf("resolving artifact %s\n", event.getArtifact());
}
}
<properties>
<aetherVersion>1.1.0</aetherVersion>
<mavenVersion>3.2.1</mavenVersion>
</properties>
<dependencies>
<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-util</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>
</dependencies>
我最近发现自己需要从 maven 存储库下载文件。我偶然发现了(在: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 比 Eclipse Aether 项目更容易使用的库。
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());
}
}