Search code examples
mavenmaven-resources-plugin

Maven resources plugin filtering not working


I have a POM with the following in:

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

And I have a properties file under src/test/resources:

p1=${prop1}

My goal is to copy the .properties file into target/test-classes directory and automatically change the value of p1. But it does not work. It copies the resource but does not change the value.


Solution

  • The problem is that you're configuring main resources instead of test resources; the main resources are configured with the resource element, whereas the test resources are configured with the testResource element. With the current configuration, the files under src/test/resources would be treated as filtered main resources, and the actual test resources would be unfiltered. This is why the copied properties file under target/test-classes is not filtered.

    What you're looking for is:

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

    With this, the files under src/test/resources will be treated as filtered test resources, and the main resources will be left untouched.