1

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?

iamrajshah
  • 963
  • 1
  • 11
  • 23
cuteBrick
  • 75
  • 2
  • 8
  • change this @PropertySource("classpath:" + SpringConfiguration.EXTERNALIZED_PROPERTIES) as @PropertySource("file:" + {location}/propertiesfilename) it will give you access external side also you have to give exactly {location} via JVM parameter or spring parameter – Mithat Konuk Jan 18 '19 at 12:10

6 Answers6

3

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.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
3

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'
2

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.

cuteBrick
  • 75
  • 2
  • 8
1

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

Nadir
  • 1,369
  • 1
  • 15
  • 28
0

@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)

Volo Myhal
  • 74
  • 5
-1

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.

iamrajshah
  • 963
  • 1
  • 11
  • 23