Configure Java EE 6 for dev/QA/prod

こ雲淡風輕ζ 提交于 2019-12-03 04:01:35
Pau Kiat Wee

You can use maven to achieve that. Especially using resource filtering.

First, you can define list of profiles:

  <profiles>
    <profile>
      <id>dev</id>
      <properties>
        <env>development</env>
      </properties>
      <activation>
        <activeByDefault>true</activeByDefault> <!-- use dev profile by default -->
      </activation>
    </profile>
    <profile>
      <id>prod</id>
      <properties>
        <env>production</env>
      </properties>
    </profile>
  </profiles>

Then the resources that you need to filter:

  <build>
    <outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory>
    <filters>
      <filter>src/main/filters/filter-${env}.properties</filter> <!-- ${env} default to "development" -->
    </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 properties based on profiles in 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 production profile, you can package war using mvn clean package -Pprod command.

Here you can see the sample project that use profile in maven.

This is not direct response to question. This explain diff strategy to manage env properties One other way to manage properties for diff env is using the database to store the properties. This way you have only need to manage the config of the DB. Based on which DB you are pointing you can load the properties from that DB. If you are using spring than spring provides PropertyPlaceholderConfigurer which can initialize the properties from DB. This approach allows you to change the property value without doing a build.

This approach is useful if you want to promote the artifact tested by QA\Testing team. In this case DB configuration will not be part of artifact generated by build process.

If you need to configure web.xml check this how-to: https://community.jboss.org/docs/DOC-19076

It uses same method (resource filtering) as described in another answers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!