Maven项目中的本地和生产配置文件

时间:2013-05-21 12:40:19

标签: maven

我开始使用maven,我有一个关于为部署选择资源的问题。我有一个本地和生产属性目录,我在部署时想要选择其中一个并放在适当的位置。我的项目结构是这样的

-
| -/src
|  | -/main
|  |  | -/java
|  |  | -/resources
|  |  | -/webapp
|  |  |  | -/WEB-INF
| -/config
|  | -/local
|  |  | -/properties
|  |  |  | -config.properties
|  | -/prod
|  |  | -/properties
|  |  |  | -config.properties
| -/WEB-INF

因此,当我部署它时,应选择适当的属性文件夹并将其放入/WEB-INF/。我怎样才能做到这一点?

如果config目录位于src/main/resources内,则附注,但也相关吗?

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作(您已经拥有):

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

除了上述内容之外,您还需要为每个环境提供maven-assembly-descriptor,如下所示:

<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. XYZ-1.0-快照test.war
  2. XYZ-1.0-快照qa.war
  3. XYZ-1.0-快照production.war
  4. 将通过一次maven mvn clean package调用创建。而已。但是你应该意识到这种方法正在进入部署方向,这实际上不是Maven的工作。

答案 1 :(得分:0)

我建议保持简单。正如@khmarbaise建议的那样,尽量不要混合maven和部署。

使用默认的war插件结构和配置,并在部署时处理* .properties文件(比如让它们保留令牌并将其替换为相关的环境值)

像您一样拆分,需要您在不同的文件中保持相同的更改。这是一条在大型项目中迷失的滑路。

我希望这会有所帮助。

相关问题