Skip to content

Maven resources filtering and inclusion/exclusion

Maven provides mechanisms to control which resource files are included in the build and how they are processed, primarily through the <resources> element within the <build> section of the pom.xml.^[600-developer__java__3-party__java-maven01.md]

Resource filtering

Filtering allows Maven to modify files during the build process by replacing variable placeholders (e.g. ${...}) with actual property values defined in the POM.^[600-developer__java__3-party__java-maven01.md]

This feature is enabled by setting the <filtering> element to true within a specific resource definition.^[600-developer__java__3-party__java-maven01.md]

Inclusion and Exclusion

Within a resource definition, developers can specify exactly which files should be included or excluded from the final build artifact using the <includes> and <excludes> tags.^[600-developer__java__3-party__java-maven01.md]

  • Includes: The <includes> element accepts a list of <include> patterns to specify files to be processed^[600-developer__java__3-party__java-maven01.md].
  • Excludes: The <excludes> element accepts a list of <exclude> patterns to specify files to be ignored^[600-developer__java__3-party__java-maven01.md].

According to the source material, exclusion rules take precedence and are considered "stronger" than inclusion rules^[600-developer__java__3-party__java-maven01.md].

Example configuration

The following example demonstrates a resource configuration that enables filtering, specifically includes app1.xml, and explicitly excludes app2.xml^[600-developer__java__3-party__java-maven01.md]:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>app1.xml</include>
        </includes>
        <excludes>
            <exclude>app2.xml</exclude>
        </excludes>
    </resource>
</resources>

Sources

^[600-developer__java__3-party__java-maven01.md]