如何在Gradle中添加默认的JVM参数

38

我需要在使用Gradle构建时将默认JVM选项添加到我的jar包中。 从文档中得知,我需要设置以下内容:

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


你什么时候需要添加JVM参数?你能否在主类的静态初始化中设置系统变量? - Ethan
我需要在编译时添加参数,这样用户在启动 .jar 文件时就不必手动添加它了。 - Jhonny007
你可以在代码中使用类似 System.setproperty("javafx.embed.singleThread", "true") 的语句,但是你需要确保该语句在系统属性被访问之前得到执行。 - KKishore
5个回答

44

就我个人而言,我能想到两种选择:

选项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

从 distribution(script) 中获取。从应用插件提供的生成的脚本中:

$gradle clean build distZip

然后gradle会在${your.projectdir}/build目录下生成一个zip文件。找到这个zip文件并解压,在/bin目录下你会找到一个${yourproject}.bat${yourproject}可执行文件,其中一个适用于Linux/mac/unix(${yourproject}),另一个适用于Windows(${yourproject.bat})。

选项3(Android开发人员):使用gradle.properties设置jvm参数

# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx1024m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 

# You can setup or customize it according to your needs and combined with the above default value.
org.gradle.jvmargs=-Djavafx.embed.singleThread=true

有关如何在docs.gradle.org上使用Gradle构建环境的更多信息,请参阅。


谢谢,第二个选项起作用了。第一个选项看起来是有效的,但只在IntelliJ中有效。由于jar似乎有不同的执行顺序,所以它在那里无法工作。 - Jhonny007
5
@portenez,我认为选项3是针对运行Gradle的JVM的。 - kaushik

4

Application插件提供了applicationDefaultJvmArgs。因此,如果您应用该插件,则可能会消除错误,并且一旦将mainClassName属性设置为要调用的主方法的完全限定类名,就可以通过发出gradle run来执行程序。


3
好的,经过深入研究,应用程序插件只能使jvm参数在gradle运行时执行。但是我需要它们在消费者想要启动应用程序时由.jar执行。 - Jhonny007

2
使用本地gradlew是最简单的方法。只需将其附加到DEFAULT_JVM_OPTS即可。
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m" "-XX:+AllowRedefinitionToAddDeleteMethods"'

1
您可以使用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


-2

将此设置为您的Java主类。

static {
    System.setProperty("nashorn.args", "--no-deprecation-warning");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接