Search code examples
javajbossweblogic

Where to put deployment specific properties in Java


For building web applications in Java, I have Tomcat as the integrated server in Eclipse for development. Some of the target systems are using JBoss while others are using WebLogic 12C.

Application related settings are kept in app.properties file and loaded at runtime. Same as given here .

Where should I keep deployment specific properties? i.e. I want to keep items such as "Site Title", "Company Name", "Database User", blah blah which would be different for each deployment. As an example, if I deploy the same app for two customers, I should be able to change the "Company Name".

When using .properties files, I'd have to keep separate branches of the same code and recompile for each deployment.

What is the recommended pratice/method for doing so?


Solution

  • The standard practice is the use an external property file loaded from the file system that you can customize for each environment. Using Spring boot as an example (https://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/boot-features-external-config.html), it looks for application.properties files in different locations in the following order...

    1. A /config subdir of the current directory.
    2. The current directory
    3. A classpath /config package
    4. The classpath root

    When packaging a spring boot app as a war file for deploying in a servlet container like weblogic or jboss, you can specify where to look for the externalized property file using a servlet context parameter...

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    
      <context-param>
        <param-name>spring.config.additional-location</param-name>
        <param-value>file:./config/sparq/dms-oncall-messaging.yml</param-value>
      </context-param>
      
    
    </web-app>
    

    If you're not using spring boot you can still take a similar approach for loading externalized property files to customize configuration for each environment. So you can provide a default property file packaged up in your jar/war file that can be overridden by an externalized property file.