【发布时间】:2019-05-14 10:00:00
【问题描述】:
我遇到了以下 Maven 问题。
基本上,我有两个项目。项目 A 是一个 Maven 插件,项目 B 正在使用它。
项目Apom.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>
项目 Bpom.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 上执行 mvn demo:demo 时,mojo 开始执行,但随后出现以下错误:
[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-core)依赖项slf4j-api。
但是,由于某种原因,它没有包含在类领域中:
[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 时才会出现此问题。如果我删除它的任何 explicit 用法,一切正常。
我正在使用 Maven 3.5.2 和 Java 11。
我不知道这是 SLF4J 的问题还是 Maven 的更普遍的问题,或者只是事件的不幸组合。
我尝试过的:
- 删除并重新填充了我的 Maven 存储库(以排除损坏的 JAR 文件)
- 尝试了不同版本的 SLF4J
- 在
<pluginDepdendencies>下明确添加slf4j-api - 使用 Java 8(而不是 11)
更新 我创建了一个演示项目来重现此错误:https://github.com/martinschneider/stackoverflow_53757567
【问题讨论】:
-
在项目 B 的
pom.xmlorg.slf4j.slf4j-api中被提及两次,这是复制到 StackOverflow 时的错误吗? -
好点,是的,这是一个错误。项目 B 中只指定了
slf4j-api。 -
首先你怎么知道有损坏的 JAR 文件?其次,为什么需要 SLF4j?你可以简单地做
getLog().debug("xxx")或getLog().info("XXX")... -
我的 Level 用例比示例中的(虚拟的)稍微复杂一些。当然,我有一个解决方法,但是这个错误太奇怪了,我想知道它的根源是什么。
-
对于损坏的文件,我认为没有。它只是发生在过去,错误有点相似(找不到类)。所以,为了排除这种情况(因为我只是没有想法了),我也尝试了一个新的 Maven 存储库。
标签: java maven maven-plugin slf4j