Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 859
fhcat Avatar asked Jan 10 '17 20:01

fhcat


People also ask

What is Maven resource filtering?

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

What is true about resource filter?

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.

What is Maven resources plugin?

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.


2 Answers

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.

like image 81
Tunaki Avatar answered Nov 08 '22 23:11

Tunaki


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

like image 45
Johnathan Wan Avatar answered Nov 09 '22 00:11

Johnathan Wan