V
V
Vit2016-10-25 09:40:05
Java
Vit, 2016-10-25 09:40:05

How is it customary to configure the environment for a Java web application?

As part of learning Java, I am writing a small web application without using frameworks, only Servlets + Freemarker. I build the application with Maven and deploy it in Tomcat.
I would like to have a variable somewhere in the config that indicates the mode in which the application is launched (production/development/test), and so that I can read the value of this variable in the code. I can add such parameter in web.xml as context-param. But it’s not clear to me how it can be done so that “production” is conveniently and automatically substituted into it when I actually deploy the application to the production server.
As an example, how I did it in PHP: there was a config in which the values ​​of some parameters (db password, environment, etc.) were not explicitly specified. They were taken from another file that I added to git ignore. On the working machine, it contained the settings for the development environment, and when deployed to the production server, the settings for production were substituted into it accordingly.
Is it possible to do something similar in my case in Java? And how is it generally accepted in the Java world to manage this? Thank you.

UPD: I also had an idea to write a Listener that, when the application starts, reads some config (property-file), then, if there is another config along a certain path, then it reads it and overrides the corresponding properties from the first config. Thus, I can place the parameters for production in the second config and not store it in the git repository. Is this approach used, or is there something more sane?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2016-10-25
@fornit1917

Yes, there is actually nothing special invented. Specifically, in maven, you can resolve this with profiles . Roughly speaking, profiles will allow maven to hint which settings file to put in the build.

P
protven, 2016-10-25
@protven

Pass the path to the configuration file through system variables, for example. Or, in your container (Tomcat), you set up data-source, and the application reads the configs from the database at startup. Lots of options.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question