Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

    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!

like image 313
Evgeny Avatar asked Aug 01 '14 11:08

Evgeny


People also ask

How do I pass JVM options to gradle?

Try to use ./gradlew -Dorg. gradle. jvmargs=-Xmx16g wrapper , pay attention on -D , this marks the property to be passed to gradle and jvm. Using -P a property is passed as gradle project property.

How does gradle bootRun work?

The Spring Boot gradle plugin provides the bootRun task that allows a developer to start the application in a “developer mode” without first building a JAR file and then starting this JAR file. Thus, it's a quick way to test the latest changes you made to the codebase.


1 Answers

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:

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

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:

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.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:

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

like image 131
geoand Avatar answered Sep 21 '22 10:09

geoand