Maven 2 如何根据目标环境在Maven中复制资源?
我必须创建测试战和生产战,它们在Maven 2 如何根据目标环境在Maven中复制资源?,maven-2,resources,Maven 2,Resources,我必须创建测试战和生产战,它们在WEB-INF目录中有一个不同的log4j.properties文件。我有这些文件log4j.properties(testwar)和dev.log4j.properties(用于生产环境) 如何将dev.log4j.properties文件复制到生产战争的log4j.properties文件中?我使用maven resources插件解决了这个问题,在这里我创建了包含生产环境资源的prod目录,并在process resources阶段将它们复制到WEB-INF
WEB-INF
目录中有一个不同的log4j.properties
文件。我有这些文件log4j.properties
(testwar)和dev.log4j.properties
(用于生产环境)
如何将
dev.log4j.properties
文件复制到生产战争的log4j.properties
文件中?我使用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>
org.apache.maven.plugins
maven资源插件
2.3
复制产品资源
过程资源
复制资源
webapp/WEB-INF/classes
src/main/resources/prod
真的
- 使用Maven配置文件()
- 创建一个“dev”和“prod”概要文件,为每个概要文件选择一组备用资源。默认情况下使开发处于活动状态
<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>
发展 真的 src/main/resources/dev 戳 src/main/resources/prod
- 通过以下方式使用所需的配置文件进行构建:
或mvn安装-Pdev
mvn安装-Pprod
<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>
maven资源插件
复制产品资源
过程资源
复制资源
真的
${basedir}/target/classes
src/main/resources/prod
最后一个响应正在工作。
但是你需要给出一个版本来让它工作
<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资源插件
2.3
复制产品资源
过程资源
复制资源
真的
${basedir}/target/classes
src/main/resources/prod
另一种方法是使用maven antrun插件
<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>
进行maven构建时,请在每个环境中运行以下命令:
mvn package -Dbuild.env=dev
mvn package -Dbuild.env=local
mvn package -Dbuild.env=prod
这看起来比我以前拥有的要优雅得多!!很高兴听到这个消息。Maven可能看起来很棘手,但当按照最佳实践使用时,像你这样的人会说“漂亮!”非常有用。对于稍微复杂的版本,您可以添加额外的“资源”目录:dev true src/main/resources/common src/main/resources/dev当您能够简单地获取其中一个备选方案的所有内容时,这是最好的答案,但有时需要从基本资源文件夹开始,然后在覆盖中合并。在这种情况下,Build Monkey的解决方案以及插件配置中的一些过滤器是一种很好的方法。@StevenC在这种情况下,您可以提供一个附加目录和公共资源,并在主pom标记中配置它。配置文件中声明的资源将被合并。这对我来说是一个完美的答案,因为它与gwt编译器插件配合使用效果很好,而其他解决方案失败(但是这些“其他解决方案”在其他情况下也可以工作,没有涉及gwt插件)。我尝试了这个解决方案,但现在maven正在向类添加一个额外的prod目录。我的其他目录如dev和test也被创建。我做错了什么?
mvn package -Dbuild.env=dev
mvn package -Dbuild.env=local
mvn package -Dbuild.env=prod