11

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
fhcat
  • 971
  • 2
  • 9
  • 28
  • Do you really have `p1=bbbbbb` in your properties file? You should have `p1=${prop1}`. – Tunaki Jan 10 '17 at 21:05
  • sorry, I fixed that in the properties file. now I have p1=${prop1} but it still does not work. It just copies the .properties file without any modification. – fhcat Jan 10 '17 at 21:13
  • I'm running mvn clean verify. – fhcat Jan 10 '17 at 21:19
  • You're using ``, but since those are test resources, you need to use `` instead. Something like: `src/test/resourcestrue` – Tunaki Jan 10 '17 at 21:20
  • You saved my day. Create an answer so I can send the credit. – fhcat Jan 10 '17 at 21:37
  • 2
    I had a quite similar issue but mine dealt w/ spring boot. when having a spring-boot-starter-parent involved in the pom.xml variables to be replaced become sth. like @varname@ instead of ${varname}. See the issue and solution here: https://codedump.io/share/IGE4mIB5B74j/1/maven-resource-filtering-not-working---because-of-spring-boot-dependency – Dirk Schumacher Oct 23 '17 at 14:18
  • I have the same situation that @DirkSchumacher reported, a conflict with spring-boot-starter-parent, but I couldn't change the delimiters to @, so I disable default delimiters and force my own delimiters like this to work: `false ${*} # ` – dchang Sep 12 '19 at 09:52

3 Answers3

11

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
3

Following is the Note from the official reference document: (refer to https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/maven-plugin/reference/html/)

Note that, since the application.properties and application.yml files accept Spring style placeholders (${…​}), the Maven filtering is changed to use @..@ placeholders. (You can override that by setting a Maven property called resource.delimiter.)

1

I had similar issue when using copy-resources goal of maven properties plugin. The resources were copied but placeholders were not replaced. For me this was because of silly mistake - I copied the resources in eariler maven phase (validate) and I included placeholders properties file in later phase (initialize)... so the properties were not yet available.

I changed the phase of including the properties to validate and inclusion of placeholders to initialize and all works fine.

My working config is following:

Inclusion of properties file in validate:

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

Copying resources in 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>
walkeros
  • 4,736
  • 4
  • 35
  • 47