如何使用 Gradle 添加默认 JVM 参数
How do I add default JVM arguments with Gradle
在使用 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
非常感谢,
强尼
applicationDefaultJvmArgs 由 Application
插件提供。因此,如果您应用该插件,错误可能会消失,一旦您将 mainClassName 属性 设置为完全限定的 class 名称,您应该能够通过发出 gradle run
来执行程序,您要调用的主要方法。
在我的脑海中,我想到了 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
有关如何在 docs.gradle.org
上使用 gradle 构建环境的更多信息
你可以在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
将此设置为您的 java 主 Class。
static {
System.setProperty("nashorn.args", "--no-deprecation-warning");
}
使用本地 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"'
在使用 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
非常感谢, 强尼
applicationDefaultJvmArgs 由 Application
插件提供。因此,如果您应用该插件,错误可能会消失,一旦您将 mainClassName 属性 设置为完全限定的 class 名称,您应该能够通过发出 gradle run
来执行程序,您要调用的主要方法。
在我的脑海中,我想到了 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
有关如何在 docs.gradle.org
上使用 gradle 构建环境的更多信息你可以在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
将此设置为您的 java 主 Class。
static {
System.setProperty("nashorn.args", "--no-deprecation-warning");
}
使用本地 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"'