如何获取工件的依赖树?

发布于 2024-09-11 18:02:13 字数 122 浏览 9 评论 0 原文

dependency:tree 可用于查看给定项目的依赖关系树。但我需要的是查看第三方工件的依赖关系树。

我想我可以创建一个空项目,但我正在寻找更简单的东西(我需要为几个工件执行此操作)。

dependency:tree can be used to see the dependency tree for a given project. But what I need is to see the dependency tree for a 3rd party artifact.

I guess I can create an empty project, but I'm looking for something easier (I need to do this for several artifacts).

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

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

发布评论

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

评论(11

夜光 2024-09-18 18:02:13

1)使用 maven 依赖插件

创建一个简单的项目 pom.xml。添加您的依赖项并运行:(

mvn dependency:tree

多模块 Maven 项目的版本:mvncompile dependency:tree

不幸的是 依赖 mojo 必须使用 pom.xml 否则您会收到以下错误:

无法执行 mojo: 树。它需要一个具有现有 pom.xml 的项目,但构建并未使用该项目。

2) 在 maven 中央存储库中查找工件的 pom.xml

依赖项在工件的 pom.xml 中进行了描述。使用 Maven 基础设施找到它。

前往 https://central.sonatype.com 并输入您的 groupIdartifactId

或者您可以转到 https://repo1.maven.org/maven2/ 并首先使用插件导航groupId,稍后使用artifactId,最后使用其版本。

例如,请参阅 org.springframework:spring-core

3) 针对您的工件使用 Maven 依赖插件

依赖工件的一部分是 pom.xml。这指定了它的依赖性。您可以在此 pom 上执行 mvn dependency:tree -f

1) Use maven dependency plugin

Create a simple project with pom.xml only. Add your dependency and run:

mvn dependency:tree

(Version for multi-module Maven project: mvn compile dependency:tree )

Unfortunately dependency mojo must use pom.xml or you get following error:

Cannot execute mojo: tree. It requires a project with an existing pom.xml, but the build is not using one.

2) Find pom.xml of your artifact in maven central repository

Dependencies are described In pom.xml of your artifact. Find it using maven infrastructure.

Go to https://central.sonatype.com and enter your groupId and artifactId.

Or you can go to https://repo1.maven.org/maven2/ and navigate first using plugins groupId, later using artifactId and finally using its version.

For example see org.springframework:spring-core

3) Use maven dependency plugin against your artifact

Part of dependency artifact is a pom.xml. That specifies it's dependency. And you can execute mvn dependency:tree -f <your-pom-filename> on this pom.

东北女汉子 2024-09-18 18:02:13

如果您费心创建示例项目并向其中添加第 3 方依赖项,那么您可以运行以下命令来查看依赖项的完整层次结构。

您可以使用此 Maven 命令搜索特定的工件

mvn dependency:tree -Dverbose -Dincludes=[groupId]:[artifactId]:[type]:[version]

根据文档:

其中每个模式段都是可选的,并且支持完整和部分 * 通配符。空模式段被视为隐式通配符。

想象一下,您正在尝试在项目的不同模块中查找“log4j-1.2-api”jar 文件:

mvn dependency:tree -Dverbose -Dincludes=org.apache.logging.log4j:log4j-1.2-api

可以找到更多信息 此处

编辑: 详细 标志已在版本 3.2.0
您可以使用以下特定版本:

mvn org.apache.maven.plugins:maven-dependency-plugin:3.2.0:tree -Dverbose=true 

If you bother creating a sample project and adding your 3rd party dependency to that, then you can run the following in order to see the full hierarchy of the dependencies.

You can search for a specific artifact using this maven command:

mvn dependency:tree -Dverbose -Dincludes=[groupId]:[artifactId]:[type]:[version]

According to the documentation:

where each pattern segment is optional and supports full and partial * wildcards. An empty pattern segment is treated as an implicit wildcard.

Imagine you are trying to find 'log4j-1.2-api' jar file among different modules of your project:

mvn dependency:tree -Dverbose -Dincludes=org.apache.logging.log4j:log4j-1.2-api

more information can be found here.

Edit: The verbose flag has been reintroduced in version 3.2.0.
You can use the specific version as below:

mvn org.apache.maven.plugins:maven-dependency-plugin:3.2.0:tree -Dverbose=true 
平安喜乐 2024-09-18 18:02:13

解决方案是使用工件的 pom.xml 文件调用 dependency:tree:

mvn -f "$HOME/.m2/repository/$POM_PATH" dependency:tree

另请参阅 如何列出存储库中工件的传递依赖项?

The solution is to call dependency:tree with the artifact's pom.xml file:

mvn -f "$HOME/.m2/repository/$POM_PATH" dependency:tree

See also How to list the transitive dependencies of an artifact from a repository?

唐婉 2024-09-18 18:02:13

如果使用 IntelliJ 的人想要直接在 IDE 中查看依赖关系树,那么他们可以安装 Maven Helper Plugin 插件。

安装后打开 pom.xml,您将能够看到如下所示的依赖分析选项卡。它还提供了选项来查看仅冲突的依赖关系以及树结构。

输入图片此处描述

If anyone using IntelliJ wants to see dependency tree directly in IDE then they can install Maven Helper Plugin plugin.

Once installed open pom.xml and you would able to see Dependency Analyze tab like below. It also provides option to see dependency that is conflicted only and also as a tree structure.

enter image description here

極樂鬼 2024-09-18 18:02:13

for-artifact 目标“https://github.com/ferstl/depgraph-maven-plugin/” rel="noreferrer">depgraph-maven-plugin 应该做你想做的事。要获得 org.jboss.ws:jbossws-common-tools:1.3.2.Final 的依赖关系图,您只需执行此操作即可

mvn com.github.ferstl:depgraph-maven-plugin:3.3.0:for-artifact -DgroupId=org.jboss.ws -DartifactId=jbossws-common-tools -Dversion=1.3.2.Final -DgraphFormat=text -DshowGroupIds=true -DshowVersions=true

获得,

org.jboss.ws:jbossws-common-tools:1.3.2.Final:compile
+- org.jboss.ws:jbossws-api:1.1.2.Final:compile
+- org.apache.ant:ant:1.7.1:provided
|  \- org.apache.ant:ant-launcher:1.7.1:provided
+- gnu.getopt:java-getopt:1.0.13:provided
+- log4j:log4j:1.2.14:provided
\- junit:junit:3.8.2:test

无需创建 Maven 项目或使用 IDE 或在线应用程序。

The for-artifact goal of the depgraph-maven-plugin should do what you want. To get a dependency graph of org.jboss.ws:jbossws-common-tools:1.3.2.Final you simply do

mvn com.github.ferstl:depgraph-maven-plugin:3.3.0:for-artifact -DgroupId=org.jboss.ws -DartifactId=jbossws-common-tools -Dversion=1.3.2.Final -DgraphFormat=text -DshowGroupIds=true -DshowVersions=true

and you get

org.jboss.ws:jbossws-common-tools:1.3.2.Final:compile
+- org.jboss.ws:jbossws-api:1.1.2.Final:compile
+- org.apache.ant:ant:1.7.1:provided
|  \- org.apache.ant:ant-launcher:1.7.1:provided
+- gnu.getopt:java-getopt:1.0.13:provided
+- log4j:log4j:1.2.14:provided
\- junit:junit:3.8.2:test

without the need to create a Maven project or use and IDE or online app.

苍暮颜 2024-09-18 18:02:13

如果您使用当前版本的 m2eclipse (如果您使用 eclipse 和 maven,则应该这样做):

选择菜单项

导航 ->打开 Maven POM

并输入您要查找的工件。

pom 将在 pom 编辑器中打开,您可以从中选择选项卡 Dependency Hierarchy 来查看依赖关系层次结构(顾名思义:-))

If you use a current version of m2eclipse (which you should if you use eclipse and maven):

Select the menu entry

Navigate -> Open Maven POM

and enter the artifact you are looking for.

The pom will open in the pom editor, from which you can select the tab Dependency Hierarchy to view the dependency hierarchy (as the name suggests :-) )

泛滥成性 2024-09-18 18:02:13

使用 IntelliJ 时,您有一个用于 Maven 依赖项探索的一键工具: https://www.jetbrains.com/help/idea/work-with-maven-dependency.html

结果:
输入图像描述这里

When using IntelliJ, you have a one-click tool for Maven dependency exploration: https://www.jetbrains.com/help/idea/work-with-maven-dependencies.html

Results:
enter image description here

如日中天 2024-09-18 18:02:13

您可以使用 -Dincludes 标志过滤特定工件:

mvn dependency:tree -Dincludes=<artifact>

您可以在此处找到更多信息

You can filter a specific artifact using the -Dincludes flag:

mvn dependency:tree -Dincludes=<artifact>

You can find more information here

奢华的一滴泪 2024-09-18 18:02:13

如果您的工件不是给定项目的依赖项,那么最好的选择是使用 存储库搜索引擎。其中许多描述了给定工件的依赖性。

If your artifact is not a dependency of a given project, your best bet is to use a repository search engine. Many of them describes the dependencies of a given artifact.

川水往事 2024-09-18 18:02:13

如果您使用本地存储库,请使用 -s settings.xml 。以下是对我有用的:

mvn clean compile install -s settings.xml
mvn dependency:tree -s settings.xml

If you are using a local repository use -s settings.xml along with it. Below is what worked for me:

mvn clean compile install -s settings.xml
mvn dependency:tree -s settings.xml
随梦而飞# 2024-09-18 18:02:13

为了快速查看我们正在购买的工件,独立于项目,可以使用以下脚本:

#!/bin/bash
# Idea from: https://stackoverflow.com/a/31097420/2954288
# single argument must be the URL of a pom
pomUrl=$1

dir=$(mktemp --tmpdir -d showMavenTree-XXX)
cd "$dir"
wget "$pomUrl" -O pom.xml

## more fancy things can be added by reading
## https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html
mvn dependency:tree -DoutputFile=pom.dot -DoutputType=dot -Dscope=compile

dot -Tsvg -Grankdir=LR <pom.dot >pom.svg
xdg-open pom.svg

To just quickly see for an artifact what we are buying into, independent of a project, this script can be used:

#!/bin/bash
# Idea from: https://stackoverflow.com/a/31097420/2954288
# single argument must be the URL of a pom
pomUrl=$1

dir=$(mktemp --tmpdir -d showMavenTree-XXX)
cd "$dir"
wget "$pomUrl" -O pom.xml

## more fancy things can be added by reading
## https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html
mvn dependency:tree -DoutputFile=pom.dot -DoutputType=dot -Dscope=compile

dot -Tsvg -Grankdir=LR <pom.dot >pom.svg
xdg-open pom.svg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文