【问题标题】:Java Build Enviroment configuration in Classes or TextFiles?类或文本文件中的 Java 构建环境配置?
【发布时间】:2014-05-10 23:34:33
【问题描述】:

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

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

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

如果我在 Produktion 上部署 Webapp,那么我使用 prod-properties.jar 进行部署。对于本地工作区,我使用 test-properties.jar

我的问题:将环境属性与属性文件或Java类一起使用会更好吗? (性能?不需要 FileInputStream ......糟糕的代码风格?)

感谢您的回答:)

【问题讨论】:

    标签: java properties build maven-assembly-plugin


    【解决方案1】:

    我建议使用属性文件,Java 类应该在环境之间保持不变,并且 maven 已经内置了资源过滤。

    在我正在处理的多模块项目中,我有一个动态设置的属性文件,具体取决于我使用的配置文件/环境:

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

    REGISTRATION_URL=${REGISTRATION_URL}
    

    然后我可以在 maven 配置文件中指定注册 URL:

    <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 test-environment)构建项目时, environment.properties 中的上述属性将设置如下:

    REGISTRATION_URL=http://test-env/register
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多