You can use maven to achieve this. Especially using resource filtering .
First you can define a list of profiles:
<profiles> <profile> <id>dev</id> <properties> <env>development</env> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>prod</id> <properties> <env>production</env> </properties> </profile> </profiles>
Then the resources that need to be filtered:
<build> <outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory> <filters> <filter>src/main/filters/filter-${env}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
And then your custom profile-based properties in the src/main/filters directory:
filter-development.properties
# profile for developer db.driver=org.hsqldb.jdbcDriver db.url=jdbc:hsqldb:mem:web
and
filter-production.properties
# profile for production db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/web?createDatabaseIfNotExist=true
to use the production profile, you can pack the war with the mvn clean package -Pprod .
Here you can see an example project that uses a profile in maven.
source share