如何从 bootRun 传递 JVM 选项 [英] How to pass JVM options from bootRun

查看:26
本文介绍了如何从 bootRun 传递 JVM 选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发与远程主机通信的简单 Spring Web 应用程序,我想在公司代理后面本地测试它.我使用Spring Boot"gradle 插件,问题是如何为 JVM 指定代理设置?

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?

我尝试了几种方法:

  1. gradle -Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080 bootRun
  2. 导出 JAVA_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"
  3. 导出 GRADLE_OPTS="-Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=8080"

但似乎它们都不起作用 - NoRouteToHostException"抛出网络"代码.另外,我添加了一些额外的代码来调试 JVM 启动参数:

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);

并且只打印了一个参数:-Dfile.encoding=UTF-8".

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");

一切正常!

推荐答案

原始答案(使用 Gradle 1.12 和 Spring Boot 1.0.x):

Spring Boot gradle 插件的 bootRun 任务扩展了 gradle JavaExec 任务.见 this.

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

到您的构建文件.

当然你可以使用 systemProperties 而不是 jvmArgs

Of course you could use the systemProperties instead of jvmArgs

如果您想从命令行有条件地添加 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"

更新答案:

在使用 Spring Boot 1.2.6.RELEASEGradle 2.7 尝试我的上述解决方案后,我发现它没有像一些评论提到的那样工作.但是,可以进行一些小的调整以恢复工作状态.

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.

新代码是:

bootRun {
   jvmArgs = ["-Dhttp.proxyHost=xxxxxx", "-Dhttp.proxyPort=xxxxxx"]
}

对于硬编码参数,和

bootRun {
    if ( project.hasProperty('jvmArgs') ) {
        jvmArgs = (project.jvmArgs.split("\s+") as List)

    }
}

用于从命令行提供的参数

for arguments provided from the command line

这篇关于如何从 bootRun 传递 JVM 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆