10

I have this requirement that my webapp needs to read from a properties file outside the Tomcat webapps directory, for example from $CATALINA_HOME/properties/myapp.properties. I have tried several things but so far no luck

Fernando
  • 103
  • 1
  • 1
  • 6
  • A file is a file; give it a path and read it. – Dave Newton Mar 10 '13 at 03:32
  • Yeah this is easy to do. It's not senseless, there are reasons for it. – Rob Mar 10 '13 at 03:40
  • I think the file will be shared by multiple projects, I post an answer, hope it can give you the hint. – OQJF Mar 10 '13 at 03:46
  • 1
    This is required for configuration, when we deploy the same war to different boxes (dev, qa, etc). We use $TOMCAT/conf/xxx.properties files, and have a jar (with classes to read the named property files and the properties themselves) that different applications use. – Manidip Sengupta Mar 10 '13 at 04:07

4 Answers4

19

There are multiple approaches .

  1. Use an Environmental variable
  2. Use a System Property
  3. Set it as a Application Context Param in Web.xml

Heres a sample ,that showsOption 1 and Option 2

try {

    //Use Any Environmental Variable , here i have used CATALINA_HOME
    String propertyHome = System.getenv("CATALINA_HOME");           
    if(null == propertyHome){

        //This is a system property that is  passed
        // using the -D option in the Tomcat startup script
        propertyHome  =  System.getProperty("PROPERTY_HOME");
    }


    String filePath= propertyHome+"/properties/myapp.properties";

    Properties property = new Properties();         
    property.load(SystemTest.class.getClassLoader().getResourceAsStream(filePath));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Sudhakar
  • 4,663
  • 1
  • 29
  • 42
  • Thank you! I used your second approach with System Property "catalina.home" This is exactly what I needed! – Fernando Mar 10 '13 at 16:38
2

I prefer the option to configure via Tomcat context descriptors.

These XML-documents are placed into the folder tomcat/conf/Catalina/<host>/*.xml

This refers to: Externalizing Tomcat webapp config from .war file

Community
  • 1
  • 1
Hartmut Pfarr
  • 3,994
  • 4
  • 32
  • 38
1

In our project that we config the path of file as vm arguments which will stored in the tomcat properties and in the code, use System.getProperty(the parameter you config) to get the path and read the properties file, then get the results.

OQJF
  • 1,330
  • 9
  • 12
  • Yeah, but in case Tomcat and if I need read file somewhere in class .... implements ServletContextListener {} I got WAR related path ! Is it possible something using for example SPRING to get path outside of WAR ? file:d:/my/path/toFile.log – abn_java Mar 05 '15 at 09:04
0

We have put the path of webapp-config directory as follows:

/var/lib/tomcat7/webapps/webapp/WEB-INF/classes/system-properties-location.properties

where webapp= name of your webapp file

Here are the contents of system-properties-location.properties file:

location=/var/lib/tomcat7/webapp-configs/webapp

It points to an external location to pull environment specific properties from. This way we can deploy same war in dev , staging and prod without modifying war contents.