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.

7
  • Do you really have p1=bbbbbb in your properties file? You should have p1=${prop1}.
    – Tunaki
    Jan 10, 2017 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, 2017 at 21:13
  • I'm running mvn clean verify.
    – fhcat
    Jan 10, 2017 at 21:19
  • You're using <resources>, but since those are test resources, you need to use <testResources> instead. Something like: <testResources><testResource><directory>src/test/resources</directory><filtering>true</filtering></testResource></testResources>
    – Tunaki
    Jan 10, 2017 at 21:20
  • 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: codedump.io/share/IGE4mIB5B74j/1/… Oct 23, 2017 at 14:18

3 Answers 3

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.

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

3
  • This is not related with the question.
    – lubrum
    Mar 18, 2021 at 13:10
  • 1
    thank you very much @Johnathan Wan, that was the cause of my issue ! Mar 26, 2021 at 15:20
  • I think in some cases - like mine - it is perfectly related and saved my day :) May 13, 2022 at 11:50
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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.