【发布时间】:2017-06-07 22:40:02
【问题描述】:
我在 Maven 中有一个多模块项目,其中一些模块依赖于其他模块。现在,作为依赖项的模块具有一些已在依赖模块的 pom 中列出的依赖项。
有没有一种快速的方法来识别这种重复的依赖关系并将它们从依赖模块的 pom 中删除?
【问题讨论】:
标签: java maven dependencies dependency-management multi-module
我在 Maven 中有一个多模块项目,其中一些模块依赖于其他模块。现在,作为依赖项的模块具有一些已在依赖模块的 pom 中列出的依赖项。
有没有一种快速的方法来识别这种重复的依赖关系并将它们从依赖模块的 pom 中删除?
【问题讨论】:
标签: java maven dependencies dependency-management multi-module
可以扩展项目的依赖关系树以显示依赖关系冲突。 使用命令
mvn dependency:tree -Dverbose=true
来识别这种重复的依赖关系。它显示了 pom 中的所有重复和冲突。
使用 pom 的 <dependency> 部分下的 <exclusions> 标签来排除此类重复的依赖项。
<dependencies>
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
【讨论】:
mvn dependency:tree 命令在这种情况下的用途吗?
如果您使用 Eclipse 作为 IDE,则可以在相关 pom.xml 的依赖层次结构中看到重复项。并且使用排除标记可以省略它们。
【讨论】:
您可以使用mvn depgraph:graph -Dincludes=ets.tkt -DshowDuplicates -Dscope:compile.
要使用此插件,请将其放在您的 settings.xml 中
<settings>
. . .
<pluginGroups>
<pluginGroup>com.github.ferstl</pluginGroup>
</pluginGroups>
</settings>
当你运行前面的控制台命令时,你可以去 /target ,你会发现一个 .dot 文件。您可以使用 graphviz 渲染此文件。 更多信息https://github.com/ferstl/depgraph-maven-plugin
【讨论】:
有一个 JBoss 工具可以帮助解决这些问题:http://tattletale.jboss.org/ 不幸的是,这几天似乎没有在积极开发中。
【讨论】: