Java NetBeans中带有gradle的JVM参数

Java NetBeans中带有gradle的JVM参数,java,gradle,jvm,netbeans-8,Java,Gradle,Jvm,Netbeans 8,我很难将NetBeans中的JVM参数传递给我的Gradle项目。我以前的尝试没有成功,也许有人可以帮助我 以下是我迄今为止所尝试的: 我通过右键单击项目-->属性-->内置任务-->运行-->将JVM值放入指定字段来添加JVM参数 -Dtest=mytestvalue (不幸的是,我的声誉不够高,无法添加嵌入式图像) 当我随后通过右键单击并运行项目时,显示: Executing: gradle :run Arguments: [-PcmdLineArgs=, -c, D:\NetBeansP

我很难将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参数:[-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).


将属性传递给gradle build和将其传递给应用程序之间存在差异。您已经知道可以在
build.gradle
中设置属性(如中所述)。您可以做的是将属性传递给Gradle,并在构建文件中查找它,然后将其进一步传递给您启动的应用程序。
顺便说一句:当你这样做的时候,你可以把系统属性(-D)或项目属性(-P)传递给Gradle,正如在

中所解释的,感谢@MarvinFrommhold和他的帖子,我终于找到了我想要的

我只需要用

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"]
}

这对于Maven项目来说是正确的,但不幸的是对于Gradle来说不是。对话看起来完全不同。我们的解决方案对我不起作用。找不到参数[build28bus5y71kwmo5sgwcrxdkkvy$的方法run()_closure5@37d70d88]在root projecthm上,您可以发布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"]
}