10

I have a spring-boot application that is configured with a yml file. Is it possible to override these properties when executing the jar? For example let say I have the input variable in yml file set to user1 and I want to execute the jar with user2. Is it possible to do something like this?

java -jar --input=user2
salvador
  • 1,079
  • 3
  • 14
  • 28
  • According to the docs, it is possible http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/htmlsingle/#boot-features-external-config-command-line-args Have you tried it? – ci_ Apr 20 '15 at 15:30
  • Yes, this is possible. I overrride my tomcat port defined in the application.yml file as server: port: ${port:8080} using --server.port=8090 – ufdeveloper Mar 16 '16 at 17:48

2 Answers2

4

To elaborate answer by cLyric, you can do this:

java -jar yourapp.jar --input=user2

Or if you want to provide using json, you can do

java -jar yourapp.jar --spring.application.json='{"input":"user2"}'

Or if you're in unix/linux,

SPRING_APPLICATION_JSON='{"input":"user2"}' java -jar yourapp.jar 
Community
  • 1
  • 1
eis
  • 51,991
  • 13
  • 150
  • 199
  • In my project, it's not work on "java -jar app.jar --input=user2" but work well with "java -Dinput=user2 -jar app.jar". But with the second command can't override configuration in application.properties – free斩 Feb 15 '17 at 07:34
  • @free斩 the second command is different, not related to spring boot. You should probably open another question if the standard `--input` style is for some reason not working. – eis Feb 15 '17 at 07:37
  • http://stackoverflow.com/questions/42244380/override-yml-configuration-in-spring-boot-with-command-line-arguments-not-work – free斩 Feb 15 '17 at 08:41
0

this worked for me

java -Dspring.profiles.active=test-environment -jar config-server-0.0.1-SNAPSHOT.jar

If we see below syntax, it tells options should be included between java and -jar. Also, options are listed below as well.

Reason why this command works. Also, it takes the parameters instead of the application.yml configuration.

C:\Users\joy> java -?

Usage: java [-options] -jar jarfile [args...] (to execute a jar file) where options include:

-d32          use a 32-bit data model if available
-d64          use a 64-bit data model if available
-server       to select the "server" VM
              The default VM is server. 
 -cp <class search path of directories and zip/jar files>
 -classpath <class search path of directories and zip/jar files>
               A ; separated list of directories, JAR archives,
               and ZIP archives to search for class files.
 **-D<name>=<value>
               set a system property**

Note: Operating system is windows

Joy
  • 21
  • 2
  • Please don't post code-only answers but add a little textual explanation about how and why your approach works and what makes it different from the other answers given. You may also have a look at our ["How to write a good answer"](https://stackoverflow.com/help/how-to-answer) entry. – ahuemmer Jul 22 '22 at 14:39