如何创建Maven构建配置文件以有条件地复制文件?

时间:2013-04-26 18:15:57

标签: java eclipse maven build maven-3

我是maven的新手,我正在尝试使用配置文件创建pom.xml来为不同的环境构建war文件

所以我创建了构建目标

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

然后为每个环境创建配置文件

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

我为每个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

现在,当我使用任何配置文件运行此pom.xml时,例如jboss,文件形式的config / jboss文件夹没有被复制(或者我的意思是src / main / resources中的文件没有被替换)。

当我在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

但它不是替换文件。这些文件仍然与src / main / resources相同。

Maven版本是3.0.4

有人可以帮我解决我做错的事吗?

我看过这个question。这提供了一个解决方案,但我想覆盖文件而不是排除它们然后复制。

2 个答案:

答案 0 :(得分:8)

我相信你必须在复制资源执行中将你的资源添加到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>

答案 1 :(得分:6)

在我添加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>