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

新手上路,请多包涵

使用 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

非常感谢,强尼

原文由 Jhonny007 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.7k
2 个回答

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

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

构建.gradle:

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

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

从摇篮

$gradle run

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

 $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 构建环境的更多信息

原文由 portenez 发布,翻译遵循 CC BY-SA 4.0 许可协议

applicationDefaultJvmArgs 由 Application 插件提供。因此,如果您应用该插件,错误可能会消失,并且您应该能够通过发出 gradle run 一旦您将 mainClassName 属性设置为完全限定的类名,其中的 main 方法你想调用。

原文由 KKishore 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题