【问题标题】:Remove Duplicate Dependencies in Maven Pom删除 Maven Pom 中的重复依赖项
【发布时间】:2017-12-31 15:49:00
【问题描述】:

我想删除 maven pom.xml 中的重复依赖项。

我想要多个工具 Linux、Notepad++、Netbeans、Intellij ...甚至...VIM 等的答案。

示例(我可以明显删除但我想要搜索替换答案)

  `<dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.7</version>
    </dependency>`  

请在本地写下答案链接如果被删除可能会令人沮丧。谢谢。

【问题讨论】:

  • 通用的最佳实践解决方案显然是首先避免添加重复的依赖项。一旦你有足够的pom.xml 具有无法手动处理它们的重复依赖项,我真的不确定开发自定义解决方案以删除它们的成本是否值得离开的成本可能很低它们和/或在找到它们时更正它们。

标签: linux windows maven netbeans notepad++


【解决方案1】:

由于 pom.xml 是 XML,您也可以使用 XSLT:

  1. 转到https://www.freeformatter.com/xsl-transformer.html

  2. 使用这个 XSLT:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
    <xsl:template match="/*">
        <dependencies>
            <xsl:for-each-group select="dependency" group-by="concat(groupId , '|', artifactId)">
                <xsl:sequence select="."/>
            </xsl:for-each-group>
        </dependencies>
    </xsl:template>
    
    </xsl:stylesheet>
    
  3. 从 pom.xml 中删除 &lt;dependencies&gt; 部分作为输入

  4. 将 XSLT 的输出粘贴回你的 pom.xml

【讨论】:

    【解决方案2】:

    dependency:analyze-duplicate 分析pom.xml中的and标签,判断重复声明的依赖。

    mvn dependency:analyze-duplicate
    

    【讨论】:

      【解决方案3】:

      运行 mvn 清洁 它会告诉您重复的依赖项。然后删除它们。

      【讨论】:

      • 我仍然希望看到多行查找替换选项,但这是关于现场的。
      • 如何删除它们?
      【解决方案4】:

      您可以使用mvn dependency:tree 命令在您的项目中查找重复的依赖项。

      使用 &lt;exclusions&gt; 标签到 pom 的 &lt;dependency&gt; 标签中,从 maven 项目中排除重复的依赖项。

      <dependencies>
          <dependency>
            <groupId>test.ProjectX</groupId>
            <artifactId>ProjectX</artifactId>
            <version>2.0</version>
            <scope>compile</scope>
            <exclusions>
              <exclusion>  
                <groupId>test.ProjectY</groupId>
                <artifactId>ProjectY</artifactId>
              </exclusion>
            </exclusions> 
          </dependency>
        </dependencies>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-07
        • 2022-12-06
        • 2010-11-01
        • 2015-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多