cuteBrick
cuteBrick

Reputation: 121

Spring Boot External properties outside of jar

I am creating a Spring Boot application and i faced this problem.
I have my application.properties in recource folder but i also need external.properties file outside of jar to configure properties such like:
name,
password,
etc.
I want to have external.properties file outside of jar and inside resources folder for testing while developing.
I tried creating configuration file like this:

@Configuration
 @PropertySource("classpath:" + SpringConfiguration.EXTERNALIZED_PROPERTIES)
 @PropertySource(value = "file:./" + 
 SpringConfiguration.EXTERNALIZED_PROPERTIES, ignoreResourceNotFound = true)
 public class SpringConfiguration {
       static final String EXTERNALIZED_PROPERTIES = "external.properties";
 }

But it still reads properties from resource folder. How can i make it read from outside of jar?

Upvotes: 1

Views: 7673

Answers (6)

Volo Myhal
Volo Myhal

Reputation: 144

@cuteBrick but this way, you need to copy/edit the whole application.properties. When you need to modify subset only, utilize Spring Profiles for it: place application-dev.properties and/or application-prod.properties nearby JAR file, and override only necessary properties (urls, secrets) there.

Then, run application specifying Spring Profile: -Dspring.profiles.active=dev (BTW the default Spring Profile is default)

Upvotes: 0

cuteBrick
cuteBrick

Reputation: 121

The solution was to delete external.properties and configuration file. And instead of using it put all properties to application.properties. And put application.properties to folder with jar. Spring automatically prioritizes this property file over the property file inside jar.

Upvotes: 2

Victor Dovgaliuc
Victor Dovgaliuc

Reputation: 168

Similar answer of Maciej Kowalski.

@PropertySources({
@PropertySource(value = {"classpath:application.properties"}, ignoreResourceNotFound = true),
@PropertySource(value = {"file:${external.config.location}/application.properties"}, ignoreResourceNotFound = true)

})

Assuming in folder "/home/me/configs" you have "application.properties" file.

Run app with custom folder:

java -jar app.jar --external.config.location="/home/me/configs"

or

java -jar app.jar --external.config.location="C:\\users\\your_user\\configs"

Additionally you could export it as environment variable (unix)

export JAVA_OPTS='-Dexternal_config_location=/home/me/configs'

Upvotes: 3

Maciej Kowalski
Maciej Kowalski

Reputation: 26502

Try specifying an absolute system path as the value of file: attribute.

Optionally I would advise setting that absolute path first as an ENV variable and then using that variable in the file::

@PropertySource("file:${EXTERNAL_RESOURCE_DIR}/application.properties") 

So that when that directory changes you wont need to alter your code.

Upvotes: 3

Nadir
Nadir

Reputation: 1379

If you want different props for development and production, use application-dev.properties and application-prod.properties and set proper spring profile on startup. If you need to override any property from the jar just add -Dmyproperty=myvalue to startup command

Upvotes: 1

iamrajshah
iamrajshah

Reputation: 979

If you need to add external properties you can specify that in application.properties only.

For example: myapplication.username='john'

From Spring boot code you can access it like:

 @Autowired  
 private Environment env;

//To access it
 String username = env.getProperty("myapplication.username");  

OR

@Value("$myapplication.username")

I don't think there is any need to have external file if your requirement is as you mention in question.

Upvotes: -1

Related Questions