Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - set location of application.properties in war file

I'm trying to build a war file that knows to look in a specific path for a spring boot application.properties. For example, when running in a linux environment,

I'd like my maven build to some how build that app so that it knows to look at the following location:

/etc/projects/application.properties

To be clear, I want to avoid setting system or command line properties. I want to wire into spring the location of the file and have application server and os be unaware of how spring does that. Is this possible?

like image 567
fansonly Avatar asked Mar 15 '23 23:03

fansonly


2 Answers

You can pass -Dspring.config.location=/etc/projects/application.properties as part of the JAVA_OPTS to Tomcat.

like image 70
Ekkelenkamp Avatar answered Mar 17 '23 11:03

Ekkelenkamp


//edit:

I was sure that above will work, but..:)

according to documentation (here) you can create config directory next to your war/jar file, and create application.properties file there. It will be loaded. Ive tested that by cloning this repo. Edit HelloController class in initial project:

@RestController
public class HelloController {

@Value("${test.property}")
private String testProperty;

@RequestMapping("/")
public String index() {
    return "Greetings from Spring Boot! Test property value="+testProperty;
}
}

build jar with maven:

mvn clean package

create config directory, add our property there, run app:

cd target
mkdir config
echo "test.property=Value from external config" >> config/application.properties 
java -jar gs-spring-boot-0.1.0.jar

goto localhost:8080 - value from external property file should be there :)

Ofcourse if you really have to store your files on /etc/projects/ then you can create symlinks inside config dir.

like image 43
hi_my_name_is Avatar answered Mar 17 '23 13:03

hi_my_name_is