如何从bootRun传递JVM选项

Evg*_*eny 88 java gradle spring-boot

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

我尝试了几种方法:

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

但似乎没有一个工作 - "NoRouteToHostException"抛出"网络"代码.另外,我添加了一些额外的代码来调试JVM启动参数:

    RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
    List<String> arguments = runtimeMxBean.getInputArguments();
    for (String arg: arguments) System.out.println(arg);
Run Code Online (Sandbox Code Playgroud)

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

如果我在代码中设置系统属性:

    System.setProperty("http.proxyHost", "X.X.X.X");
    System.setProperty("http.proxyPort", "8080");
Run Code Online (Sandbox Code Playgroud)

一切正常!

geo*_*and 95

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

bootRun春季启动gradle这个插件的任务延长了gradle这个JavaExec任务.看到这个.

这意味着您可以通过添加以下内容来配置插件以使用代理:

bootRun {
   jvmArgs = "-Dhttp.proxyHost=xxxxxx", "-Dhttp.proxyPort=xxxxxx"
}
Run Code Online (Sandbox Code Playgroud)

到您的构建文件.

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

如果要从命令行有条件地添加jvmArgs,可以执行以下操作:

bootRun {
    if ( project.hasProperty('jvmArgs') ) {
        jvmArgs project.jvmArgs.split('\\s+')
    }
}

gradle bootRun -PjvmArgs="-Dwhatever1=value1 -Dwhatever2=value2"
Run Code Online (Sandbox Code Playgroud)

更新答案:

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

新代码是:

bootRun {
   jvmArgs = ["-Dhttp.proxyHost=xxxxxx", "-Dhttp.proxyPort=xxxxxx"]
}
Run Code Online (Sandbox Code Playgroud)

对于硬编码的参数,和

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

    }
}
Run Code Online (Sandbox Code Playgroud)

用于从命令行提供的参数

  • 我今天试过,这个工作的唯一方法就是用方括号来表示字符串列表,比如bootRun {jvmArgs = ["-Dhttp.proxyHost = xxxxxx"," - DHttp.proxyPort = xxxxxx"]} (7认同)
  • 我想在构建文件中没有"硬编码"这个选项.有可能指定代理设置会很棒.即 - 使用命令行参数. (4认同)

小智 67

bootRun {
  // support passing -Dsystem.property=value to bootRun task
  systemProperties = System.properties
}
Run Code Online (Sandbox Code Playgroud)

这应该将所有JVM选项传递给通过它启动的应用程序bootRun.

  • 这是将命令行选项传递给JVM的最佳方法 (2认同)

sum*_*n j 6

在gradle构建脚本中,为运行任务定义systemProperties.

//to provide the properties while running the application using spring-boot's run task
    run {
        systemProperties['property name'] = 'value'
    }
Run Code Online (Sandbox Code Playgroud)

并且gradle run应该接受这个值.

或者定义项目级属性,如 http://forums.gradle.org/gradle/topics/how_can_i_provide_command_line_args_to_application_started_with_gradle_run中所述


小智 5

@marvin,感谢您的帖子,它非常有帮助。

分享我的使用方式:

test {
  // support passing -Dsystem.property=value to bootRun task
  systemProperties = System.properties
}
Run Code Online (Sandbox Code Playgroud)

我有要跳过的JUnit测试,除非使用一个属性来包含此类测试。使用JUnit假定有条件地包括测试:

//first line of test
assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true)
Run Code Online (Sandbox Code Playgroud)

使用gradle进行此操作需要在运行gradle构建时提供的系统属性(如下所示),

gradle build -Ddeep.test.run=true
Run Code Online (Sandbox Code Playgroud)

确实通过了测试。

希望这可以帮助其他人尝试这种方法以有条件地运行测试。