I have a war file to be deployed in Glassfish installed in Linux and Windows OS. I am trying to externalize the config.properties file so it can be changed without re-deploying war file. I can make it by referencing 'absolute path' such as C:\Temp\config.properties; but of course it does not work in Linux due to a different file system.
I was wondering how it could be done to work in both linux and Windows environments ? Also I would like to hear any best practices or approaches related to deployments on different OSs.
The properties file should always be in a fixed location relative to your WAR. Use the java.nio.file.Path class to refer to it. You can use Paths.get() to get such a path. Use one argument per path segment, don't use slashes or backslashes. The paths that are returned are file system agnostic.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Stephan van Hulst wrote:The properties file should always be in a fixed location relative to your WAR.
Shouldn't you use ServletContext.getRealPath? That's the official way to get a path to something inside your WAR. You can then use Path to go up as far as you like. That can still be troublesome because the container might deploy the files to some temp folder.
WebLogic has an environment variable (DOMAIN_HOME) that indicates where its domain home is, and that's what we use at work to determine a proper base location. JBoss has some system variables that can be used in a similar way (e.g. jboss.server.config.dir). These work on both Windows and Linux. Does GlassFish also have something like that?
Not a best practice, but a best hack - call System.getProperty("my.external.config.file") from the code that reads the property file. Then set that property on your linux and Windows servers to the right location.
The reason it isn't a best practice is relative paths are better. But not always possible...