Is there any way to input arguments when launching spring-boot application (mvn spring-boot:run) from commandline and then get them in main()?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Spring Boot 1 as 2 provide a way to pass multiple profiles as argument and avoid the issue related to the comma used both as separator between the args and the values passed as active profile.
So instead of writing :
use the Spring Boot Maven
profiles
property that is a convenience shortcut ofspring.profiles.active
such as the followings :The Maven user property is different according to the Spring Boot version.
For Spring Boot 1.4+, that is
run.profiles
:For Spring Boot 2, that is
spring-boot.run.profiles
:From the plugin documentation :
Be aware : The way of passing arguments depends on the
spring-boot
major version.TLDR
For Spring Boot 1 :
For Spring Boot 2 :
1)
spring-boot-maven-plugin
version and the theSpring Boot
version you use has to be aligned.According to the Spring Boot major version used (
1
or2
), thespring-boot-maven-plugin
in the1
or the2
version should indeed be used.If your
pom.xml
inherits from thespring-boot-starter-parent
:In your pom, the version of the plugin used should not even be specified as this plugin dependency is inherited :
In case of your
pom.xml
not inheriting fromspring-boot-starter-parent
, don't forget to align the version ofspring-boot-maven-plugin
with the exact version ofspring boot
you want to use.2) Passing arguments in command line with spring-boot-maven-plugin:1.X.X
For one argument :
for multiple :
The maven plugin page documents it :
3) Passing arguments in command line with spring-boot-maven-plugin:2.X.X
For one argument :
for multiple :
I didn't find the plugin documentation for the 2.X.X version that refers to that.
But the
org.springframework.boot.maven.AbstractRunMojo
class of thespring-boot-maven-plugin:2.0.0.M3
plugin refers to this user property:4) Hint : as you pass more than one argument, whitespaces between commas are considered.
will be interpreted as
["argOne", "argTwo"]
But this :
will be interpreted as
["argOne", " argTwo"]
Looking at the source code of the spring-boot-maven-plugin I found that you need to do:
Another way to get more information about what options the
run
goal of thespring-boot
plugin supports is to execute the following command:For Spring Boot 2.x, the source is here and you now need to use
-Dspring-boot.run.arguments="args1,args2"
If you are using Gradle and you want to be able to pass command line arguments to the Gradle
bootRun
task, you first need to configure, for example like so:and run the task using
gradle bootRun -Pargs="arg1 arg2"
When passing multiple arguments using -Drun.arguments, if the argument in turn has 'comma-separated' values, 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 more of a workaround. Not sure if there is an alternative unless the delimiter is different - like '|'.
E.g Issue:
Picks only 'test' profile for the above command.
Workaround:
Picks both 'dev' & 'test' profiles for the above command.
This is what worked for me (
spring-boot v1.4.3.RELEASE
),.And if you're using Eclipse...