如何根据目标环境在Maven中复制资源或其他资源?

Spr*_*key 33 resources maven-2

我必须创建测试战争和生产战争,它将log4j.propertiesWEB-INF目录中有一个不同的文件.我有这些文件log4j.properties(测试战争)和dev.log4j.properties(生产enivorment).

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

Mat*_*ugh 58

  • 使用Maven配置文件(http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
  • 创建"dev"和"prod"配置文件,为每个配置文件选择一组备用资源.默认情况下启用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>
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用所需的配置文件通过: mvn install -Pdevmvn install -Pprod

  • 很高兴听到它.Maven可能看起来很棘手,但是当沿着最佳练习线使用时,听到像你这样的人说"漂亮!" (2认同)
  • @StevenC 在这种情况下,您可以提供一个包含公共资源的附加目录,并在主 pom &lt;build&gt; 标记中对其进行配置。在配置文件中声明的资源将被合并。这对我来说是完美的答案,因为它与 gwt 编译器插件一起工作得很好,而其他解决方案却失败了(但是这些“其他解决方案”在其他情况下有效,不涉及 gwt 插件)。 (2认同)

Spr*_*key 10

我使用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>
Run Code Online (Sandbox Code Playgroud)


ste*_*nos 5

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

<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>
Run Code Online (Sandbox Code Playgroud)