[java] How to pass JVM options from bootRun



Answers

bootRun {
  // support passing -Dsystem.property=value to bootRun task
  systemProperties = System.properties
}

This should pass all JVM options to the app started via bootRun.

Question

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:

  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"

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!




@marvin, thanks for your post it was very helpful.

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:

//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 build -Ddeep.test.run=true

was indeed passed through to the tests.

Hope this helps others trying out this approach for running tests conditionally.




bootRun {
  args = ['myProgramArgument1', 'myProgramArgument2']
}

Using jvmArgs may cause JVM start issues. Using args allows you to pass your custom program arguments




Links