My friend has an interesting problem: His application will be deployed multiple times on a single Java EE server, but every instance must use different database. There are multiple ways how to do that. His application is a Maven application, so my first idea was to use Maven profiles:
Edit: How to do the same on Tomcat without touching the WAR file
Put into pom.xml:
<build> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> <profiles> <profile> <id>app1</id> <build> <resources> <resource> <directory>src/main/resources-app1</directory> </resource> </resources> </build> </profile> <profile> <id>app2</id> <build> <resources> <resource> <directory>src/main/resources-app2</directory> </resource> </resources> </build> </profile> </profiles>
And create files src/main/resources-app1/db.properties
, src/main/resources-app2/db.properties
and inside put your database configuration.
When you run mvn clean package -P app1
, you will have inside WEB-INF/classes (if it's a web application) db.properties from src/main/resources-app1/
And when you run mvn clean package -P app2
, you will have inside WEB-INF/classes (if it's a web application) db.properties from src/main/resources-app2/
Job done :-)
Made better & faster using https://www.yourkit.com/ Java Profiler