2014-03-31 80 views
0

我使用maven assembly插件为环境测试和生产内部的属性(test1.properties和test2.properties)构建了不同的jar(test-properties.jar和prod-properties.jar)。现在我将properties文件(test1.properties和test2.properties)更改为Java文件(Test1.java和Test2.java)......编译它们并再次运行程序集插件。 所以我有两个jar文件,里面有类,它适用于不同的环境。类或TextFiles中的Java Build Enviroment配置?

- >测试properties.jar(Test1.class,Test2.class)

- > PROD-properties.jar(Test1.class,Test2.class)

如果我上部署web应用程序Produktion,然后我使用prod-properties.jar进行部署。对于本地工作区,我使用test-properties.jar

我的问题:在属性文件或Java类中使用Enviroment属性更好吗? (性能?不需要FileInputStream中......不好的代码风格?)

谢谢你的答案:)

回答

0

我会建议使用属性文件,Java类应保持环境和Maven之间的常数已建成资源过滤。

在多模块项目,我是做什么工作的我有一个是动态设置的属性文件,根据不同的配置文件/环境使用:

的动态属性指向注册网址为我的项目,并且是特定于环境的。它在属性文件中定义为如下:

REGISTRATION_URL=${REGISTRATION_URL} 

然后我就可以指定一个maven配置文件中的注册网址:

<profile> 
    <id>test-environment</id> 
    <activation> 
     <property> 
      <name>environment</name> 
      <value>test</value> 
     </property> 
    </activation> 
    <properties> 
     <REGISTRATION_URL>http://test-env/register</REGISTRATION_URL> 
    </properties> 
</profile> 

从这里,也有应用此配置文件特定的属性两种方式:

  1. 应用资源滤波模块的POM的构建部分包含属性文件:

    <resources> 
        <resource> 
         <directory>src/main/resources</directory> 
         <filtering>true</filtering> 
        </resource> 
    </resources> 
    
  2. 在程序集中的moduleSet或fileSet中指定资源过滤。在我来说,我使用moduleSet:

    <moduleSet> 
        <useAllReactorProjects>true</useAllReactorProjects> 
        <includes> 
         <include>${project.groupId}:core-module</include> 
        </includes> 
        <sources> 
         <fileSets> 
          <fileSet> 
           <directory>src/main/resources/META-INF</directory> 
           <outputDirectory>conf</outputDirectory> 
           <includes> 
            <include>environment.properties</include> 
           </includes> 
           <filtered>true</filtered> 
          </fileSet> 
         </fileSets> 
         <includeModuleDirectory>false</includeModuleDirectory> 
        </sources> 
    </moduleSet> 
    

当项目使用该配置文件(-P测试环境),建于environment.properties上述属性将被设置为以下几点:

REGISTRATION_URL=http://test-env/register