我有一个Maven模块(多模块项目的一部分),声明了以下依赖项:
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1</version>
</dependency>
该包中的类以下列方式使用:
private static final Log LOG = LogFactory.getLog(MyClass.class);
一切都很好。
当我运行mvn dependency:analyze
(或analyze-only
)时,会发出以下警告:
[WARNING] Unused declared dependencies found:
[WARNING] commons-logging:commons-logging:jar:1.1:compile
这似乎表明我可以删除所述依赖项,特别是因为我们要将failOnWarning
选项设置为true
,因此警告会导致构建失败。但是,显然需要包,当我删除它时,模块无法编译:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project mymodule: Compilation failure: Compilation failure:
[ERROR] MyClass.java:[18,34] package org.apache.commons.logging does not exist
[ERROR] MyClass.java:[19,34] package org.apache.commons.logging does not exist
[ERROR] MyClass.java:[44,26] cannot find symbol
[ERROR] symbol: class Log
[ERROR] location: class MyClass
周围有相关问题:
How to tell maven-dependency-plugin that the artifact is used in the project?,
Why is dependency:analyze lying to me?
但它们要么没有答案,要么答案与范围问题有关。就我而言,范围必须是compile
(即无需声明)。