Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

application.properties outside jar file how to [duplicate]

As stated in spring-boot-reference:

On your application classpath (e.g. inside your jar) you can have an application.properties that provides a sensible default property value for name. When running in a new environment, an application.properties can be provided outside of your jar that overrides the name

I place a duplicated application.properties with overridden name on the same path as jar file, however when running the application with:

java -jar target/myproject-0.0.1-SNAPSHOT.jar

The name value is not overridden, it still refers to the one inside application.properties inside jar file. I also tried:

java -Dspring.config.location=/target/application.properties -jar target/myproject-0.0.1-SNAPSHOT.jar

But it does not work, please help.

Edit

When I change the current directory to target and run it, it works.

java -jar myproject-0.0.1-SNAPSHOT.jar

Why? Why cannot be outside the path and run it?

like image 402
Gibi Avatar asked Sep 10 '16 15:09

Gibi


People also ask

Is application properties part of jar?

Application properties packaged inside your jar ( application. properties and YAML variants). @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed.

How do I change the properties of a jar file?

Use 7zip, right click on jar and with 7z say open archive, then go to property file, double click and open file with notepad, edit it and save. Now when you will close 7z console, it will ask "update archive?" Say yes... That's it. Save this answer.


2 Answers

It doesn't work because you are trying to launch the jar from another folder: spring boot looks for files/folder relative your current folder.

You can:

1) copy application.properties either in ./ or ./config/, relative to your current folder.

2) Or specify -Dspring.config.location:

$ java -Dspring.config.location=target/application.properties -jar target/myproject-0.0.1-SNAPSHOT.jar
like image 200
alexbt Avatar answered Oct 14 '22 23:10

alexbt


You spelt config as conig, should work if you spell it right.

like image 20
Magnus Avatar answered Oct 14 '22 22:10

Magnus