Maven插件编译良好,但在执行过程中找不到类
我陷入了以下Maven问题。
基本上,我有两个项目。项目A是一个Maven插件,项目B正在使用它。
项目 A :pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.martinschneider</groupId>
<artifactId>demo-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
项目 B :pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.martinschneider</groupId>
<artifactId>demo-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>io.github.martinschneider</groupId>
<artifactId>demo-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
</plugin>
</plugins>
</build>
</project>
在项目B上执行时,mojo开始执行,但随后我收到以下错误:mvn demo:demo
[ERROR] Failed to execute goal my.group:my-maven-plugin:1.2.3:some-goal (default-cli) on project my-project: Execution default-cli of goal my.group:my-maven-plugin:1.2.3:some-goal failed: A required class was missing while executing io.github.martinschneider:demo-maven-plugin:0.0.1-SNAPSHOT:demo: org/slf4j/event/Level
错误的核心信息是:
A required class was missing [...] : org/slf4j/event/Level
日志和堆栈跟踪还包括:
[WARNING] Error injecting: package.SomeMojo java.lang.NoClassDefFoundError: org/slf4j/event/Level
和
Caused by: java.lang.ClassNotFoundException: org.slf4j.event.Level
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass (SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass (ClassRealm.java:271)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass (ClassRealm.java:247)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass (ClassRealm.java:239)
它归结为在类路径上找不到,这很奇怪,因为它在插件中既是显式的,也是传递的(通过)依赖项。org.slf4j.event.Level
slf4j-api
slf4j-core
但是,由于某种原因,它不包含在类域中:
[DEBUG] Populating class realm plugin>io.martinschneider.github:demo-maven-plugin:0.0.1-SNAPSHOT
[DEBUG] Included: io.martinschneider.github:demo-maven-plugin:0.0.1-SNAPSHOT
[DEBUG] Included: org.slf4j:slf4j-simple:jar:1.7.25
...
slf4j-simple
在列表中,不是。slf4j-api
前面几行:
[DEBUG] Dependency collection stats: {ConflictMarker.analyzeTime=121223, ConflictMarker.markTime=606293, ConflictMarker.nodeCount=72, ConflictIdSorter.graphTime=74295, ConflictIdSorter.topsortTime=819750, ConflictIdSorter.conflictIdCount=45, ConflictIdSorter.conflictIdCycleCount=0, ConflictResolver.totalTime=1909563, ConflictResolver.conflictItemCount=70, DefaultDependencyCollector.collectTime=68369892, DefaultDependencyCollector.transformTime=3575054}
[DEBUG] io.martinschneider.github:demo-maven-plugin:jar:0.0.1-SNAPSHOT:
[DEBUG] org.slf4j:slf4j-simple:jar:1.7.25:compile
[DEBUG] org.slf4j:slf4j-api:jar:1.7.25:compile
...
在这里,两者都包括在内。slf4j-simple
slf4j-api
仅当我在插件代码中显式使用受影响的类时,才会出现问题。如果我删除它的任何显式用法,一切都很好。org.slf4j.event.Level
我使用的是 Maven 3.5.2 和 Java 11。
我不知道这是SLF4J的问题还是Maven的更普遍的问题,或者只是一个不幸的事件组合。
我尝试过:
- 删除并重新填充了我的 Maven 存储库(以排除损坏的 JAR 文件)
- 尝试了不同版本的SLF4J
- 显式添加到
slf4j-api
<pluginDepdendencies>
- 使用Java 8(而不是11)
更新我创建了一个演示项目来重现此错误:https://github.com/martinschneider/stackoverflow_53757567