'Dependency not found' 分析时使用版本 17

'Dependency not found' with release 17 while analyzing

我正在研究的 the project 之一具有以下相关配置:

<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) 时,构建按预期成功。我现在对属性进行了更改,以 replace 源和目标发布为:

<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

导致失败(因为警告)! Why/How 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)

复制使用:

  1. Git结帐this branch.
  2. 配置 maven 使用 Java-17.
  3. failOnWarning 属性 编辑为 true
  4. 执行mvn clean verify.

这个问题与Java版本无关,它也与Java 16有关,对我来说似乎是maven-dependency-plugin从3.1.2升级时出现的dependabot升级到3.2.0,降级解决问题

经过调查我发现问题出在将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 工件集,因为 class org.hamcrest.MatcherAssert 仅用于测试 classes。当然,这种行为是不正确的,因为来自 hamcrest 依赖项的其他 classes 存在于非测试 classes - maven-dependency-plugin.

中肯定存在缺陷

P.S.

就像我的想法的证明一样,您可以按以下方式更改非测试 classes 之一:

...
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);
    }
    
    ...
}

如您所见,我在 Java8Matchers 中添加了使用 org.hamcrest.MatcherAssert 的新方法,之后构建过程将成功完成。


经过以上几个方面的考虑,您可以通过以下方式解决问题:

  • 使用插件 3.1.2 支持的最新版本 java,在修复错误之前不要更新插件
  • 关闭构建分析器并等待分析器修复

我通过从以下 repo https://github.com/apache/maven-dependency-plugin/tree/maven-dependency-plugin-3.2.0 克隆 maven-dependency-plugin 解决了这个问题,并且我自己构建了它。然后我在项目中使用了3.3.0-SNAPSHOT版本!