如何从 gradle 设置使用 --enable-preview compile 和 运行 标志?
how to set the use --enable-preview compile and run flags from gradle?
Looking 在 gradle
构建中使用 Java 14 中的 records
,但得到:
thufir@dur:~/NetBeansProjects/FileWatcherHandler$
thufir@dur:~/NetBeansProjects/FileWatcherHandler$ gradle clean build
> Task :compileJava FAILED
/home/thufir/NetBeansProjects/FileWatcherHandler/src/main/java/net/bounceme/dur/files/FXOrder.java:3: error: records are a preview feature and are disabled by default.
public record FXOrder(int units) {}
^
(use --enable-preview to enable records)
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 641ms
2 actionable tasks: 1 executed, 1 up-to-date
thufir@dur:~/NetBeansProjects/FileWatcherHandler$
用 javac
编译看起来没问题:
thufir@dur:~/java$
thufir@dur:~/java$ ls
FXOrder.java
thufir@dur:~/java$
thufir@dur:~/java$ cat FXOrder.java
public record FXOrder(int units) {}
thufir@dur:~/java$
thufir@dur:~/java$ javac --enable-preview -source 14 FXOrder.java
Note: FXOrder.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
thufir@dur:~/java$
thufir@dur:~/java$ ls
FXOrder.class FXOrder.java
thufir@dur:~/java$
如何在以下构建文件中设置这些编译选项:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.4.1/userguide/tutorial_java_projects.html
*/
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:28.2-jre'
// Use TestNG framework, also requires calling test.useTestNG() below
testImplementation 'org.testng:testng:7.1.1'
}
application {
// Define the main class for the application.
// mainClassName = 'FileWatcherHandler.App'
mainClassName = 'net.bounceme.dur.files.App'
}
test {
// Use TestNG for unit tests
useTestNG()
}
java版本:
thufir@dur:~/java$
thufir@dur:~/java$ gradle --version
------------------------------------------------------------
Gradle 6.4.1
------------------------------------------------------------
Build time: 2020-05-15 19:43:40 UTC
Revision: 1a04183c502614b5c80e33d603074e0b4a2777c5
Kotlin: 1.3.71
Groovy: 2.5.10
Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM: 14.0.1 (AdoptOpenJDK 14.0.1+7)
OS: Linux 5.4.0-29-generic amd64
thufir@dur:~/java$
thufir@dur:~/java$ java --version
openjdk 14.0.1 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)
thufir@dur:~/java$
thufir@dur:~/java$ javac --version
javac 14.0.1
thufir@dur:~/java$
thufir@dur:~/java$ which java
/home/thufir/.sdkman/candidates/java/current/bin/java
thufir@dur:~/java$
将此添加到 build.gradle
文件:
compileJava {
options.warnings = false
options.deprecation = false
options.compilerArgs += ["-Xdoclint:none", "-Xlint:none", "-nowarn"]
options.compilerArgs += ["-Xlint"]
options.compilerArgs += ["--enable-preview"]
options.compilerArgs += ["-source 14"]
// options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
此外,运行 选项是必需的:
tasks.withType(JavaExec) {
jvmArgs += '--启用预览'
}
可能会修复,或者我认为至少在正确的轨道上。
要完成这项工作,您可以修改 compileJava
任务并添加此标志。将此添加到您的 build.gradle
:
compileJava {
options.compilerArgs += ['--enable-preview']
}
这将确保您的代码能够编译。
如果您有其他需要编译的任务(例如 compileTestJava
),您可以为类型为 JavaCompile
的所有任务启用此标志:
tasks.withType(JavaCompile).all {
options.compilerArgs += ['--enable-preview']
}
要为测试任务启用此标志,您可以执行以下操作:
tasks.withType(Test).all {
jvmArgs += '--enable-preview'
}
您还必须确保为将 运行 您的代码添加此标志的 JVM :
tasks.withType(JavaExec) {
jvmArgs += '--enable-preview'
}
这个在对应的JEP中有描述:
Developers who wish to use preview language features in their programs must explicitly enable them in the compiler and the runtime system
Looking 在 gradle
构建中使用 Java 14 中的 records
,但得到:
thufir@dur:~/NetBeansProjects/FileWatcherHandler$
thufir@dur:~/NetBeansProjects/FileWatcherHandler$ gradle clean build
> Task :compileJava FAILED
/home/thufir/NetBeansProjects/FileWatcherHandler/src/main/java/net/bounceme/dur/files/FXOrder.java:3: error: records are a preview feature and are disabled by default.
public record FXOrder(int units) {}
^
(use --enable-preview to enable records)
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 641ms
2 actionable tasks: 1 executed, 1 up-to-date
thufir@dur:~/NetBeansProjects/FileWatcherHandler$
用 javac
编译看起来没问题:
thufir@dur:~/java$
thufir@dur:~/java$ ls
FXOrder.java
thufir@dur:~/java$
thufir@dur:~/java$ cat FXOrder.java
public record FXOrder(int units) {}
thufir@dur:~/java$
thufir@dur:~/java$ javac --enable-preview -source 14 FXOrder.java
Note: FXOrder.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
thufir@dur:~/java$
thufir@dur:~/java$ ls
FXOrder.class FXOrder.java
thufir@dur:~/java$
如何在以下构建文件中设置这些编译选项:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.4.1/userguide/tutorial_java_projects.html
*/
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:28.2-jre'
// Use TestNG framework, also requires calling test.useTestNG() below
testImplementation 'org.testng:testng:7.1.1'
}
application {
// Define the main class for the application.
// mainClassName = 'FileWatcherHandler.App'
mainClassName = 'net.bounceme.dur.files.App'
}
test {
// Use TestNG for unit tests
useTestNG()
}
java版本:
thufir@dur:~/java$
thufir@dur:~/java$ gradle --version
------------------------------------------------------------
Gradle 6.4.1
------------------------------------------------------------
Build time: 2020-05-15 19:43:40 UTC
Revision: 1a04183c502614b5c80e33d603074e0b4a2777c5
Kotlin: 1.3.71
Groovy: 2.5.10
Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM: 14.0.1 (AdoptOpenJDK 14.0.1+7)
OS: Linux 5.4.0-29-generic amd64
thufir@dur:~/java$
thufir@dur:~/java$ java --version
openjdk 14.0.1 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)
thufir@dur:~/java$
thufir@dur:~/java$ javac --version
javac 14.0.1
thufir@dur:~/java$
thufir@dur:~/java$ which java
/home/thufir/.sdkman/candidates/java/current/bin/java
thufir@dur:~/java$
将此添加到 build.gradle
文件:
compileJava {
options.warnings = false
options.deprecation = false
options.compilerArgs += ["-Xdoclint:none", "-Xlint:none", "-nowarn"]
options.compilerArgs += ["-Xlint"]
options.compilerArgs += ["--enable-preview"]
options.compilerArgs += ["-source 14"]
// options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
此外,运行 选项是必需的:
tasks.withType(JavaExec) { jvmArgs += '--启用预览' }
可能会修复,或者我认为至少在正确的轨道上。
要完成这项工作,您可以修改 compileJava
任务并添加此标志。将此添加到您的 build.gradle
:
compileJava {
options.compilerArgs += ['--enable-preview']
}
这将确保您的代码能够编译。
如果您有其他需要编译的任务(例如 compileTestJava
),您可以为类型为 JavaCompile
的所有任务启用此标志:
tasks.withType(JavaCompile).all {
options.compilerArgs += ['--enable-preview']
}
要为测试任务启用此标志,您可以执行以下操作:
tasks.withType(Test).all {
jvmArgs += '--enable-preview'
}
您还必须确保为将 运行 您的代码添加此标志的 JVM :
tasks.withType(JavaExec) {
jvmArgs += '--enable-preview'
}
这个在对应的JEP中有描述:
Developers who wish to use preview language features in their programs must explicitly enable them in the compiler and the runtime system