如何使用Gradle添加默认JVM参数

当使用Gradle构建时,我需要向我的jar添加默认的JVM选项。 从我得到的文档中我必须设置:

applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"] 

我没有太多使用Gradle的经验,编写build.gradle文件的开发人员写的不同于大多数网站提供的示例。

这是build.gradle:

 apply plugin: 'java' apply plugin: 'eclipse' version = '0.1' repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.+' compile 'placeholder' } task release(type: Jar) { manifest { attributes("Implementation-Title": "placeholder", "Implementation-Version": version, 'Main-Class': 'placeholder.Application') } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } with jar } task wrapper(type: Wrapper) { gradleVersion = '2.2.1' } 

我不知道在哪里提出论点。 我尝试将它们放在不同的位置,但我总是得到:

 A problem occurred evaluating root project 'placeholder'. > No such property: applicationDefaultJvmArgs for class: org.gradle.api.tasks.bundling.Jar_Decorated 

非常感谢,Jhonny

从头顶我可以想到两个选项:

选项1:做@Ethan所说的,它可能会起作用:

 package placeholder; //your imports public class Application{ static { System.getProperties().set("javafx.embed.singleThread", "true"); } // your code public static void main(String... args){ //your code } } 

选项2:使用应用程序插件+默认的jvm值

的build.gradle:

 apply plugin: 'application' //your code applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"] 

现在您可以通过以下两种方式运行代码:

从gradle

 $gradle run 

从分发(脚本)。 从应用程序插件将提供的生成脚本:

 $gradle clean build distZip 

然后gradle将在${your.projectdir}/build下的某处生成一个zip文件。 找到zip然后解压缩它,在/bin你会找到${yourproject}.bat${yourproject}可执行文件。 一个用于Linux / mac / unix( ${yourproject} )另一个用于windows( ${yourproject.bat}

applicationDefaultJvmArgs由Application插件提供。 因此,如果您应用该插件,错误可能会消失,并且您应该能够通过在将mainClassName属性设置为完全限定的类名(您要调用的主方法)后发出gradle run来执行该程序。

你可以使用带有gradle任务的命令行:

 class AppRun extends JavaExec { private boolean withDebug @Option(option = "with-debug", description = "enable debug for the process. ") public void setDebugMode(boolean debug) { this.withDebug = debug } public boolean getDebugMode() { return this.withDebug } } task run(type: AppRun) { } 

然后使用选项运行任务

gradle run –with-debug