为dev / QA / prod配置Java EE 6 [英] Configure Java EE 6 for dev/QA/prod

查看:253
本文介绍了为dev / QA / prod配置Java EE 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java EE 6应用程序,我使用Maven构建,在NetBeans 7中的代码,并部署在GlassFish 3.1.2上。在我完成之后,我发现自己正在部署演示版本。



问题是,我没有任何死机容易的方法来构建不同的环境,如dev,QA ,demo,prod等。对于一些东西,我一直在使用一个Java类,一堆静态getter基于环境常量的值返回值。但这不能帮我有条件地设置




  • javax.faces.PROJECT_STAGE(web.xml)

  • 邮件伺服器(glassfish-resources.xml)

  • JPA记录层级(persistence.xml) )



可能还有其他一些我现在不能想到的分散在XML文件中的东西。



有没有任何方法来定义这些配置文件的多个版本,只是在构建时设置一个标志来选择环境,而在没有指定环境的情况下默认为dev。

解决方案

您可以使用 maven 来实现。特别是使用资源过滤



首先,您可以定义配置文件列表:

 < profiles& 
< profile>
< id> dev< / id>
< properties>
< env> development< / env>
< / properties>
< activation>
< activeByDefault> true< / activeByDefault> <默认情况下使用dev配置文件 - >
< / activation>
< / profile>
< profile>
< id> prod< / id>
< properties>
< env> production< / env>
< / properties>
< / profile>
< / profiles>

然后您需要过滤的资源:

 < build> 
< outputDirectory> $ {basedir} / src / main / webapp / WEB-INF / classes< / outputDirectory>
< filters>
< filter> src / main / filters / filter - $ {env} .properties< / filter> <! - $ {env} default todevelopment - >
< / filters>
< resources>
< resource>
< directory> src / main / resources< / directory>
< includes>
< include> ** / *。xml< / include>
< include> ** / *。properties< / include>
< / includes>
< filtering> true< / filtering>
< / resource>
< / resources>
< / build>

然后你的自定义属性基于 src / main / filters 目录:



filter-development.properties

 #profile for developer 
db.driver = org.hsqldb.jdbcDriver
db.url = jdbc:hsqldb:mem:web





filter-production.properties p>

 #profile for production 
db.driver = com.mysql.jdbc.Driver
db.url = jdbc :mysql:// localhost:3306 / web?createDatabaseIfNotExist = true

package war使用 mvn clean package -Pprod 命令。



在这里,您可以看到在maven中使用配置文件的示例项目。


I have a Java EE 6 app that I build with Maven, code in NetBeans 7 and deploy on GlassFish 3.1.2. As I near completion, I find myself deploying demo builds.

The problem is that I don't have any dead easy way to build for different environment such as dev, QA, demo, prod, etc. For some stuff, I've been using a Java class with a bunch of static getters that return values based on the value of an environment constant. But this doesn't help me with conditionally setting

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

and probably a number of other things I can't think about now that are scattered across XML files.

Is there any way to define multiple versions of these configuration files and just set a flag at build time to select the environment, while defaulting to dev when no environment is specified? Is there a way I could make Maven work for me in this instance?

解决方案

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.

这篇关于为dev / QA / prod配置Java EE 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆