我是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文件中读取一个配置文件配置变量,该变量仅指示我要使用的文件集(测试,证书或产品)。

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

有两个问题:


使用Maven POM可以通过非骇客的方式实现吗?是否有Maven文档,书籍或参考资料涉及应用程序构建的这一阶段?
是否有更明智的方式将我的应用程序构建概要文件定向到特定的执行环境(测试,产品等)?我想这可能更值得商debate,但是如果有一种更简单,更优雅的方式来解决这一问题,我就会:P。


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

最佳答案

我首先建议将您的测试环境配置放入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>


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


artifactId-version-test.war
artifactId-version-qa.war
artifactId-version-production.war

10-08 04:51