如何从bootRun传递JVM选项 [英] How to pass JVM options from bootRun
问题描述
我正在开发与远程主机通信的简单Spring Web应用程序,我想在公司代理的本地测试它。
我使用Spring Bootgradle插件,问题是我如何为JVM指定代理设置?
几种方法:
$ b $ ol
gradle -Dhttp.proxyHost = XXXX -Dhttp.proxyPort = 8080 bootRun code>
export JAVA_OPTS = - Dhttp.proxyHost = XXXX -Dhttp.proxyPort = 8080
export GRADLE_OPTS = - Dhttp.proxyHost = XXXX -Dhttp.proxyPort = 8080
但它们似乎都不起作用 - NoRouteToHostException引发网络代码。
也,我已经添加一些额外的代码调试JVM开始参数:
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean() ;
列表< String> arguments = runtimeMxBean.getInputArguments();
for(String arg:arguments)System.out.println(arg);
只打印一个参数:-Dfile.encoding = UTF-8。
如果我在代码中设置系统属性:
pre $ System $ set System .proxyHost,XXXX);
System.setProperty(http.proxyPort,8080);
一切正常!
原始答案(使用Gradle 1.12和Spring Boot 1.0.x):
Spring Boot gradle插件的 bootRun
任务扩展了Gradle JavaExec任务。参见 。
这意味着您可以通过添加以下代码来配置插件以使用代理:
bootRun {
jvmArgs =-Dhttp.proxyHost = xxxxxx,-Dhttp.proxyPort = xxxxxx
}
添加到您的构建文件中。
当然,您可以使用 systemProperties
而不是 jvmArgs
如果您想从命令行有条件地添加jvmArgs,您可以执行以下操作:
bootRun {
if( project.hasProperty('jvmArgs')){
jvmArgs project.jvmArgs.split('\\s +')
}
}
gradle bootRun - PjvmArgs = - Dwhatever1 = value1 -Dwhatever2 = value2
Upd ated答案:使用 Spring Boot 1.2.6.RELEASE 和 Gradle 2.7 我观察到,由于一些评论提及,它不起作用。
然而,可以做一些小的调整来恢复工作状态。
新代码是:
bootRun {
jvmArgs = [-Dhttp.proxyHost = xxxxxx,-Dhttp.proxyPort = xxxxxx]
}
用于硬编码参数,以及
bootRun {
if(project.hasProperty('jvmArgs')){
jvmArgs =(project.jvmArgs.split(\\ s +)as List)
}
}
用于从命令行提供的参数
I'm developing simple Spring web application that communicates with remote host and I would like to test it locally behind corporate proxy. I use "Spring Boot" gradle plugin and the question is how can I specify proxy settings for JVM?
I have try several ways to do it:
gradle -Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080 bootRun
export JAVA_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"
export GRADLE_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"
But it seems like none of them work - "NoRouteToHostException" throws in "network" code. Also, I have added some extra code to debug JVM start arguments:
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();
for (String arg: arguments) System.out.println(arg);
And only one argument was printed: "-Dfile.encoding=UTF-8".
If I set system property in code:
System.setProperty("http.proxyHost", "X.X.X.X");
System.setProperty("http.proxyPort", "8080");
Everything works just fine!
Original Answer (using Gradle 1.12 and Spring Boot 1.0.x):
The bootRun
task of the Spring Boot gradle plugin extends the gradle JavaExec task. See this.
That means that you can configure the plugin to use the proxy by adding:
bootRun {
jvmArgs = "-Dhttp.proxyHost=xxxxxx", "-Dhttp.proxyPort=xxxxxx"
}
to your build file.
Of course you could use the systemProperties
instead of jvmArgs
If you want to conditionally add jvmArgs from the command line you can do the following:
bootRun {
if ( project.hasProperty('jvmArgs') ) {
jvmArgs project.jvmArgs.split('\\s+')
}
}
gradle bootRun -PjvmArgs="-Dwhatever1=value1 -Dwhatever2=value2"
Updated Answer:
After trying out my solution above using Spring Boot 1.2.6.RELEASE and Gradle 2.7 I observed that it was not working as some of the comments mention. However, a few minor tweaks can be made to recover the working state.
The new code is:
bootRun {
jvmArgs = ["-Dhttp.proxyHost=xxxxxx", "-Dhttp.proxyPort=xxxxxx"]
}
for hard-coded arguments, and
bootRun {
if ( project.hasProperty('jvmArgs') ) {
jvmArgs = (project.jvmArgs.split("\\s+") as List)
}
}
for arguments provided from the command line
这篇关于如何从bootRun传递JVM选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!