6

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

所以我创建了构建目标

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

我为每个环境创建一个配置文件夹

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

现在,当我使用例如 jboss 的任何配置文件运行此 pom.xml 时,不会复制 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

有人可以帮我做错什么吗?

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

4

2 回答 2

9

我相信您必须在复制资源执行中将资源添加到 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>
于 2013-04-26T18:24:56.807 回答
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>
于 2013-05-21T13:29:24.743 回答