代码之家  ›  专栏  ›  技术社区  ›  vpiTriumph

禁止Maven Dependency插件的“发现未使用的声明依赖项”警告

  •  15
  • vpiTriumph  · 技术社区  · 10 年前

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

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

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

    问题 :我如何在我的 pom.xml 是这样吗?

    6 回复  |  直到 9 年前
        1
  •  18
  •   Community Mohan Dere    6 年前

    您应该在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>
    
        2
  •  4
  •   user9130454    8 年前

    这几乎就是我想要的,但我想你会说得更像:

    <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>
    

    因此,这几乎是相同的,但更具体地说,应该忽略哪种依赖关系

        3
  •  2
  •   npn_or_pnp    10 年前
        4
  •  1
  •   supersingh05    8 年前

    尝试使用提供的范围

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

        5
  •  1
  •   OneCricketeer Gabriele Mariotti    8 年前

    由于maven依赖插件版本2.6,您可以使用 usedDependencies 标记强制使用依赖关系。

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <configuration>
            <usedDependencies>
                <dependency>groupId:artifactId</dependency>
            </usedDependencies>
        </configuration>
    </plugin>
    
        6
  •  0
  •   Martin Miklovič    4 年前

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

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