【问题标题】:How can I create a Maven build profile to conditionally copy files?如何创建 Maven 构建配置文件以有条件地复制文件?
【发布时间】:2013-04-21 00:01:19
【问题描述】:

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

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

当我在 maven build 上启用调试时,我可以看到副本正在执行。

 [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。这提供了一个解决方案,但我想覆盖文件而不是排除它们然后复制。

【问题讨论】:

标签: java eclipse maven build maven-3


【解决方案1】:

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

【讨论】:

  • 我们没有使用 maven-resources 插件
  • 你可能没有在你的 POM 中显式使用它,但它是一个核心 Maven 插件,它在你的构建生命周期中被隐式使用。配置插件是一种让插件从您的自定义源位置复制资源的方法。
  • 绝对的救星!我正在考虑根据配置文件重命名这一点,但无法弄清楚如何做到这一点。这要优雅得多。谢谢
【解决方案2】:

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

【讨论】:

  • 过滤将文件中的 $(something) 替换为 maven 属性值
猜你喜欢
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 2012-02-18
相关资源
最近更新 更多