如何以编程方式使用 maven 下载 jar [英] How do I download a jar using maven programatically

查看:80
本文介绍了如何以编程方式使用 maven 下载 jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,需要我根据 mavens groupid/artifactid/version 下载一个 jar.

I am writing an application that requires me to download a jar given the mavens 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 模型对象.这似乎运作良好.

So given the url of the POM, I now have the maven Model object representing it. This seems to work well.

我想知道的是两件事:

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

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

I already have poor quality solutions: I go to mvnrepository.com manually coding the url, and I use ~/.m2/repository as the prefix for the file. I can obviously improve this solution, but I will end up poorly duplicating the code that is well tested and well used in the maven code base.

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

I'd really like to know how to do this using the maven classes.

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

I have done a fair bit of google searching, but the amusing thing about searching for anything about maven and jars (even on stackoverflow) is that I find lots of solutions about how to use maven, rather than how to code using the maven classes.

感谢您的帮助

推荐答案

Option 1

khmarbaise 推荐的以太.

Option 1

Aether as recommended by khmarbaise.

它是 Maven 3.0 附带的原生库

It's the native library that ships with Maven 3.0

如果您想要一个从命令行或 shell 脚本下载 Maven 模块的简单解决方案,Apache ivy 有一个 独立 jar.

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.

示例:

以编程方式使用 IVY 依赖项管理器

如果您只是在寻找 Maven jar 并且不关心依赖项管理,那么它甚至更简单.

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

以下是直接从 Maven Central 检索 log4j jar 的方法:

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

如果您使用 Nexus 存储库管理器,它有一个有用的 REST API

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

使用 Nexus rest API 获取给定 groupid/artifactId 的最新工件版本

这篇关于如何以编程方式使用 maven 下载 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆