我使用Java 9模块设置了一个小的测试项目。其结构如下:
.
├── build.gradle
└── src
├── main
│ └── java
│ ├── module-info.java
│ └── slfTest
│ └── Main.java
└── test
└── java
└── slfTest
└── MainTest.java
(随意克隆并查看您自己:git clone https://github.com/michas2/slfTest.git
)
类Main和Main Test仅记录一些简单的输出:
Logger logger = LoggerFactory.getLogger(Main.class);
logger.info("Hello World");
现在gradle run
按预期工作,但gradle test
会抛出ClassCastException异常。
$ gradle run -q
[main] INFO slfTest.Main - Hello World
$ gradle test -q
java.lang.ClassCastException: org.slf4j.simple/org.slf4j.simple.SimpleLoggerFactory cannot be cast to org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext
at org.gradle.internal.logging.slf4j.Slf4jLoggingConfigurer.configure(Slf4jLoggingConfigurer.java:42)
at org.gradle.internal.logging.config.LoggingSystemAdapter.startCapture(LoggingSystemAdapter.java:54)
at org.gradle.internal.logging.services.DefaultLoggingManager$StartableLoggingSystem.start(DefaultLoggingManager.java:297)
at org.gradle.internal.logging.services.DefaultLoggingManager.start(DefaultLoggingManager.java:73)
at org.gradle.internal.logging.services.DefaultLoggingManager.start(DefaultLoggingManager.java:37)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:83)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:64)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:62)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:67)
移除Java 9模块后,问题得到解决。因此我认为这是模块访问问题。 - 应该如何正确修复它?
module-info.java
的内容为:
module slfTest {
requires org.slf4j;
exports slfTest;
}
当Gradle运行测试时,它会尝试注入自己的日志记录后端。- 猜测这可能是一些模块访问问题引起的部分。
gradle
和slf4j
版本细节与Java9兼容? - Naman