Java 如何从 bootRun 传递 JVM 选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25079244/
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 to pass JVM options from bootRun
提问by Evgeny
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?
我正在开发与远程主机通信的简单 Spring Web 应用程序,我想在公司代理后面本地测试它。我使用“Spring Boot”gradle 插件,问题是如何为 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"
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:
但似乎它们都不起作用——“NoRouteToHostException”抛出“网络”代码。另外,我添加了一些额外的代码来调试 JVM 启动参数:
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".
并且只打印了一个参数:“-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!
一切正常!
采纳答案by geoand
Original Answer (using Gradle 1.12 and Spring Boot 1.0.x):
原始答案(使用 Gradle 1.12 和 Spring Boot 1.0.x):
The bootRun
task of the Spring Boot gradle plugin extends the gradle JavaExec task. See this.
在bootRun
春季启动gradle这个插件的任务延长了gradle这个JavaExec任务。看到这个。
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
当然你可以使用systemProperties
代替jvmArgs
If you want to conditionally add jvmArgs from the command line you can do the following:
如果要从命令行有条件地添加 jvmArgs,可以执行以下操作:
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.RELEASEand Gradle 2.7I 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.
在使用Spring Boot 1.2.6.RELEASE和Gradle 2.7尝试我的解决方案后,我观察到它没有像一些评论提到的那样工作。但是,可以进行一些小的调整以恢复工作状态。
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
对于从命令行提供的参数
回答by suman j
In gradle build script, define systemProperties for run task.
在 gradle 构建脚本中,为运行任务定义 systemProperties。
//to provide the properties while running the application using spring-boot's run task
run {
systemProperties['property name'] = 'value'
}
and gradle run
should accept this value.
并且gradle run
应该接受这个值。
Or define a project level property as mentioned in http://forums.gradle.org/gradle/topics/how_can_i_provide_command_line_args_to_application_started_with_gradle_run
或者定义一个项目级别的属性,如 http://forums.gradle.org/gradle/topics/how_can_i_provide_command_line_args_to_application_started_with_gradle_run
回答by levsa
It seems to work:
它似乎有效:
bootRun {
systemProperties "property1": "value1", "property2": "value2"
}
回答by Marvin Frommhold
bootRun {
// support passing -Dsystem.property=value to bootRun task
systemProperties = System.properties
}
This should pass all JVM options to the app started via bootRun
.
这应该将所有 JVM 选项传递给通过bootRun
.
回答by Cristian Botiza
bootRun {
args = ['myProgramArgument1', 'myProgramArgument2']
}
Using jvmArgs may cause JVM start issues. Using args allows you to pass your custom program arguments
使用 jvmArgs 可能会导致 JVM 启动问题。使用 args 允许您传递自定义程序参数
回答by Rishik Dhar
@marvin, thanks for your post it was very helpful.
@marvin,感谢您的帖子,这非常有帮助。
Sharing how I used it:
分享一下我的使用方法:
test {
// support passing -Dsystem.property=value to bootRun task
systemProperties = System.properties
}
I have JUnit tests that I wanted to skip unless a property was used to include such tests. Using JUnit Assume for including the tests conditionally:
我有我想跳过的 JUnit 测试,除非使用属性来包含此类测试。使用 JUnit Assume 有条件地包含测试:
//first line of test
assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true)
Doing this with gradle required that the system property provided at the time of running gradle build, shown here,
使用 gradle 执行此操作需要在运行 gradle build 时提供系统属性,如下所示,
gradle build -Ddeep.test.run=true
was indeed passed through to the tests.
确实通过了测试。
Hope this helps others trying out this approach for running tests conditionally.
希望这有助于其他人尝试这种有条件地运行测试的方法。
回答by Evelino Bomitali
I got into a similar problem, bootRun needed some parameters but I wouldn't feel like modifying bootRun as I want to keep some flexibility and stick to standard bootRun behaviour. My suggestion is to add some custom tasks (let's say bootRunDev, bootRunProxy) that extends bootRun, as described in the following code snippet
我遇到了类似的问题,bootRun 需要一些参数,但我不想修改 bootRun,因为我想保持一些灵活性并坚持标准的 bootRun 行为。我的建议是添加一些扩展 bootRun 的自定义任务(比如 bootRunDev、bootRunProxy),如以下代码段所述
task bootRunPxy(type: org.springframework.boot.gradle.run.BootRunTask, dependsOn: 'build') {
group = 'Application'
doFirst() {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
systemProperty 'http.proxyHost', 'xxxxx'
systemProperty 'http.proxyPort', 'yyyyy'
}
}
I don't have an environment to exercise the script but I used this approach to pass profile to spring using the property spring.profiles.active. Credits should go to Karol Kaliński
我没有运行脚本的环境,但我使用这种方法使用属性 spring.profiles.active 将配置文件传递给 spring。学分应该去Karol Kaliński