Part of my webapp involves uploading image files. On the production server, the files will need to be written to /somepath_on_production_server/images. For local development, I want to write the files to /some_different_path/images.
What’s the best way to handle these configuration differences?
One important requirement is this: I don’t want to have to mess with the production server at all, I just want to be able to deploy a war file and have it work. So I don’t want to use any technique which will require me to mess with the environment variables/classpath/etc. on the production machine. I’m fine with setting those on my local machine though.
I’m imaginine two possible general approaches:
- loading a special “dev” configuration file at runtime if certain conditions are met (environment variable/classpath/etc)
- flipping a switch during the build process (maven profiles maybe?)
Simple things like a
Stringcan be declared as environment entries in theweb.xmland obtained via JNDI. Below, an example with anenv-entrynamed “imagePath”.To access the properties from your Java code, do a JNDI lookup:
This is typically done in an old fashioned
ServiceLocatorwhere you would cache the value for a given key.Another option would be to use a properties files.
And the maven way to deal with multiple environments typically involves profiles and filtering (either of a properties file or even the
web.xml).Resources