Karthik Suresh
Karthik Suresh

Reputation: 407

Externalize a property file which is inside a jar file

I have jar with application.properties file as below-

BASE_ENVIRONMENT = http://localhost

This is my utility class to read the property file

        Properties prop = new Properties();
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream in = loader.getResourceAsStream(fileName + ".properties");

the property file is in <default package>

This jar is used as a dependent jar in my web application which is deployed in tomcat server.

But i need to change the BASE_ENVIRONMENT for production environment.

Is there a way i can externalize this property file value?

Upvotes: 0

Views: 126

Answers (1)

Essex Boy
Essex Boy

Reputation: 7968

You could add a system parameter to act as a profile:

 // default to DEV
 String profile = "dev"
 if (System.getProperty("ENV") != null) {
    profile = System.getProperty("ENV");
 }
 Properties prop = new Properties();
 ClassLoader loader = Thread.currentThread().getContextClassLoader();
 InputStream in = loader.getResourceAsStream(fileName + "_" + profile + ".properties");

Then you would start your app with

   .... -DENV=prod

and the file like config_prod.properties would be found in the classpath, the default would be config_dev.properties.

Upvotes: 1

Related Questions