具有 gradle 的 NetBeans 中的 JVM 参数

JVM Arguments in NetBeans with gradle

我在将 NetBeans 中的 JVM 参数传递到我的 Gradle 项目时遇到困难。我之前的尝试没有成功,也许有人可以帮助我。

这是我目前尝试过的方法: 我通过右键单击项目添加 JVM 参数 --> 属性 --> 构建任务 --> 运行 --> 将 JVM 值放入指定字段

-Dtest=mytestvalue

(不幸的是我的声誉不够高,无法添加嵌入图像) 当我 运行 之后通过右键单击项目并 运行 它显示:

Executing: gradle :run
Arguments: [-PcmdLineArgs=, -c, D:\NetBeansProjects\app\settings.gradle]
JVM Arguments: [-Dtest=mytestvalue]

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run

10:54:55.899 [main] System.getProperty('test') null

所以参数显示在 JVM Arguments: [-Dtest=mytestvalue] 中,但似乎没有传输到应用程序,System.getProperty('test') 结果为 null。我也尝试使用具有相同效果的自定义任务。

如果我创建一个 jar 文件并传递参数,一切都会按预期进行:

λ java -Dtest=mytestvalue -jar app.jar
System.getProperty('test') mytestvalue

System.getProperty('test') 结果为 mytestvalue

我目前的解决方法是在 build.gradle 文件中设置 JVM 参数,这工作正常,但我想摆脱将参数直接写入该文件的做法。

我正在使用 Gradle 3.3 和 NetBeans 8.2

 You can right click on the project, and select Properties.

 Click on Run category and insert you configuration in VM Options(not JVM).

-Dtest=testing

将 属性 传递给 gradle 构建与将其传递给您的应用程序之间存在差异。您已经知道可以在 build.gradle 中设置 属性(在 https://docs.gradle.org/current/userguide/application_plugin.html 中有描述)。您可以做的是将 属性 传递给 Gradle 并在构建文件中查找它并将其进一步传递给您启动的应用程序。 顺便说一句:执行此操作时,您可以将系统 属性 (-D) 或项目 属性 (-P) 传递给 Gradle,如 https://docs.gradle.org/current/userguide/build_environment.html#properties

中所述

感谢@MarvinFrommhold 和他的 Post 我终于找到了我想要的东西。

我只需要用

扩展 运行 任务
run {
    systemProperties = System.properties
}

并且我的参数被传递到我可以使用它的应用程序。

更新

上述方法按预期工作,但如果您不想委托所有属性,您可以指定您需要的属性。例如,您要设置 mytestvalue

你通过了 NetBeans

-Dtest=mytestvalue

并在 build.gradle

run {
    // delegate the property 'mytestvalue' to the jvm 
    systemProperty "mytestvalue", System.getProperty("mytestvalue")

    // confirm that the property has been delegated
    println "mytestvalue: " + systemProperties["mytestvalue"]
}