如何使用 Gradle 添加默认的 JVM 参数 [英] How do I add default JVM arguments with Gradle
问题描述
当使用 Gradle 构建时,我需要将默认的 JVM 选项添加到我的 jar 中.从我得到的文档中我必须设置:
I need to add default JVM options to my jar, when build with Gradle. From the documentation I got that I have to set:
applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
我对 Gradle 没有太多经验,编写 build.gradle 文件的开发人员编写的文件与大多数网站提供的示例不同.
I have not much experience with Gradle and the developer that wrote the build.gradle file wrote it different from what most websites give as examples.
这是build.gradle:
Here is the 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'
}
我不知道把参数放在哪里.我尝试将它们放在不同的位置,但我总是得到:
I don't know where to put the arguments. I tried putting them in different locations, but I always get:
A problem occurred evaluating root project 'placeholder'.
> No such property: applicationDefaultJvmArgs for class: org.gradle.api.tasks.bundling.Jar_Decorated
非常感谢,约翰尼
推荐答案
在我的脑海中,我可以想到 2 个选项:
From the top of my head I can think of 2 options:
选项 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:
build.gradle:
apply plugin: 'application'
//your code
applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
现在您可以通过两种方式运行代码:
Now you can run your code 2 ways:
来自 gradle
$gradle run
来自发行版(脚本).从应用程序插件将提供的生成脚本中:
From distribution(script). from the generated script that the application plugin will provide:
$gradle clean build distZip
然后 gradle 将在 ${your.projectdir}/build
下的某处生成一个 zip 文件.找到 zip 然后解压缩它,在 /bin
下你会找到一个 ${yourproject}.bat
和 ${yourproject}
可执行文件.一个用于 Linux/mac/unix (${yourproject}
) 另一个用于 windows (${yourproject.bat}
)
Then gradle will generate a zip file somewhere under ${your.projectdir}/build
. Find the zip then unzip it and under /bin
you'll find a ${yourproject}.bat
and ${yourproject}
executables. One is for Linux/mac/unix (${yourproject}
) the other one is for 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 构建环境的更多信息一个>
这篇关于如何使用 Gradle 添加默认的 JVM 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!