【发布时间】:2009-02-04 17:51:29
【问题描述】:
我必须创建测试战争和生产战争,这将在WEB-INF 目录中有不同的log4j.properties 文件。我有这些文件log4j.properties(测试战争)和dev.log4j.properties(用于生产环境)。
如何将dev.log4j.properties文件复制到log4j.properties文件中进行生产战争?
【问题讨论】:
我必须创建测试战争和生产战争,这将在WEB-INF 目录中有不同的log4j.properties 文件。我有这些文件log4j.properties(测试战争)和dev.log4j.properties(用于生产环境)。
如何将dev.log4j.properties文件复制到log4j.properties文件中进行生产战争?
【问题讨论】:
创建“开发”和“产品”配置文件,为每个配置文件选择一组备用资源。默认情况下使 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-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>
【讨论】:
上面的代码对我不起作用 - 必须将其更改为以下内容:
<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>
【讨论】:
最后一个响应有效。 但是您需要提供版本才能使其正常工作。
<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-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
【讨论】: