0

Is there a way I could use Java -D arguments in application .properties file, such that when I load my properties, on a specific key-value, part of value is replaced with -D argument.

I have an app.properties file like this

example.file=file:/Users/XXXX/assets/tables/overage/test1.xls
example.template.file=file:/Users/XXXX/assets/tables/overage/test2.xls
example.mapping.file=file:/Users/XXXX/assets/tables/overage/test3.properties

I want to extract "file:/Users/XXXX/assets/tables/" to a -D argument to make my properties file independent of the server environment.

Example:

Jvm arg:

-Dfilepath="file:/Users/XXXX/assets/tables/"

I tried something like this

example.file={filepath}overage/test1.xls

so when I call below code and with some mechanism it should replace file path with environment variable.

Properties prop = new Properties();
FileInputStream fis = new FileInputStream(new File(app.properties))
prop.load(fis);

4 Answers 4

2

If you want to have a simpler properties file, you can do something like this:

example.file.folder=file:/Users/XXXX/assets/tables/overage
example.file=${example.file.folder}/test1.xls
example.template.file=${example.file.folder}/test2.xls
example.mapping.file=${example.file.folder}/test3.properties

This way it is more readable and maintainable.

Sign up to request clarification or add additional context in comments.

Comments

0

I would recommend you to use different properties file for each environment because it has its own benefits as below:

Benefit of having seperate properties file for each environment ?

  1. clean properties file per each environment.
  2. segregation of duties (developer team is not required to know production files location)
  3. Easy maintainence i.e. you can duplication the entire file if you wish to have an new super dev environment.

How to do this ?

configure the file location as an environment variable on the environment. The property used to set the environment needs to be loaded to read the correct folder location.

Follow below steps:

  1. Configure environment variable:
set app.properties.file = "your.file.location";
  1. In java class configure the Properties configuration:
PropertiesConfiguration propConfig = new PropertiesConfiguration(environment.getProperty("app.properties.file"));
          final FileChangedReloadingStrategy reloadingStrategy = new FileChangedReloadingStrategy();
          reloadingStrategy.setRefreshDelay(10000);
          config.setReloadingStrategy(reloadingStrategy);
  1. for getting properties value use:
String value = propConfig.getProperty("your.property.name");

1 Comment

these are not typical application properties files, these are readable and writable files specific to business, We wanted to export to a different environment, we dont want to worry about manual edits of file path
0

You can use separate properties file for each environment. The original properties would get overridden by environment specific properties.

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

Moreover, -D argument overrides everything. So if you provide a property using -D argument that property value would be used in your application.

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

Comments

0

I believe you're looking for something that is described pretty well here: http://www.summa.com/blog/2009/04/20/6-tips-for-managing-property-files-with-spring

If you want system variables to override values defined in your property files, you should use:

<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>

i.e.

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>

  <property name="locations">
    <list>
      <value>classpath:application.properties</value>
    </list>
  </property>

</bean>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.