Я застрял со следующей проблемой 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>
Когдапри выполнении mvn demo:demo
в проекте B, 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-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