如何在基于Kotlin的Gradle脚本中为测试启用“ --enable-preview”?我实际上尝试了可以在网上找到的所有内容,其中https://stackoverflow.com/a/61849770/226895最接近正确答案。
我在:test
任务上仍然收到以下错误
org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not execute test class 'com.blabla.playground.AppTest'.
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:53)
Caused by: java.lang.UnsupportedClassVersionError: Preview features are not enabled for com/blabla/playground/AppTest (class file version 58.65535). Try running with '--enable-preview'
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
按脚本是
plugins {
java
application
}
repositories {
jcenter()
}
dependencies {
implementation("com.google.guava:guava:28.2-jre")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
}
application {
mainClassName = "com.blabla.playground.App"
}
java {
sourceCompatibility = JavaVersion.VERSION_14
targetCompatibility = JavaVersion.VERSION_14
}
tasks {
withType<Test>().all {
allJvmArgs.add("--enable-preview")
testLogging.showStandardStreams = true
testLogging.showExceptions = true
useJUnitPlatform {
}
}
withType<JavaExec>().all {
jvmArgs!!.add("--enable-preview")
}
withType<Wrapper>().all {
gradleVersion = "6.4.1"
distributionType = Wrapper.DistributionType.BIN
}
withType(JavaCompile::class.java).all {
options.compilerArgs.addAll(listOf("--enable-preview", "-Xlint:preview"))
}
jar {
manifest {
attributes("Main-Class" to "com.blabla.playground.App")
}
}
}
我想念什么?
事实证明,为Test
任务配置标志的方式并未真正将标志附加到allJvmArgs
。
我设法通过配置Test
任务使其工作:
withType<Test>().all {
jvmArgs("--enable-preview")
}
或
withType<Test>().all {
allJvmArgs = listOf("--enable-preview")
}
然而,第二个选项可能不是更可取,因为它将所有JVM参数替换为派生进程(包括用于定义系统属性,最小/最大堆大小和引导程序类路径的参数)。第一个选项应该更可取。