java - 分析时版本 17 的“未找到依赖项”
问题描述
我正在研究的项目之一具有以下相关配置:
<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-maven-3.6.3, java : 17-ea) 时,构建按预期成功。我现在对属性进行了更改,以 将源和目标替换为发布:
<maven.compiler.release>17</maven.compiler.release>
终端上的日志读取
[INFO] --- maven-dependency-plugin:3.2.0:analyze-only (default) @ java-8-matchers --- [WARNING] Non-test scoped test only dependencies found: [WARNING] org.hamcrest:hamcrest:jar:2.2:compile
导致失败(因为警告)!为什么/如何在 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)
使用复制:
- Git 签出这个分支。
- 配置 Maven 以使用 Java-17。
failOnWarning
将属性编辑为true
。- 执行
mvn clean verify
。
解决方案
经过调查,我发现问题出在maven-dependency-plugin
从版本 3.1.2 升级到 3.2.0,因为在新版本的插件中添加了新属性进行分析: testArtifactsWithNonTestScope
您可以比较以下代码截图: 版本 3.1.2:
我在插件中调试了完整的分析器依赖过程,发现依赖:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>
被传递给testArtifactsWithNonTestScope
一组工件,因为类org.hamcrest.MatcherAssert
仅在测试类中使用。当然,这种行为是不正确的,因为依赖的其他类hamcrest
存在于非测试类中 - 肯定存在maven-dependency-plugin
.
附言
就像我的想法的证明一样,您可以通过以下方式更改非测试类之一:
...
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
in的新方法Java8Matchers
,之后构建过程将成功完成。
经过以上所有方面,您有以下方法可以解决问题:
- 使用插件 3.1.2 支持的最新版本的 java 并且在错误修复之前不要更新插件
- 为构建关闭分析器并等待分析器的修复
推荐阅读
- sql - 合并最终表中的更新记录
- flutter - 列表包含“未来”的实例
' - java - Optional#map 可以将输入参数的状态更改为 Function lambda 吗?
- scala - 如何从 Azure Databricks Scala Notebook 发送电子邮件
- javascript - 从JS中的单词数组中选择单词
- javascript - 如何修复从序列化 C# 日期到 js 日期的转换,因为 js 日期已过一天或时区
- python - 为什么 dropna 和 as_type 等在 for 循环中不起作用?大熊猫
- laravel - Retrive model results ordered by SUM from relationship
- c# - 如何将此 PHP 转换为 C# 代码?
- excel - 条件格式和普通单元格中相同公式的结果不同