Multiple .properties files in a Java project

I would like to find a suitable way to switch between multiple .propertiesfiles for different deployment configurations.

My initial inclination is to create a separate file selector.properties, the only property of which is used to determine the correct file:

properties.file=deploymentConfiguration1.properties

... for one deployment or:

properties.file=deploymentConfiguration2.properties

... for the next deployment.

Another developer of my team has a class ApplicationPropertiesin which:

private static final String PROP_FILE="someFileName.properties";

... is a means to this. However, I want to switch property files without rebuilding! Thank you in advance for your entry.

EDIT: Maybe I should have been clearer at first, but this is for a set of web services packaged as AAR. I just send it to the web server and let Tomcat and Axis2 do their job.

So, I do not think that some answers with command line options will work in this context.

0
source share
3 answers

They have several properties files, one on env, for example:

application-dev.properties
application-test.properties
application-prod.properties

Run the application with env as a system property;

java -Denv=test

Download your properties from the appropriate file;

String props = "application-" + System.getProperty("env") + ".properties";

Note that I usually discouraged this in favor of a fixed-name property file, where the file is generated during assembly.

Edit:
-, env (web.xml)

<env-entry>
  <env-entry-name>myEnv</env-entry-name>
  <env-entry-type>java.lang.String</env-entry-type>
  <env-entry-value>test</env-entry-value>
</env-entry>

- :

Context ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env");
String env = (String)envCtx.lookup("myEnv");
String props = "application-" + env + ".properties";
+5

, !

java Program -config deploymentConfiguration2.properties
+2

"manager manager" . master vm jvm , . , , .

0

Source: https://habr.com/ru/post/1794182/


All Articles