1

I have a external configuration file(out side jar). I try to run and expected that value in external file will override value in internal file(application.properties in \resource\ - in jar file). I read Documentation and try this:

java -jar ccgame-1.0.jar --spring.config.location=classpath:/application.properties,file:/production.properties

This not working.

My jar file at \target\ directory and my production.properties too(at \target\)

How can I resolve my problem?

  • Where should I put external config file ?
  • And what I have to do ?
amseager
  • 5,795
  • 4
  • 24
  • 47
Puskin
  • 125
  • 1
  • 11

2 Answers2

4

Starting from Spring Boot 2.0 it's possible to use property spring.config.additional-location. With this property, you can set external config file, but properties from that config will only override the corresponding ones from internal config, leaving other properties unchanged.

More about it in docs.

If you need to completely override the whole config, then continue to use spring.config.location property instead.

amseager
  • 5,795
  • 4
  • 24
  • 47
  • Oh my got, Thank you so much. I try to spring.config.location and Dspring.config.location so much and can't resolve my problem until I use additional-localtion from you. It break my brain 2 hours. THank you so much – Puskin Sep 18 '20 at 09:06
0

By convention, Spring Boot looks for an externalized configuration file – application.properties or application.yml – in 4 predetermined locations in the following order of precedence:

  1. /config subdirectory of the current directory
  2. The current directory
  3. Classpath /config package
  4. The classpath root

You can place your application.properties in any of the 4 locations without needing to give the location of application.properties while executing the jar. If you want to given any other custom location , then you will have to provide the path of the config location while executing the jar:

java -jar -Dspring.config.location=<path-to-file> myProject.jar

Source: https://www.baeldung.com/spring-properties-file-outside-jar

Umesh Sanwal
  • 681
  • 4
  • 13
  • It didn't work for me. In my external file config. I just change the port to 8081. The server still sun at 8080 and throw me error about dataSource. If I add data source then it working. I think it just use external file and not override internal file – Puskin Sep 18 '20 at 08:32
  • If you are giving external location which executing the jar , the internal application properties will be ignored. – Umesh Sanwal Sep 18 '20 at 08:36
  • How can I solve this. I want it will be overriden. Not ignore – Puskin Sep 18 '20 at 08:39
  • I think may be have a solution. Just i didn't know – Puskin Sep 18 '20 at 08:40
  • you can place that file also in the external location and maintain your properties like: application.properties application-prod.properties . Now while executing the jar : -Dspring.profiles.active=prod -Dspring.config.location=C:\Config ;pass the profile also . Now in this case it will first read application.properties and override from the application-prod.properties – Umesh Sanwal Sep 18 '20 at 08:42
  • Do you have another way ?. My Director has a requirement for me that: - Application have 2 file config. One is application.properties in resource(inside jar). And one is external. - When app starting. The external file will run and override internal file(for duplicate field) - The external file just override the field which need to be override(not at all) – Puskin Sep 18 '20 at 08:49
  • I found this on Spring documentation "On your application classpath (for example, inside your jar) you can have an application.properties file that provides a sensible default property value for name. When running in a new environment, an application.properties file can be provided outside of your jar that overrides the name. For one-off testing, you can launch with a specific command line switch (for example, java -jar app.jar --name="Spring")." Must be one way solve my problem – Puskin Sep 18 '20 at 08:52
  • Yes in this approach you will have to put all the properties you want to override while running the jar – Umesh Sanwal Sep 18 '20 at 08:53
  • "All the properties you want to override". Have this include properties i do not want override. For example, I just want change the port. Should i add data source config or any one to production.properties – Puskin Sep 18 '20 at 08:59
  • No just the properties you want to override; in your case just port – Umesh Sanwal Sep 18 '20 at 09:12
  • I found this from @amseager answer. It mus be --spring.config.additional-location option – Puskin Sep 18 '20 at 09:22