Skip to content
Advertisement

How to pass JVM options from bootRun

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:

JavaScript

And only one argument was printed: “-Dfile.encoding=UTF-8”.

If I set system property in code:

JavaScript

Everything works just fine!

Advertisement

Answer

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:

JavaScript

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:

JavaScript

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:

JavaScript

for hard-coded arguments, and

JavaScript

for arguments provided from the command line

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement