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.
Resource Filtering. You can use Maven to perform variable replacement on project resources. When resource filtering is activated, Maven will scan resources for property references surrounded by ${ and }.
Resource filters are the first to handle a request after authorization. They can run code before the rest of the filter pipeline, and after the rest of the pipeline has completed. Resource filter is useful to implement caching or otherwise short-circuit the filter pipeline for performance reasons.
The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With