Single web application on one Java EE server with different datasouces (Maven profiles)

22.04.2016 13:51:10

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 :-)




© Jiri Pinkas | this project on GitHub | related: JavaVids | Maven Central Repository Search | Java školení | Top SQL Blogs | monitored using: sitemonitoring

Top Java Blogs is a Java blog aggregator (with English-written blogs only) focused on Java SE, Java EE, Spring Framework and Hibernate.