【问题标题】:Maven resources plugin filtering not workingMaven资源插件过滤不起作用
【发布时间】:2017-05-25 12:01:01
【问题描述】:

我有一个 POM,其中包含以下内容:

<properties>
    <prop1>xxxxxxxxxx</prop1>
</properties>
<build>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <resources>
        <resource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
 </build>

我在src/test/resources 下有一个属性文件:

p1=${prop1}

我的目标是将.properties文件复制到target/test-classes目录下,并自动更改p1的值。但它不起作用。它复制资源但不更改值。

【问题讨论】:

  • 您的属性文件中真的有p1=bbbbbb 吗?你应该有p1=${prop1}
  • 抱歉,我在属性文件中修复了这个问题。现在我有 p1=${prop1} 但它仍然不起作用。它只是复制 .properties 文件而不做任何修改。
  • 我正在运行 mvn clean verify。
  • 您使用的是&lt;resources&gt;,但由于这些是测试资源,您需要改用&lt;testResources&gt;。比如:&lt;testResources&gt;&lt;testResource&gt;&lt;directory&gt;src/test/resources&lt;/directory&gt;&lt;filtering&gt;true&lt;/filtering&gt;&lt;/testResource&gt;&lt;/testResources&gt;
  • 我有一个非常相似的问题,但我的问题是用弹簧靴处理的。当 pom.xml 变量中涉及 spring-boot-starter-parent 时,要替换的变量变成了某事。像 @varname@ 而不是 ${varname}。在此处查看问题和解决方案:codedump.io/share/IGE4mIB5B74j/1/…

标签: maven maven-resources-plugin


【解决方案1】:

问题在于您正在配置主要资源而不是测试资源;主要资源配置了resource元素,而测试资源配置了testResource元素。在当前配置下,src/test/resources 下的文件将被视为过滤的主要资源,而实际的测试资源将是未过滤的。这就是为什么target/test-classes 下复制的属性文件没有被过滤的原因。

您正在寻找的是:

<testResources>
  <testResource>
    <directory>src/test/resources</directory>
    <filtering>true</filtering>
  </testResource>
</testResources>

这样,src/test/resources 下的文件将被视为过滤的测试资源,而主要资源将保持不变。

【讨论】:

    【解决方案2】:

    我在使用 Maven 属性插件的复制资源目标时遇到了类似的问题。资源被复制,但占位符没有被替换。对我来说,这是因为愚蠢的错误 - 我在早期的 maven 阶段 (validate) 复制了资源,并在后期阶段 (initialize) 包含了占位符属性文件......所以这些属性还不可用。

    我将包含属性的阶段更改为validate,并将占位符的包含阶段更改为initialize,一切正常。

    我的工作配置如下:

    在验证中包含属性文件:

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>read-project-properties</goal>
                            </goals>
                            <configuration>
                                <files>
                                    <file>${project.basedir}/path/to/placeholders.properties</file>
                                </files>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    复制initialize中的资源:

       <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <!-- here the phase you need -->
                    <phase>initialize</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/path/to/directory/with/resources/to/copy</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
    

    【讨论】:

      【解决方案3】:

      以下是官方参考文档中的注释: (参考https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/maven-plugin/reference/html/

      请注意,由于 application.properties 和 application.yml 文件接受 Spring 样式的占位符 (${...​}),因此 Maven 过滤更改为使用 @..@ 占位符。 (您可以通过设置名为 resource.delimiter 的 Maven 属性来覆盖它。)

      【讨论】:

      • 这与问题无关。
      • 非常感谢@Johnathan Wan,这就是我的问题的原因!
      猜你喜欢
      • 1970-01-01
      • 2016-11-07
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多