java 抑制 Maven 依赖插件的“发现未使用的声明依赖”警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36573510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:37:07  来源:igfitidea点击:

Suppress Maven Dependency Plugin's "Unused declared dependencies found" warnings

javamavenmaven-3pom.xmlmaven-dependency-plugin

提问by vpiTriumph

The maven-dependency-pluginidentifies what it believes to be unused dependencies when you compile by producing warnings at compile time.

maven-dependency-plugin它认为是未使用的依赖,当你在编译时产生警告编译标识。

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

In some cases this message is a false positive and the dependency is required transitively.

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

Question: How can I identify in my pom.xmlthat this is the case?

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

回答by A_Di-Matteo

You should configure in your pom the ignoredDependencieselement:

您应该在 pom 中配置ignoredDependencies元素:

List of dependencies that will be ignored. Any dependency on this list will be excluded from the "declared but unused" and the "used but undeclared" list. The filter syntax is:

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

where each pattern segment is optional and supports full and partial * wildcards. An empty pattern segment is treated as an implicit wildcard. *

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

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

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

As also specified by the official Exclude dependencies from dependency analysis. A sample configuration would be:

正如官方所指定的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>

回答by npn_or_pnp

回答by supersingh05

try using provided scope

尝试使用提供的范围

provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

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

回答by Manuel Romeiro

Since maven-dependency-plugin version 2.6 you can use usedDependenciestag to force dependencies as used.

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

回答by user9130454

That was nearly what i was looking for, but i guess you specify that a little more like:

这几乎就是我要找的,但我想你更像是指定了一点:

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

So this does nearly the same but is more specific on which kind of dependencies should be ignored

所以这几乎是一样的,但更具体的是应该忽略哪种依赖