【问题标题】:How do I add default JVM arguments with Gradle如何使用 Gradle 添加默认 JVM 参数
【发布时间】:2016-05-16 15:48:29
【问题描述】:

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

非常感谢, 强尼

【问题讨论】:

  • 什么时候需要添加JVM参数?您可以在主类的初始化静态中设置系统变量吗?
  • 我需要在编译时添加参数,所以用户在启动他的.jar时不必手动添加它
  • 您可以在代码中执行System.setproperty("javafx.embed.singleThread", "true") 之类的操作,但您需要确保在系统属性在某处被访问之前执行此语句。

标签: java gradle jar build.gradle jvm-arguments


【解决方案1】:

从我的脑海中,我可以想到 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 值

build.gradle:

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

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

来自 gradle

$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

有关如何使用 gradle 构建环境的更多信息,请访问 docs.gradle.org

【讨论】:

  • 谢谢,第二个选项有效。第一个选项看起来很有效,但仅限于 IntelliJ。 jar 似乎有不同的执行顺序,所以它在那里不起作用。
  • @portenez,我认为选项 3 适用于运行 gradle 的 JVM
【解决方案2】:

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

【讨论】:

  • 好吧,深入研究后,Application插件只允许使用gradle run执行jvm参数。但是当消费者想要启动应用程序时,我需要它们由 .jar 执行。
【解决方案3】:

你可以使用命令行来执行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 运行 --with-debug

【讨论】:

    【解决方案4】:

    使用本地 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"'
    

    【讨论】:

      【解决方案5】:

      将此设置为您的 java 主类。

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

      【讨论】:

        猜你喜欢
        • 2013-01-16
        • 1970-01-01
        • 1970-01-01
        • 2021-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-08
        相关资源
        最近更新 更多