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

时间:2016-02-06 18:45:08

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

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

3 个答案:

答案 0 :(得分:22)

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

选项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 Developer):使用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构建环境的详细信息

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

    final SparkConf sparkConf = new SparkConf()
        .setMaster("local[*]")
        .setAppName("test_app_name");

    final JavaSparkContext jsc = new JavaSparkContext(sparkConf);

    // Get the singleton instance of SparkSession
    SparkSession sparkSession = SparkSession.builder()
            .config(jsc.getConf())
            .getOrCreate();

    Dataset<Row> dataset = sparkSession.read()
            .parquet(inputPath);

然后使用选项

运行任务
  

gradle run --with-debug