6

我正在编写一个应用程序,该应用程序需要我下载一个 jar 给定的 maven groupid/artifactid/version。

我目前从

public Model pomToModel(String pomUrl) throws Exception {
URL url = new URL(pomUrl);
    InputStream stream = url.openStream();
    try {
        Model model = new MavenXpp3Reader().read(stream);
        return model;
    } finally {
        stream.close();
    }
}

所以给定了 POM 的 url,我现在有了代表它的 maven 模型对象。这似乎运作良好。

我想知道的是两件事:

  1. 在 maven 插件将使用的 maven 存储库中找到模型表示的 jar 工件的 url
  2. 给定模型,在本地存储库中找到代表 jar 的文件

我已经有了质量较差的解决方案:我去 mvnrepository.com 手动编码 url,并使用 ~/.m2/repository 作为文件的前缀。我显然可以改进这个解决方案,但我最终会很差地复制经过良好测试并在 maven 代码库中使用良好的代码。

我真的很想知道如何使用 Maven 类来做到这一点。

我已经做了一些谷歌搜索,但是关于 maven 和 jars(甚至在 stackoverflow 上)的任何搜索的有趣之处在于,我找到了很多关于如何使用 maven 的解决方案,而不是如何使用 maven 类进行编码。

谢谢您的帮助

4

2 回答 2

7

Option 1

Aether as recommended by khmarbaise.

It's the native library that ships with Maven 3.0

Option 2

If you want a simple solution for downloading Maven modules from the command line or within a shell script, Apache ivy has a standalone jar.

Example:

using IVY dependencies manager programmatically

Option 3

If you're just looking for a Maven jar and don't care about dependency management it's even simpler.

Here's how to retrieve the log4j jar direct from Maven Central:

curl -O http://search.maven.org/remotecontent?filepath=log4j/log4j/1.2.17/log4j-1.2.17.jar

If you're using a Nexus repository manager it has a useful REST API

Using the Nexus rest API to get latest artifact version for given groupid/artifactId

于 2012-06-05T18:41:12.463 回答
2

要从 Maven 存储库下载工件,它打算使用Aether lib

另见http://www.sonatype.com/people/category/aether/

此外,还有如何做事的示例和 Aether lib 的完整源代码。很久以前,我玩过Aether。

于 2012-06-05T12:40:40.970 回答