我正在研究的项目之一具有以下相关配置:
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<failOnWarning>true</failOnWarning>
</configuration>
<executions>
<execution>
<goals>
<goal>analyze-only</goal>
</goals>
</execution>
</executions>
</plugin>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<!-- not scoped to test -->
</dependency>在执行mvn clean verify (apache 3.6.3,java : 17-ea)时,构建按预期成功。现在,我已经对属性进行了更改,将替换为(源和目标),如下所示:
<maven.compiler.release>17</maven.compiler.release>终端上的日志显示
信息-maven-依赖-插件:3.2.0:仅仅分析(默认)@ java-8-matchers只发现警告非测试作用域的依赖关系:警告org.hamcrest:hamcrest:jar:2.2:编译
导致失败(因为警告)!为什么/如何在Java版本的升级中对依赖进行不同的处理?有办法解决这个问题吗?
如果对此目标有帮助,则用于此目标的调试日志如下:
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-dependency-plugin:3.2.0:analyze-only from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-dependency-plugin:3.2.0, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@579bb367]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-dependency-plugin:3.2.0:analyze-only' with basic configurator -->
[DEBUG] (f) analyzer = default
[DEBUG] (f) baseDir = .../java-8-matchers
[DEBUG] (f) failOnWarning = true
[DEBUG] (f) ignoreNonCompile = false
[DEBUG] (f) ignoreUnusedRuntime = false
[DEBUG] (f) outputDirectory = .../java-8-matchers/target
[DEBUG] (f) outputXML = false
[DEBUG] (f) project = MavenProject: uk.co.probablyfine:java-8-matchers:2.0.0-SNAPSHOT @ .../java-8-matchers/pom.xml
[DEBUG] (f) scriptableFlag = $$%%%
[DEBUG] (f) scriptableOutput = false
[DEBUG] (f) skip = false
[DEBUG] (f) verbose = false
[DEBUG] -- end configuration --
[WARNING] Non-test scoped test only dependencies found:
[WARNING] org.hamcrest:hamcrest:jar:2.2:compile
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:3.2.0:analyze-only (default) on project java-8-matchers: Dependency problems found -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:3.2.0:analyze-only (default) on project java-8-matchers: Dependency problems found
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)使用以下方法复制:
failOnWarning属性编辑为true。mvn clean verify。发布于 2021-06-29 01:08:42
经过调查,我发现问题在于将maven-dependency-plugin从3.1.2版本升级到3.2.0版本,因为在插件的新版本中添加了用于分析:testArtifactsWithNonTestScope的新属性,您可以比较下面的代码截图:版本3.1.2:

3.2.0版:

我在插件中调试了完整的分析器依赖进程,发现依赖:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>传递给testArtifactsWithNonTestScope工件集,因为类org.hamcrest.MatcherAssert仅在测试类中使用。当然,这种行为是不正确的,因为来自hamcrest依赖的其他类都存在于非测试类中--在maven-dependency-plugin中肯定存在缺陷。
P.S.
就像我的观点的证明一样,您可以通过以下方式更改一个非测试类:
...
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
...
public final class Java8Matchers {
public static void customAssert(String reason, boolean assertion) {
assertThat(reason, assertion);
}
...
}如您所见,我在org.hamcrest.MatcherAssert Java8Matchers 中使用添加了新方法,然后构建过程将成功完成。
在完成以上所有方面之后,您有以下解决问题的方法:
发布于 2021-06-26 20:52:42
这个问题与Java版本无关,它在Java 16中也失败了,在我看来,当maven依赖项插件被依赖方从3.1.2升级到3.2.0时,降级解决了这个问题。
发布于 2022-02-22 08:44:41
我通过从下面的repo https://github.com/apache/maven-dependency-plugin/tree/maven-dependency-plugin-3.2.0克隆maven依赖插件来解决这个问题,并且我自己构建了它。然后我在我的项目中使用了3.3.0快照版本!
https://stackoverflow.com/questions/68013715
复制相似问题