如何从 pom 中删除重复的依赖项?

How to remove duplicate dependencies from pom?

我在 maven 中有一个多模块项目,其中一些模块依赖于其他模块。现在,充当依赖项的模块具有一些已在依赖模块的 pom 中列出的依赖项。

是否有快速的方法来识别此类重复依赖项并将其从依赖模块的 pom 中删除?

可以展开项目的依赖关系树以显示依赖关系冲突。 使用命令

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>

如果您将 eclipse 用作 IDE,那么可以在相关 pom.xml 的依赖层次结构中看到重复项。并使用排除标签可以省略它们。

有一个 JBoss 工具可以帮助解决这些问题:http://tattletale.jboss.org/ 不幸的是,这些天似乎没有在积极开发中。

您可以使用 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