有没有办法向 Maven 输入一个 jar 名称并获取将其添加到我的项目的 jar 的完整路径?
最佳答案
你能做的最好的事情就是使用 mvn dependency:tree
command .
它不显示拉取依赖项的 jar 的完整路径。
相反,它显示当前 Maven 项目的依赖关系树。
对于每个已解决的依赖项,您都可以知道拉取该依赖项的 Maven 模块/依赖项。
使由三重奏 groupId-artifactId-version
标识的依赖项与本地存储库之间的映射应该非常简单。
这是一个项目的例子,其中有 jmh
作为依赖:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ map-benchmark --- [INFO] davidxxx:map-benchmark:jar:1.0 [INFO] +- junit:junit:jar:4.7:test [INFO] +- org.openjdk.jmh:jmh-core:jar:1.19:compile [INFO] | +- net.sf.jopt-simple:jopt-simple:jar:4.6:compile [INFO] | \- org.apache.commons:commons-math3:jar:3.2:compile [INFO] \- org.openjdk.jmh:jmh-generator-annprocess:jar:1.19:compile
You can see for example that junit
is not a transitive dependency as it pulled by the current project itself.
But you could also see that commons-math3
is a transitive dependency pulled by jopt-simple
itself pulled by jmh-core
.
The dependency:tree
goal can also be used to filter only specific dependencies.
mvn dependency:tree -Dincludes=org.apache.commons:commons-math3
或(注意 :
如果我们不需要指定 groupId,则不带前缀):
mvn dependency:tree -Dincludes=:commons-math3
将输出:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ map-benchmark --- [INFO] davidxxx:map-benchmark:jar:1.0 [INFO] \- org.openjdk.jmh:jmh-core:jar:1.19:compile [INFO] \- org.apache.commons:commons-math3:jar:3.2:compile [INFO] ------------------------------------------------------------------------
This plugin can help to solve conflicts.
Here is a relevant example from the documentation.
For example, to find out why Commons Collections 2.0 is being used by the Maven Dependency Plugin, we can execute the following in the project's directory:
mvn dependency:tree -Dverbose -Dincludes=commons-collections
详细标志指示依赖树显示冲突 从已解析的依赖关系树中省略的依赖关系。在 在这种情况下,目标输出:
[INFO] [dependency:tree] [INFO] org.apache.maven.plugins:maven-dependency-plugin:maven-plugin:2.0-alpha-5-SNAPSHOT [INFO] +- org.apache.maven.reporting:maven-reporting-impl:jar:2.0.4:compile [INFO] | \- commons-validator:commons-validator:jar:1.2.0:compile [INFO] | \- commons-digester:commons-digester:jar:1.6:compile [INFO] | \- (commons-collections:commons-collections:jar:2.1:compile - omitted for conflict with 2.0) [INFO] \- org.apache.maven.doxia:doxia-site-renderer:jar:1.0-alpha-8:compile [INFO] \- org.codehaus.plexus:plexus-velocity:jar:1.1.3:compile [INFO] \- commons-collections:commons-collections:jar:2.0:compile
关于java - 我怎么知道为什么某个 Jar 被添加到我的项目中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49335281/