Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass deployment dependant parameters to webapp

In projects I work(ed) on, deployment parameters - such as storage path or DB login - are usually given through a parameter file, which is stored in the war file.

I find that unsuitable because those values needs to be changed each time the webapp is packaged for a different deployment (dev vs prod, change of executing computer). The source code being versioned, this makes it even more bothering.

Is there some better option to pass parameters such as listed above?

By better, I mean:

  • practical: simple to setup, change and explain to others
  • separated from the war
  • as independent as possible to the web container (if dependent, I'm using tomcat in prod)

Edit

I chose the answer of @aksappy to reward the work done in the answer and because it provided several methods using standard tools. However, depending on the context I could go for any other solutions:

  • method of @Necreaux has best simplicity
  • method of @Luiggi Mendoza has a good design and is still simple
  • method of @OldCurmudgeon would be a really good one if the code covered other cases.
like image 556
Juh_ Avatar asked May 26 '15 13:05

Juh_


Video Answer


1 Answers

You can use a multitude of things based on your environment. Here are somethings which may be considered

  1. Use datasources The datasources defined in the server context removes the hard wired dependency of managing db configurations and connection pool from the web application. In Tomcat, this can be done as below in the context.xml
<Context>
       ...
      <Resource name="jdbc/EmployeeDB" auth="Container"
                 type="javax.sql.DataSource"
          description="Employees Database for HR Applications"/>
      </Context>
  1. Use Contexts

You can configure named values that will be made visible to the web application as environment entry resources, by nesting entries inside this element. For example, you can create an environment entry like this: (Source here). This can be set as context parameters or environment entries. They are equivalent to the entries made in a web.xml or a properties file except that they are available from the server's context.

  1. Use database configurations and load those configuration at ServletContextListener

Another approach which I tend to follow is to create a relational schema of properties in a database. Instead of loading the properties file during server startup, load the properties from the database during start up.

public class ContextInitialize implements ServletContextListener {
  private static Properties props;
  public void contextInitialized(ServletContextEvent servletContextEvent) {
     // connect to DB
     // Load all the key values pairs as required
     //put this into a Properties object, or create a hashtable, hashmap ..
  }
  //Getter
  public String getProperty(String key){
     // get value of key
  }
  //Setter
  public void setProperty(String key, String value){
     // set value to a key
  }
}

Note: above is just an example.

  1. Use environment variables or classpath variables

Use classpath / path variables in Environment variables and use System.getenv() in your java code to get these values as necessary.

like image 181
aksappy Avatar answered Sep 21 '22 16:09

aksappy