Gradle 无法使用 JavaFX 插件从 src/test 访问模块 src/main 中定义的 类
Gradle can't access classes defined in module src/main from src/test with JavaFX plugin
我试图让我的测试 classes 访问主 classes(在标准 gradle 设置中)。在我将我的主要 classes 放入模块(对于 JavaFX)之前,它工作正常,此时所有测试都停止工作。主代码运行没问题
如果我理解正确,根据 this gradle documentation,什么都不做应该 运行 测试正常,但我得到一个错误:
org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 1.
...
Caused by: java.lang.IllegalAccessError: class org.junit.platform.launcher.core.LauncherFactory (in unnamed module @0x50650eec) cannot access class org.junit.platform.commons.util.Preconditions (in module org.junit.platform.commons) because module org.junit.platform.commons does not export org.junit.platform.commons.util to unnamed module @0x50650eec
如果我使用 intellij 配置,或 运行 ./gradlew test
。
,就会发生这种情况
所以我试图通过 patching the module-info 来解决这个问题,但后来我收到了数百个这样的错误:
error: cannot find symbol
import snake.Snake
^
symbol: class Snake
location: package snake
其中 IntelliJ 用 Package 'snake' is declared in module 'snakegame', which does not export it to module 'snakegame'
解释。我猜它指的是在 src/main/java 中定义的原始模块-info.java 和 src/test/java.
中定义的辅助模块-info.java
在 Gradle documentation 中有一个代码片段,用于在 build.gradle 中自己添加补丁参数,但这只会导致此错误:
> java.lang.IllegalArgumentException: error: --patch-module specified more than once for module snakegame
实际执行的命令是这样的:
compiler args for task compileTestJava: [--patch-module, snakegame=/project/path/snake/build/classes/java/main, --module-path, ... , --add-modules, org.junit.jupiter.api, --add-reads, snakegame=org.junit.jupiter.api, --patch-module, snakegame=/project/path/snake/src/test/java]
所以它正在修补两个不同的模块,我不明白。对我来说如何并不重要,我只需要 classes 运行nable 并且可以访问主要的 classes.
更新:
所以我重新创建了该项目以尝试创建一个最小的可重现示例并一次添加一个元素,直到我添加 gradle JavaFX 插件后问题才出现,此时点它停止工作。
所以我从默认结构开始,简单地创建了一个名为 example
的包和一个 Example.class
,并在测试中创建了相同的包,一个测试 class Example_Test.class
和一个创建 Example 对象的测试。然后我在 main 中添加了一个空的模块信息。原文build.gradle:
plugins {
id 'java'
}
// default stuff
此时,一切正常。然后我将其更改为:
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
}
一切都停止了
我的首选解决方案是使用测试模块-info.java 或模块-info.test,但我无法让它工作。我最终 simply ignoring modules 进行测试,这是一个糟糕的解决方法,但目前有效。要在测试期间忽略模块,请将其添加到 build.gradle:
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
// other plugins
}
// other stuff
test {
useJUnitPlatform()
moduleOptions {
runOnClasspath = true
}
}
在test
中设置moduleOptions { runOnClasspath = true }
有用的链接:
我试图让我的测试 classes 访问主 classes(在标准 gradle 设置中)。在我将我的主要 classes 放入模块(对于 JavaFX)之前,它工作正常,此时所有测试都停止工作。主代码运行没问题
如果我理解正确,根据 this gradle documentation,什么都不做应该 运行 测试正常,但我得到一个错误:
org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 1.
...
Caused by: java.lang.IllegalAccessError: class org.junit.platform.launcher.core.LauncherFactory (in unnamed module @0x50650eec) cannot access class org.junit.platform.commons.util.Preconditions (in module org.junit.platform.commons) because module org.junit.platform.commons does not export org.junit.platform.commons.util to unnamed module @0x50650eec
如果我使用 intellij 配置,或 运行 ./gradlew test
。
所以我试图通过 patching the module-info 来解决这个问题,但后来我收到了数百个这样的错误:
error: cannot find symbol
import snake.Snake
^
symbol: class Snake
location: package snake
其中 IntelliJ 用 Package 'snake' is declared in module 'snakegame', which does not export it to module 'snakegame'
解释。我猜它指的是在 src/main/java 中定义的原始模块-info.java 和 src/test/java.
在 Gradle documentation 中有一个代码片段,用于在 build.gradle 中自己添加补丁参数,但这只会导致此错误:
> java.lang.IllegalArgumentException: error: --patch-module specified more than once for module snakegame
实际执行的命令是这样的:
compiler args for task compileTestJava: [--patch-module, snakegame=/project/path/snake/build/classes/java/main, --module-path, ... , --add-modules, org.junit.jupiter.api, --add-reads, snakegame=org.junit.jupiter.api, --patch-module, snakegame=/project/path/snake/src/test/java]
所以它正在修补两个不同的模块,我不明白。对我来说如何并不重要,我只需要 classes 运行nable 并且可以访问主要的 classes.
更新:
所以我重新创建了该项目以尝试创建一个最小的可重现示例并一次添加一个元素,直到我添加 gradle JavaFX 插件后问题才出现,此时点它停止工作。
所以我从默认结构开始,简单地创建了一个名为 example
的包和一个 Example.class
,并在测试中创建了相同的包,一个测试 class Example_Test.class
和一个创建 Example 对象的测试。然后我在 main 中添加了一个空的模块信息。原文build.gradle:
plugins {
id 'java'
}
// default stuff
此时,一切正常。然后我将其更改为:
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
}
一切都停止了
我的首选解决方案是使用测试模块-info.java 或模块-info.test,但我无法让它工作。我最终 simply ignoring modules 进行测试,这是一个糟糕的解决方法,但目前有效。要在测试期间忽略模块,请将其添加到 build.gradle:
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
// other plugins
}
// other stuff
test {
useJUnitPlatform()
moduleOptions {
runOnClasspath = true
}
}
在test
moduleOptions { runOnClasspath = true }
有用的链接: