有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

我对Gradle和编写构建的开发人员没有太多经验。gradle file写的与大多数网站给出的示例不同

这是构建。格拉德尔:

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

非常感谢, 约翰尼


共 (4) 个答案

  1. # 1 楼答案

    从我的头脑中,我可以想到两个选择:

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

    建造。格拉德尔:

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

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

    来自格拉德尔

    $gradle run
    

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

    $gradle clean build distZip
    

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

    选项3(Android开发者):使用gradle。设置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构建环境的详细信息

  2. # 2 楼答案

    将其设置为java主类

    static {
        System.setProperty("nashorn.args", "--no-deprecation-warning");
    }
    
  3. # 3 楼答案

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

  4. # 4 楼答案

    您可以将命令行用于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