以编程方式获取Maven工件 [英] Fetching Maven artifacts programmatically
问题描述
我正在寻找可用于从远程存储库检索Maven工件的Java API。到目前为止,我发现了 Eclipse Ather ,但是由于我的需要,我看起来很复杂,所以我正在寻找一些东西更简单。
I'm looking for a Java API which can be used to retreive Maven artifacts from a remote repository. I've found Eclipse Ather so far but it looks over complicated for my needs so i'm seeking for something more simple.
我需要的是:
- 我必须指定远程Maven存储库的位置
- 我喜欢根据它的groupId + artifactId + version 获取工件
- API必须提供当前的远程(请考虑SNAPSHOT工件,这些工件是定期生成的,以便它们在其版本中生成部分)
- 返回工件的位置,优先使用HTTP URL取自己的,例如Apache HTTP客户端)
- 可选地,检索作为所请求的依赖项的工件。
- I have to specify the location of the remote Maven repository
- I like to fetch an artifact based on it's groupId + artifactId + version
- The API have to provide the current remote version of the artifact (think about SNAPSHOT artifacts which are regularly built so they have a generated part in their versions)
- Return the location of the artifact, a HTTP URL is preferred (i'll fetch it on my own with eg. Apache HTTP Client)
- Optionally retreive the artifact's which are the dependants of the requested one.
推荐答案
jcabi-aether 可能会帮助你(我是开发人员)。它是Aether的简单包装,可以让您找到Maven工件的所有传递依赖关系:
jcabi-aether may help you (I'm a developer). It's a simple wrapper around Aether, that lets you find all transitive dependencies of a Maven artifact:
File repo = this.session.getLocalRepository().getBasedir();
Collection<Artifact> deps = new Aether(this.getProject(), repo).resolve(
new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
JavaScopes.RUNTIME
);
因此,您需要提供的所有输入都是:
Thus, all you need to provide as an input is:
- 本地回购位置,作为目录名称
- repote存储库列表(
MavenProject#getRemoteRepositories() code>)
- 工件的Maven坐标
- 要查找的Maven范围
- Local repo location, as a directory name
- List of repote repositories (
MavenProject#getRemoteRepositories()
) - Maven coordinates of the artifact
- Maven scope to look for
找到的每个依赖关系的绝对路径可以获取为 Artifact#getPath()
Absolute paths of every dependency found can be obtained as Artifact#getPath()
这篇关于以编程方式获取Maven工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!