【问题标题】:How to copy a resource or another in Maven depending on the target environment?如何根据目标环境在 Maven 中复制资源或其他资源?
【发布时间】:2009-02-04 17:51:29
【问题描述】:

我必须创建测试战争和生产战争,这将在WEB-INF 目录中有不同的log4j.properties 文件。我有这些文件log4j.properties(测试战争)和dev.log4j.properties(用于生产环境)。

如何将dev.log4j.properties文件复制到log4j.properties文件中进行生产战争?

【问题讨论】:

    标签: maven-2 resources


    【解决方案1】:
    • 使用 Maven 配置文件 (http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
    • 创建“开发”和“产品”配置文件,为每个配置文件选择一组备用资源。默认情况下使 Dev 处于活动状态。

      <profiles>
          <profile>
              <id>dev</id>
              <activation>
                  <activeByDefault>true</activeByDefault>
              </activation>
              <build>
                  <resources>
                      <resource>
                          <directory>src/main/resources/dev</directory>
                      </resource>
                  </resources>
              </build>
          </profile>
          <profile>
              <id>prod</id>
              <build>
                  <resources>
                      <resource>
                          <directory>src/main/resources/prod</directory>
                      </resource>
                  </resources>
              </build>
          </profile>
      </profiles>
      
    • 通过以下方式使用所需的配置文件进行构建: mvn install -Pdev 或者 mvn install -Pprod

    【讨论】:

    • 这看起来比我以前的优雅多了!!
    • 很高兴听到这个消息。 Maven 可能看起来很棘手,但是当按照最佳实践路线使用时,会听到像您这样的人说“漂亮!”
    • 非常有用。对于稍微复杂的版本,您可以添加额外的“资源”目录:devtruesrc/main/resources/commonsrc/main/resources/dev
    • 当您能够简单地获取其中一种替代方案的所有内容时,这是最佳答案,但有时需要从基本资源文件夹开始,然后合并覆盖。在这种情况下,Build Monkey 的解决方案以及插件配置中的一些过滤器是一个不错的选择。
    • @StevenC 在这种情况下,您可以提供一个包含公共资源的附加目录并在主 pom 标记中配置它。配置文件中声明的资源将被合并。这对我来说是一个完美的答案,因为它与 gwt 编译器插件配合得很好,而其他解决方案却失败了(但是这些“其他解决方案”在其他情况下也适用,不涉及 gwt 插件)。
    【解决方案2】:

    我使用 maven-resources 插件解决了这个问题,我在其中创建了包含生产环境资源的 prod 目录,并在 process-resources 阶段将它们复制到 WEB-INF/classes 目录。

    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-resources-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <id>copy-prod-resources</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>webapp/WEB-INF/classes</outputDirectory>
                    <resources>
                        <resource>
                            <directory>src/main/resources/prod</directory>
                            <filtering>true</filtering>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

      【解决方案3】:

      上面的代码对我不起作用 - 必须将其更改为以下内容:

      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-prod-resources</id>
            <phase>process-resources</phase>
            <goals>
               <goal>copy-resources</goal>
            </goals>
            <configuration>
              <!-- this is important -->
              <overwrite>true</overwrite>
              <!-- this as well (target/ was missing) -->
              <outputDirectory>${basedir}/target/classes</outputDirectory>
              <resources>
                <resource>
                  <directory>src/main/resources/prod</directory>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      

      【讨论】:

        【解决方案4】:

        最后一个响应有效。 但是您需要提供版本才能使其正常工作。

        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.3</version>
          <executions>
            <execution>
              <id>copy-prod-resources</id>
              <phase>process-resources</phase>
              <goals>
                 <goal>copy-resources</goal>
              </goals>
              <configuration>
                <!-- this is important -->
                <overwrite>true</overwrite>
                <!-- target -->
                <outputDirectory>${basedir}/target/classes</outputDirectory>
                <resources>
                  <resource>
                    <!-- source -->
                    <directory>src/main/resources/prod</directory>
                  </resource>
                </resources>
              </configuration>
            </execution>
          </executions>
        </plugin>
        

        【讨论】:

        • 我尝试了这个解决方案,但现在 maven 正在向类添加一个额外的 prod 目录。我的其他目录,如 dev 和 test 也被创建。我在这里做错了什么?
        【解决方案5】:

        另一种方法是使用 maven-antrun-plugin

        <build>
            <plugins>
                <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-antrun-plugin</artifactId>
                  <version>1.7</version>
                  <executions>
                    <execution>
                      <phase>validate</phase>
                      <goals>
                        <goal>run</goal>
                      </goals>
                      <configuration>
                        <tasks>
                          <echo>build.env: ${build.env} </echo>
                          <delete file="src/main/resources/log4j.properties" />
                          <copy file="src/env/${build.env}/log4j.properties"
                                tofile="src/main/resources/log4j.properties" />
                        </tasks>
                      </configuration>
                    </execution>
                  </executions>
                </plugin>
            </plugins>
        </build>
        

        假设资源文件的结构如下:

        src/
          env/
              dev/
                  log4j.properties
              local/
                  log4j.properties
              prod/
                  log4j.properties
        

        在构建 maven 时,每个环境运行以下命令:

        mvn package -Dbuild.env=dev
        mvn package -Dbuild.env=local
        mvn package -Dbuild.env=prod
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-13
          • 1970-01-01
          • 2018-08-24
          • 1970-01-01
          • 1970-01-01
          • 2020-01-14
          • 2014-06-10
          • 2019-09-16
          相关资源
          最近更新 更多