Configure Java EE 6 for dev / QA / prod

I have a Java EE 6 application that I create using Maven, code in NetBeans 7, and deployment on GlassFish 3.1.2. As it nears completion, I find myself deploying demo builds.

The problem is that I have no easy way to build for different environments like dev, QA, demo, prod, etc. For some things, I use a Java class with a bunch of static ones that return values ​​based on the value of an environment constant. But that doesn't help me with conditional tuning

  • javax.faces.PROJECT_STAGE (web.xml)
  • database credentials (glassfish-resources.xml)
  • mail servers (glassfish-resources.xml)
  • JPA Logging Level (persistence.xml)

and perhaps a number of other things that I can’t think of right now that are scattered across XML files.

Is there a way to identify multiple versions of these configuration files and just set the flag during build to select the environment, while the default is for dev if no environment is specified? Is there a way to get Maven to work for me in this case?

+2
source share
3 answers

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> <!-- use dev profile by default --> </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> <!-- ${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 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.

+8
source

This is not a direct answer to the question. This explains the diff strategy for managing env properties. Another way to manage properties for diff env is to use a database to store properties. Thus, you only need to manage the database configuration. Based on which database you specify, you can load properties from this database. If you use spring, than spring provides a PropertyPlaceholderConfigurer that can initialize properties from the database. This approach allows you to change the value of a property without performing an assembly.

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

0
source

If you need to configure web.xml, follow these steps: https://community.jboss.org/docs/DOC-19076

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

0
source

Source: https://habr.com/ru/post/1203977/


All Articles