Java 如何使用 Gradle 添加默认的 JVM 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35244961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add default JVM arguments with Gradle
提问by Jhonny007
I need to add default JVM options to my jar, when build with Gradle. From the documentation I got that I have to set:
使用 Gradle 构建时,我需要将默认 JVM 选项添加到我的 jar 中。从我得到的文档中,我必须设置:
applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
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.
我对 Gradle 没有太多经验,编写 build.gradle 文件的开发人员编写的与大多数网站提供的示例不同。
Here is the 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'
}
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
Much Thanks, Jhonny
非常感谢,约翰尼
采纳答案by portenez
From the top of my head I can think of 2 options:
在我的脑海中,我可以想到 2 个选项:
Option1: Do what @Ethan said, it'll likely work:
选项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
}
}
Option 2: Use the application plugin + default jvm values
选项 2:使用应用程序插件 + 默认 jvm 值
build.gradle:
构建.gradle:
apply plugin: 'application'
//your code
applicationDefaultJvmArgs = ["-Djavafx.embed.singleThread=true"]
Now you can run your code 2 ways:
现在您可以通过两种方式运行您的代码:
From gradle
来自 gradle
$gradle run
From distribution(script). from the generated script that the application plugin will provide:
来自发行版(脚本)。从应用程序插件将提供的生成脚本中:
$gradle clean build distZip
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}
)
然后 gradle 将在${your.projectdir}/build
. 找到 zip 然后解压缩它,在下面/bin
你会找到一个${yourproject}.bat
和${yourproject}
可执行文件。一种适用于 Linux/mac/unix ( ${yourproject}
) 另一种适用于 windows ( ${yourproject.bat}
)
Option 3 (Android Developer): Use gradle.properties to set jvm argument
选项 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
For more info on how to use gradle build environment on docs.gradle.org
有关如何使用上的Gradle构建环境的详细信息docs.gradle.org
回答by KKishore
applicationDefaultJvmArgs is provided by the Application
plugin. So if you apply that plugin, the error would probably go away, and you should be able to execute the program by issuing gradle run
once you set the mainClassName property to the fully qualified class name, the main method of which you want to invoke.
applicationDefaultJvmArgs 由Application
插件提供。因此,如果您应用该插件,错误可能会消失,并且gradle run
一旦您将 mainClassName 属性设置为您要调用的主要方法的完全限定类名称,您应该能够通过发出来执行程序。
回答by jiahut
you can use command-line with gradle task:
您可以在命令行中使用 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) {
}
then run task with options
然后使用选项运行任务
gradle run --with-debug
gradle 运行 --with-debug