How to pass JVM parameters from bootRun

I am developing a simple Spring web application that communicates with a remote host, and I would like to test it locally for a corporate proxy. I am using the Spring Boot "gradle plugin, and the question is how to specify proxy settings for the JVM?

I tried several ways to do this:

  • gradle -Dhttp.proxyHost=XXXX -Dhttp.proxyPort=8080 bootRun
  • export JAVA_OPTS="-Dhttp.proxyHost=XXXX -Dhttp.proxyPort=8080"
  • export GRADLE_OPTS="-Dhttp.proxyHost=XXXX -Dhttp.proxyPort=8080"

But it seems that none of them work - "NoRouteToHostException" throws a "network" code. In addition, I added extra code to debug the 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 a system property in code:

  System.setProperty("http.proxyHost", "XXXX"); System.setProperty("http.proxyPort", "8080"); 

Everything works perfectly!

+61
java spring-boot gradle
Aug 01 '14 at 11:27
source share
7 answers

Original answer (using Gradle 1.12 and Spring Boot 1.0.x):

The Spring Boot Gradle plugin bootRun task extends the JavaExec Gradle task. Cm. .

This means that you can configure the plugin to use a proxy by adding:

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

into the assembly file.

Of course you can use 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 polling my solution above using Spring Boot 1.2.6.RELEASE and Gradle 2.7, I noticed that it does not work, as some of the comments say. However, some minor changes can be made to restore operation.

New code:

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

for hardcoded arguments and

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

for command line arguments

+66
Aug 01 '14 at 11:39 on
source share
 bootRun { // support passing -Dsystem.property=value to bootRun task systemProperties = System.properties } 

This should pass all JVM parameters for the application running through bootRun .

+45
01 Oct '14 at 12:52
source share

In the gradle build script, define systemProperties to complete the task.

 //to provide the properties while running the application using spring-boot run task run { systemProperties['property name'] = 'value' } 

and gradle run should take this value.

Or define a project-level property, as indicated at http://forums.gradle.org/gradle/topics/how_can_i_provide_command_line_args_to_application_started_with_gradle_run

+4
Aug 01 '14 at 11:32
source share

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

Share 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 if the property was not used to enable such tests. Using JUnit Suppose that to enable tests conditionally:

 //first line of test assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true) 

Doing this with gradle required the system property provided during gradle build startup to be shown here.

 gradle build -Ddeep.test.run=true 

really passed the tests.

We hope this helps others to experience this approach for conditional testing.

+2
Nov 28 '16 at 5:14
source share

It seems to work:

 bootRun { systemProperties "property1": "value1", "property2": "value2" } 
+1
Sep 11 '14 at 15:27
source share
 bootRun { args = ['myProgramArgument1', 'myProgramArgument2'] } 

Using jvmArg can cause problems starting the JVM. Using args allows you to pass custom program arguments

+1
Aug 05 '16 at 15:28
source share

I had a similar problem, bootRun needed some parameters, but I would not want to modify bootRun, because I want to keep some flexibility and adhere to the standard bootRun behavior. My suggestion is to add some custom tasks (say bootRunDev, bootRunProxy) that extends bootRun as described in the following code snippet

 task bootRunPxy(type: org.springframework.boot.gradle.run.BootRunTask, dependsOn: 'build') { group = 'Application' doFirst() { main = project.mainClassName classpath = sourceSets.main.runtimeClasspath systemProperty 'http.proxyHost', 'xxxxx' systemProperty 'http.proxyPort', 'yyyyy' } } 

I do not have the environment to use the script, but I used this approach to transfer the profile to spring using the spring.profiles.active property. Loans must go to Karol KaliΕ„ski

0
May 3 '17 at 8:20
source share



All Articles