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

时间:2021-12-28 11:38:25

I have to create test war and production war, which will have a different log4j.properties file in the WEB-INF directory. I have these files log4j.properties (test war) and dev.log4j.properties (for production enivorment).

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

How to copy the dev.log4j.properties file into log4j.properties file for production war?

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

5 个解决方案

#1


  • Use Maven profiles (http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
  • 使用Maven配置文件(http://maven.apache.org/guides/introduction/introduction-to-profiles.html)

  • Create a "dev" and "prod" profile, selecting an alternate set of resources for each profile. Make Dev active by default.

    创建“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>
    
  • Build using the desired profile via: mvn install -Pdev or mvn install -Pprod

    通过以下方式使用所需的配置文件进行构建:mvn install -Pdev或mvn install -Pprod

#2


I solved this using the maven-resources plugin, where i created the prod directory which has the resources for production environment and copied them to WEB-INF/classes directory in process-resources phase.

我使用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


The code above didn't work for me - had to change it to the following:

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

<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


Last response is working. But you need to give the version to make it work.

最后的回应是有效的。但是你需要给出版本才能使它工作。

<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>

#5


The alternative way is to use maven-antrun-plugin

另一种方法是使用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>

Assume resource files are in following structure:

假设资源文件具有以下结构:

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

When do maven build, run the following commands per environment:

什么时候进行maven构建,每个环境运行以下命令:

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

#1


  • Use Maven profiles (http://maven.apache.org/guides/introduction/introduction-to-profiles.html)
  • 使用Maven配置文件(http://maven.apache.org/guides/introduction/introduction-to-profiles.html)

  • Create a "dev" and "prod" profile, selecting an alternate set of resources for each profile. Make Dev active by default.

    创建“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>
    
  • Build using the desired profile via: mvn install -Pdev or mvn install -Pprod

    通过以下方式使用所需的配置文件进行构建:mvn install -Pdev或mvn install -Pprod

#2


I solved this using the maven-resources plugin, where i created the prod directory which has the resources for production environment and copied them to WEB-INF/classes directory in process-resources phase.

我使用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


The code above didn't work for me - had to change it to the following:

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

<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


Last response is working. But you need to give the version to make it work.

最后的回应是有效的。但是你需要给出版本才能使它工作。

<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>

#5


The alternative way is to use maven-antrun-plugin

另一种方法是使用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>

Assume resource files are in following structure:

假设资源文件具有以下结构:

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

When do maven build, run the following commands per environment:

什么时候进行maven构建,每个环境运行以下命令:

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