eclipse 如何创建 Maven 构建配置文件以有条件地复制文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16242759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a Maven build profile to conditionally copy files?
提问by NullPointerException
I new to maven and I am trying to create pom.xml to build the war files for different environment using profiles
我是 maven 新手,我正在尝试创建 pom.xml 以使用配置文件为不同环境构建War文件
So I created the build target
所以我创建了构建目标
<build>
<finalName>myacct_okc</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>config/${environment}</directory>
</resource>
</resources>
</build>
And then created the profiles for each environment
然后为每个环境创建配置文件
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment>local</environment>
</properties>
</profile>
<profile>
<id>jboss</id>
<properties>
<environment>jboss</environment>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<environment>dev</environment>
</properties>
</profile>
and I create a config folder for each env
我为每个环境创建一个配置文件夹
project root
|-- src
| |-- main
| | |-- java
| | |-- resources
| | | |-- config.xml
| | | +-- config.properties
| | |-- webapp
| | | |-- META-INF
| | | | +--context.xml
| | |-- config
| | | |-- local
| | | | |--config.properties
| | | | +--config.xml
| | | |-- jboss
| | | | |--config.properties
| | | | +--config.xml
| | | +-- dev
| | | | |--config.properties
| | | | +--config.xml
+--pom.xml
Now when I run this pom.xml with any profile for e.g. jboss, the files form config/jboss folder are not getting copied ( or I mean the files in src/main/resources are not getting replaced).
现在,当我使用 jboss 的任何配置文件运行这个 pom.xml 时,config/jboss 文件夹中的文件不会被复制(或者我的意思是 src/main/resources 中的文件没有被替换)。
when I enabled the debug on on maven build I can see the copy getting executed.
当我在 Maven 构建上启用调试时,我可以看到副本正在执行。
[DEBUG] resource with targetPath null
directory C:\Projects\workspace\myaccount_build_4\myaccount\config\jboss
excludes []
includes []
[DEBUG] ignoreDelta true
[INFO] Copying 2 resources
[DEBUG] file config.xml has a filtered file extension
[DEBUG] copy C:\Projects\workspace\myaccount_build_4\myaccount\config\jboss\config.xml to C:\Projects\workspace\myaccount_build_4\myaccount\target\classes\config.xml
[DEBUG] file config.properties has a filtered file extension
[DEBUG] copy C:\Projects\workspace\myaccount_build_4\myaccount\config\jboss\META-INF\config.properties to C:\Projects\workspace\myaccount_build_4\myaccount\target\classes\META-INF\config.properties
But it is not replacing the files. The files are still the same as from src/main/resources.
但它不会替换文件。这些文件仍然与来自 src/main/resources 的文件相同。
Maven version is 3.0.4
Maven 版本是 3.0.4
Can someone help me what I am doing wrong ?
有人可以帮我做错什么吗?
I have looked at this question. This provides a solution but I want to override the files rather then excluding them and then copy.
我看过这个问题。这提供了一个解决方案,但我想覆盖文件而不是排除它们然后复制。
回答by bdkosher
I believe you'll have to add your resources to the maven-resources-plugin, inside a copy-resources execution.
我相信您必须在复制资源执行中将您的资源添加到 maven-resources-plugin 中。
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-config</id>
<phase>copy-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>config/${environment}</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
回答by NullPointerException
It worked after I added filtering= true
我添加后它起作用了 filtering= true
<build>
<finalName>myacct_okc</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
<resource>
<filtering>true</filtering>
<directory>config/${environment}</directory>
</resource>
</resources>
</build>