【问题标题】:Suppress Maven Dependency Plugin's "Unused declared dependencies found" warnings抑制 Maven 依赖插件的“发现未使用的声明依赖项”警告
【发布时间】:2016-08-03 01:55:09
【问题描述】:

maven-dependency-plugin 在编译时通过生成警告来识别它认为未使用的依赖项。

[WARNING] Unused declared dependencies found:
[WARNING]    org.foo:bar-api:jar:1.7.5:compile

在某些情况下,此消息是误报,并且依赖项是可传递的。

问题:如何在我的pom.xml 中确定是这种情况?

【问题讨论】:

  • 你使用哪个 IDE?
  • @mrbela 这是独立于 IDE 的。它涉及 Maven,如果这是您的偏好,您可以从终端运行它。
  • 你试过用--quiet参数运行maven吗?它应该只在使用时显示错误。
  • @vpiTriumph 您在 pom 中配置了 maven 依赖树的哪个目标和配置?你能分享你的 pom.xml 文件的相关部分吗?
  • @dambros 此处的目标是标记实际使用的依赖项(在 pom 中),以便在出现此警告的新实例时可以注意到它们。如果我使用--quiet,那么我会压制warnings

标签: java maven maven-3 pom.xml maven-dependency-plugin


【解决方案1】:

你应该在你的 pom 中配置 ignoredDependencies 元素:

将被忽略的依赖项列表。对此列表的任何依赖都将被排除在“已声明但未使用”和“已使用但未声明”列表中。过滤器语法是:

[groupId]:[artifactId]:[type]:[version]

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

同样由官方Exclude dependencies from dependency analysis指定。 示例配置是:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>analyze-dep</id>
                    <goals>
                        <goal>analyze-only</goal>
                    </goals>
                    <configuration>
                        <ignoredDependencies>
                            <ignoredDependency>org.foo:bar-api:jar:1.7.5</ignoredDependency>
                        </ignoredDependencies>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

【讨论】:

  • 我相信这正是我想要的。我过去在一个项目上做过这个,但找不到一个例子。令人惊讶的是,这在 SO 上很少涉及。我试试看。
  • 答案对于抑制警告是正确的。但是,我只想指出,maven 非常好,并且不会为 /compile/ 范围产生误报,因此:A - 这不是传递依赖,范围应该是 /runtime/ (你仍然会需要抑制警告),或 B - 这是一个传递依赖。在这种情况下,您可以通过简单地从您的依赖项部分中删除依赖项来消除警告(如果您需要特定版本,可能会将其添加到依赖项管理中)。
  • 在 3.1.1 版本中,我需要排除 &lt;ignoredDependency/&gt; 元素,并直接在 &lt;ignoredDependencies/&gt; 内部以逗号分隔的系列/列表(不带换行符!)指定被忽略的依赖项。
  • 我没有发现它适用于 2.10 或 3.1.1。我已经有一个“usedDependencies”列表,我必须将新的添加为其中提到的一个子项,以使其正常工作maven.apache.org/plugins/maven-dependency-plugin/…
  • 请注意,analyze-only 旨在用于构建生命周期,因此它假定已执行测试编译阶段。如果您没有在仅分析目标之前执行测试编译阶段,那么您也可能遇到未使用的声明依赖警告。您可以通过确保之前执行 test--compile 阶段或使用分析目标而不是仅分析目标来解决此问题。见:maven.apache.org/components/plugins/maven-dependency-plugin/…
【解决方案2】:

【讨论】:

  • 我熟悉mvn dependency:tree,但正如我所指出的,最终无法消除它识别为未使用的一些依赖项。例如,静态分析在通过反射来获取依赖项的使用方面很差。
【解决方案3】:

这几乎就是我想要的,但我猜你指定的更像:

<execution>
  <goals>
     <goal>analyze-only</goal>
  </goals>
  <configuration>
  <failOnWarning>true</failOnWarning>
  <ignoredUnusedDeclaredDependencies>
      <ignoredUnusedDeclaredDependency>org.reflections:reflections:*</ignoredUnusedDeclaredDependency>
  </ignoredUnusedDeclaredDependencies>
  <ignoredUsedUndeclaredDependencies>
      <ignoredUsedUndeclaredDependency>junit:*:*</ignoredUsedUndeclaredDependency>
  </ignoredUsedUndeclaredDependencies>
  <ignoreNonCompile>false</ignoreNonCompile>
  <outputXML>true</outputXML>
  </configuration>
 </execution>

所以这几乎是相同的,但更具体的是应该忽略哪种依赖关系

【讨论】:

    【解决方案4】:

    尝试使用提供的范围

    提供 这很像 compile,但表明您希望 JDK 或容器在运行时提供依赖项。例如,在为 Java Enterprise Edition 构建 Web 应用程序时,您可以将 Servlet API 和相关 Java EE API 的依赖设置为提供的范围,因为 Web 容器提供了这些类。此范围仅在编译和测试类路径上可用,并且不可传递。

    【讨论】:

      【解决方案5】:

      从 maven-dependency-plugin 2.6 版开始,您可以使用usedDependencies 标签强制使用依赖关系。

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
          <configuration>
              <usedDependencies>
                  <dependency>groupId:artifactId</dependency>
              </usedDependencies>
          </configuration>
      </plugin>
      

      【讨论】:

      • 实际上解决了我的问题,因为我有一个依赖项专门通过 java 反射使用,所以我想强制它为“已使用”。谢谢!
      【解决方案6】:

      当您在编译时而不是在运行时使用依赖项时,可能会出现此消息。您可以执行以下操作:

      <dependency>
         <groupId>org.foo</groupId>
         <artifactId>bar-api</artifactId>
         <version>1.7.5</version>
         <scope>runtime</scope>
      </dependency>
      

      【讨论】:

        猜你喜欢
        • 2018-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-12
        • 1970-01-01
        • 2012-08-17
        相关资源
        最近更新 更多