如何从 CLI(不在项目目录中)下载 Maven 工件及其依赖项的 Java 源代码?

发布于 2025-01-08 14:14:19 字数 612 浏览 4 评论 0原文

注意:由于下面指定的原因,maven-dependency-plugin 不适合这里

我正在将项目部署到 Artifactory 并附加源。我希望能够运行命令来下载和解压给定工件及其依赖项的源代码。我将使用它来区分工件的两个版本。

我想做的基本上是这样的:

mvn extract:sources -DgroupId=[groupId] -DartifactId=[artifactId] -Dversion=[version]

尝试将 maven-dependency-plugin 中的几个目标结合起来,但这似乎无法满足我的需要:

  • :unpack-dependency 需要项目
  • :get 要求我显式指定远程存储库。为什么它不能使用我的settings.xml 中的那些?

我尝试编写自己的 mojo 来执行此操作,但很困惑,因为除非我位于项目目录中,否则我似乎无法处理远程存储库。因此我无法下载该项目。即使我下载了该项目,mojo 也已经初始化了它的 ${project},因此我将无法获取它的依赖项等,

非常感谢您的帮助。

Note: maven-dependency-plugin isn't suitable here for reasons specified below

I'm deploying projects to Artifactory with sources attached. I'd like to be able to run a command to download and unpack sources for a given artifact and its dependencies. I'll be using this to diff two versions of an artifact.

What I'd like to do is basically this:

mvn extract:sources -DgroupId=[groupId] -DartifactId=[artifactId] -Dversion=[version]

Have tried combining a couple of goals from the maven-dependency-plugin but this doesn't seem capable of doing what I need:

  • :unpack-dependencies requires a project
  • :get requires me to explicitly specify a remote repo. Why can't it use those in my settings.xml?

I've tried writing my own mojo to do this but am flummoxed because I can't seem to get a handle on remote repositories unless I'm in a project directory. Thus I can't download the project. And even once I have downloaded the project, the mojo will have already initialised its ${project} hence I won't be able to get its dependencies etc

Would appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痕至 2025-01-15 14:14:19

apache ivy jar 可以作为 CLI 程序 来下载Maven 工件。

以下示例从 Maven Central 下载 ivy,然后使用它下载 commons-lang 源 jar:

wget -O ivy.jar \
     http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.2.0/ivy-2.2.0.jar

java -jar ivy.jar \
     -dependency commons-lang commons-lang 2.6 \
     -confs sources \
     -retrieve "[artifact](-[classifier]).[ext]"

The apache ivy jar can be used as a CLI program to download Maven artifacts.

The following example downloads ivy from Maven Central, then uses it to download the commons-lang source jar:

wget -O ivy.jar \
     http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.2.0/ivy-2.2.0.jar

java -jar ivy.jar \
     -dependency commons-lang commons-lang 2.6 \
     -confs sources \
     -retrieve "[artifact](-[classifier]).[ext]"
抽个烟儿 2025-01-15 14:14:19

我发现最好的方法是使用 3 个 Maven 命令:

  • 一个获取你的 artefact,
  • 一个获取你的 artefact pom
  • ,另一个使用它的 pom 获取它的依赖项

这是一个 bash 片段:

#!/bin/bash

DIR=some/dir
ARTEFACT_ID=your-artefact
GROUP_ID=com.your.group.id
VERSION=1.7
ARTEFACT=${GROUP_ID}:${ARTEFACT_ID}:${VERSION}

rm -fR $DIR

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy \
  -Dartifact=$ARTEFACT \
  -DoutputDirectory=$DIR

# now get the pom: it will be needed by the copy-dependencies goal
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy \
  -Dartifact=$ARTEFACT:pom \
  -DoutputDirectory=$DIR

mvn -f ${DIR}/${ARTEFACT_ID}-${VERSION}.pom org.apache.maven.plugins:maven-dependency- plugin:2.8:copy-dependencies \
  -DoutputDirectory=.

The best I found was to use 3 maven commands:

  • one that get your artefact
  • one that get your artefact pom
  • and one that fetch its dependencies using its pom

Here is a bash snippet:

#!/bin/bash

DIR=some/dir
ARTEFACT_ID=your-artefact
GROUP_ID=com.your.group.id
VERSION=1.7
ARTEFACT=${GROUP_ID}:${ARTEFACT_ID}:${VERSION}

rm -fR $DIR

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy \
  -Dartifact=$ARTEFACT \
  -DoutputDirectory=$DIR

# now get the pom: it will be needed by the copy-dependencies goal
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy \
  -Dartifact=$ARTEFACT:pom \
  -DoutputDirectory=$DIR

mvn -f ${DIR}/${ARTEFACT_ID}-${VERSION}.pom org.apache.maven.plugins:maven-dependency- plugin:2.8:copy-dependencies \
  -DoutputDirectory=.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文