代码之家  ›  专栏  ›  技术社区  ›  Titi Wangsa bin Damhore

将Maven用于多部署环境(生产/开发)

  •  50
  • Titi Wangsa bin Damhore  · 技术社区  · 15 年前

    我在Maven有一个Web应用程序,使用默认的目录结构。没问题。 默认目录结构有一些指向本地主机数据库的属性文件。

    目前我创建了一个Ant脚本来创建不同的war文件- 一个用于生产,一个用于开发,使用以下命令:

    ant deploy-dev
    ant deploy-prod
    ant deploy-sit
    ant deploy-uat
    

    所以基本上他们创建了一个war文件,然后通过插入属性文件来更新war文件。

    在Maven中是否有类似的情况(根据配置创建不同的战争)?

    如果是,我该怎么做?

    我试过 mvn war 但它只是制造了一场战争

    6 回复  |  直到 6 年前
        1
  •  67
  •   Chris    12 年前

    仅供参考的最佳做法是 必须为不同的环境重建工件,因为这不会导致可重新生成的构建,并且在重建时其他事情可能会发生变化。如上文所述,使用资源筛选仅在以下情况下有效: 重建 你的项目。

    当你从开发人员到测试人员或验收测试到生产人员,你 想要重建。

    您想要做的,实际上是让您的配置是动态的,依赖于运行时变量。即不同环境下的不同弹簧设置或属性文件,例如:

    db-dev.properties
    db-test.properties
    db-prod.properties
    

    然后您可以使用运行时变量和Spring在这些配置之间切换 PropertyPlaceholderConfigurer .

    实际上,您也可以使用不同的Spring配置文件,就像我以前所做的那样,用于更复杂的设置。

    我还建议您将“默认”设置保留为生产-这样,如果您部署到生产环境中,如果忘记设置环境变量,则无需担心。

        2
  •  61
  •   m1ld    10 年前

    我更喜欢在这种情况下使用Maven配置文件。 例如,我们有目录结构:

    src/main/resources
    |
    +- local
    |  |
    |  `- specific.properties
    +- dev
       |
       `- specific.properties
    

    在pom.xml中定义两个配置文件:

    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources/local</directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>dev</id>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources/dev</directory>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>
    

    在这种情况下,我不需要每次为新文件更新pom.xml。在IDE中,只需切换配置文件,或者使用命令行中的-p标志。

    UPD :如果配置的某些属性相同,该怎么办? 配置如下:

    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                    <resource>
                        <directory>src/main/config/local</directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>dev</id>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                    <resource>
                        <directory>src/main/config/dev</directory>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>
    

    公用部分将存储在 src/main/resources 其他配置将在config目录中的相应文件夹中。

        3
  •  18
  •   Mike Cornell    15 年前

    如果您想从您的流程中删除Ant,我将研究使用带有过滤器的构建概要文件。

    在这个场景中,将属性文件插入到src/main/resources树结构中。然后使用如下筛选器属性参数化属性文件:

    jdbc.url=${filtered.jdbc.property}
    

    然后在src/main/filters内部根据配置文件创建过滤器文件。因此,您可以拥有dev-filters.properties sit-filters.properties等。这些内容包括:

    filtered.jdbc.property=jdbc url here
    

    然后,为每个设置了 env 指向您所在建筑的特定区域的属性。然后可以设置要使用的资源筛选器 ${env}-filters.properties 对于每个构建。此外,您可以设置war插件,将env属性添加到工件中,以便在存储库中的不同分类器下实际存储4个不同的工件。

    然后您只需使用每个配置文件构建应用程序。您必须为每个概要文件调用构建,但它工作得很好。

    POM中的一些设置示例:

    <build>
      <filters>
        <filter>src/main/filters/filter-${env}-application.properties</filter>
      </filters>
      <resources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
        </resource>
      </resources>
      <plugins>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.1-beta-1</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>war</goal>
              </goals>
              <configuration>
                <classifier>${env}</classifier>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    
    <profiles>
      <profile>
        <id>LOCAL</id>
        <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
          <env>LOCAL</env>
        </properties>
      </profile>
      <profile>
        <id>DEV</id>
        <properties>
          <env>DEV</env>
        </properties>
      </profile>
      <profile>
        <id>UAT</id>
        <properties>
          <env>UAT</env>
        </properties>
      </profile>
      <profile>
        <id>PROD</id>
        <properties>
          <env>PROD</env>
        </properties>
      </profile>
    </profiles>
    

    还有,这个的道具 blog post 这就是我最初发现实现这一目标的步骤。

        4
  •  6
  •   David Tinker    14 年前

    我使用Spring的PropertyPlaceholderConfigurater处理了这个问题,包括类路径上的属性文件和文件系统上的属性文件:

    <context:property-placeholder 
        location="classpath*:META-INF/spring/*.properties,file:myapp*.properties"/>
    

    如果应用程序启动(或运行测试等)时当前目录中有myapp*.properties文件,它将覆盖来自烘焙到war/ear/whatever中的文件的属性。

        5
  •  4
  •   seth    15 年前

    this article 关于Maven 2的使用 build profiles .看起来它只是通过antrun插件以任何方式委托给Ant,这样您甚至可以避免重新使用现有的build.xml文件。

        6
  •  1
  •   chrismarx    14 年前

    这很清楚,使用了@seth提到的构建配置文件-

    http://maven.apache.org/guides/mini/guide-building-for-different-environments.html