Get command line arguments from spring-boot: execute

Is there a way to enter arguments when starting the spring-boot (mvn spring-boot: run) application from the command line and then get them in main ()?

+55
java spring-boot maven command-line-arguments
Apr 26 '14 at 21:43
source share
7 answers

Looking at the source code for the spring-boot-maven-plugin plugin, I found what you need to do:

mvn spring-boot:run -Drun.arguments="arg1,arg2" 

Another way to get more information about which run parameters the spring-boot plugin's run target is to run the following command:

 mvn help:describe -Dcmd=spring-boot:run -Ddetail 

For Spring Boot 2.x, the source code is here, and now you need to use -Dspring-boot.run.arguments="args1,args2"

If you are using Gradle and want to be able to pass command line arguments to the Gradle bootRun task, you first need to configure it, like so:

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

and run the task using gradle bootRun -Pargs="arg1 arg2"

+87
Apr 26 '14 at 22:12
source share

When passing multiple arguments using -Drun.arguments, if the argument, in turn, has values โ€‹โ€‹separated by commas, then only the first value of each argument is used. To avoid this, repeat the argument as many times as the number of values.

This is a more workaround. Not sure if there is an alternative if the delimiter is no different - for example, "|".

For example, the problem:

 mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,dev" 

Selects only the "test" profile for the specified command.

Workaround:

 mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,--spring.profiles.active=dev" 

Selects the "dev" and "test" profiles for the specified command.

+30
Apr 01 '15 at 8:31
source share

Keep in mind: the method of passing arguments depends on the main version of spring-boot .

TL; DR

For Spring Boot 1:

 mvn spring-boot:run -Drun.arguments="argOne,argTwo" 

For Spring Boot 2:

 mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo" 



1) the version of spring-boot-maven-plugin and the used version of Spring Boot should be aligned.

According to the main version of Spring Boot used ( 1 or 2 ), it is really necessary to use spring-boot-maven-plugin in version 1 or 2 .
If your pom.xml inherits from spring-boot-starter-parent :

 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>ONE_OR_TWO_VERSION</version> </parent> 

In your pom, the version of the plugin used should not even be specified, since this plugin dependency is inherited:

 <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> ... </configuration> </plugin> </plugins> 

If your pom.xml not inherited from spring-boot-starter-parent , be sure to align the version of spring-boot-maven-plugin with the exact version of spring boot you want to use.

2) Passing arguments on the command line using spring-boot-maven-plugin: 1.XX

For one argument:

 mvn spring-boot:run -Drun.arguments="argOne" 

for several:

 mvn spring-boot:run -Drun.arguments="argOne,argTwo" 

The maven plugin page documents this:

  Name Type Since Description arguments | String[] | 1.0 | Arguments that should be passed to the application. On command line use commas to separate multiple arguments. User property is: run.arguments. 

3) Passing arguments on the command line using spring-boot-maven-plugin: 2.XX

For one argument:

 mvn spring-boot:run -Dspring-boot.run.arguments="argOne" 

for several:

 mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo" 

I did not find the plugin documentation for version 2.XX that references this.
But the org.springframework.boot.maven.AbstractRunMojo class from spring-boot-maven-plugin:2.0.0.M3 refers to this user property:

 public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo ... @Parameter(property="spring-boot.run.arguments") private String[] arguments; ... protected RunArguments resolveApplicationArguments(){ RunArguments runArguments = new RunArguments(this.arguments); addActiveProfileArgument(runArguments); return runArguments; } ... } 

4) Hint: when passing more than one argument, spaces between commas are taken into account.

 mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo" 

will be interpreted as ["argOne", "argTwo"]

But this:

 mvn spring-boot:run -Dspring-boot.run.arguments="argOne, argTwo" 

will be interpreted as ["argOne", " argTwo"]

+12
08 Oct '17 at 8:06 on
source share

And if you use Eclipse ...

 |  Parameter Name |  Value |
 |  run.arguments |  "--name = Adam" |
+1
Jul 26 '14 at 21:01
source share

Spring Boot 1 as 2 provides a way to pass multiple profiles as an argument and avoids the problem of a comma used as a separator between arguments and values โ€‹โ€‹passed as the active profile.

Therefore, instead of writing:

 mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=test,--spring.profiles.active=dev 

use the Spring Boot Maven profiles property, which is a convenience label for spring.profiles.active for example:

The Maven custom property differs depending on the version of Spring Boot.

For Spring Boot run.profiles , this is run.profiles :

 mvn spring-boot:run -Drun.profiles=dev,test 

For Spring Boot 2, this is spring-boot.run.profiles :

 mvn spring-boot:run -Dspring-boot.run.profiles=dev,test 

From the plugin documentation:

profiles :

Spring profiles for activation. A convenient shortcut for specifying the argument spring.profiles.active. At the command line, use commas to separate multiple profiles.

Type: java.lang.String []

C: 1.3

Requires: no

User Property: spring-boot.run.profiles

+1
Jul 21 '18 at 14:57
source share

This is what worked for me ( spring-boot v1.4.3.RELEASE ),

 mvn spring-boot:run -Dspring.profiles.active=test,local -Dlogback-debug=true 
0
May 02 '17 at 5:50 a.m.
source share

For the latest version of Spring, use -Dspring-boot.run.arguments =, as shown in the example below

 spring-boot:run -Djasypt.encryptor.password=temp -Dspring-boot.run.arguments="OU,Grade" 
0
Feb 04 '19 at 10:38
source share



All Articles