【问题标题】:Specifying runtime configuration parameters to a Maven built Web Application为 Maven 构建的 Web 应用程序指定运行时配置参数
【发布时间】:2012-06-16 16:43:43
【问题描述】:

我是 Maven 的新手,我想知道是否有一种合理的方法可以在构建时为 Web 应用程序指定配置信息参数。

我的意思如下。使用 Ant,我通常会创建一个文件夹(例如 config-params),然后在该文件夹中放置一些属性文件或任何其他必要的文件,并为我的应用程序运行的环境设置正确的设置。

例如:

- test.jdbc.properties
- cert.jdbc.properties
- prod.jdbc.properties
- test.log4j.properties
- test.myapplication.properties
- test.web.xml

... ad nauseum

然后,在我的 ant 构建脚本中,我只是从 project.properties 文件中读取了一个配置文件配置变量,该文件仅指示我想使用的文件集(test、certprod)。

这样,当我为已知环境构建应用程序时,我不必担心检查每个可能的框架配置参数。

那么两个问题:

  1. 这是否可以通过 Maven POM 以非 hackish 的方式实现?是否有涉及应用程序构建这一阶段的 Maven 文档、书籍或参考资料?

  2. 是否有更明智的方法将我的应用程序构建配置文件定位到特定的执行环境(测试、产品等)?我想这可能更容易引起争论,但如果有更简单、更优雅的方式来处理这个问题,我会全力以赴:P.

感谢您提供的任何帮助。干杯!

【问题讨论】:

    标签: java maven deployment configuration war


    【解决方案1】:

    我建议的第一件事是将测试环境的配置放入 src/test/resources 中,Maven 可以自动将其用于测试。生产配置应该位于 src/main/resources ,这意味着在打包和发布过程中将自动使用生产配置。 其他配置目标(如 cert)意味着您需要为不同的环境再次构建应用程序。

    您是否通过为不同环境生成工件的 CI 环境构建?是的,仅仅基于为每个环境分别构建应用程序的需要,基于配置文件的解决方案并不是一个好的选择。最好只构建一次具有代表不同环境的工件。 让我们从这样的事情开始:

    .
    |-- pom.xml
    `-- src
        |-- main
        |   |-- java
        |   |-- resources
        |   |-- environment
        |   |   |-- test
        |   |   |   `-- database.properties
        |   |   |-- qa
        |   |   |   `-- database.properties
        |   |   `-- production
        |   |       `-- database.properties
        |   `-- webapp
    

    对于每个环境,您都有一个文件夹,其中包含不同的配置部分(在本例中为属性文件)。 您需要一个 maven-assembly-plugin 的配置文件(适用于每个环境),如下所示:

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    
      <id>test</id>
      <formats>
        <format>war</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <dependencySets>
        <dependencySet>
          <unpack>true</unpack>
          <useProjectArtifact>true</useProjectArtifact>
        </dependencySet>
      </dependencySets>
      <fileSets>
        <fileSet>
          <outputDirectory>WEB-INF</outputDirectory>
          <directory>${basedir}/src/main/environment/test/</directory>
          <includes>
            <include>**</include>
          </includes>
        </fileSet>
      </fileSets>
    </assembly>
    

    当然还有这样的 maven-assembly-plugin 配置:

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>test</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
          <execution>
            <id>qa</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/qa.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
          <execution>
            <id>production</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
    

    结果是只有一个构建,它为每个环境生成一个工件,如下所示:

    1. artifactId-version-test.war

    2. artifactId-version-qa.war

    3. artifactId-version-production.war

    【讨论】:

    【解决方案2】:

    了解Maven build profiles

    基本概念是在单个POM 文件中创建多个构建配置,然后使用

    mvn -P <profile-name> <targets>
    

    您还可以通过定义 &lt;activation/&gt; 规则让 maven 决定使用哪个配置文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-31
      • 2016-04-04
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 2010-09-16
      • 1970-01-01
      相关资源
      最近更新 更多