取消Maven Dependency Plugin的“未使用声明的依赖关系”警告

时间:2016-04-12 12:37:42

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

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

SELECT

在某些情况下,此消息是误报,并且需要传递依赖性。

问题:如何在我的[WARNING] Unused declared dependencies found: [WARNING] org.foo:bar-api:jar:1.7.5:compile 中识别出这种情况?

5 个答案:

答案 0 :(得分:9)

您应该在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>

答案 1 :(得分:1)

答案 2 :(得分:1)

尝试使用提供的范围

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

答案 3 :(得分:1)

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

答案 4 :(得分:0)

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

boolean deviceHeaderExists = httpReq.getHeader("device") != null;

所以这几乎相同,但更具体地说明应该忽略哪种依赖