Skip to content

Maven resource filtering and includes/excludes

Maven resource filtering and include/exclude configurations allow developers to control which files are included in the final build and whether variable substitution occurs within those files^[600-developer-java-3-party-java-maven01.md].

Resource filtering

Resource filtering is activated by setting the <filtering> element to true within a resource definition^[600-developer-java-3-party-java-maven01.md]. When enabled, Maven will parse files located in the specified directory and replace placeholders (such as ${project.build.sourceEncoding}) with actual property values defined in the pom.xml^[600-developer-java-3-party-java-maven01.md].

Includes and excludes

The <includes> and <excludes> elements are used to whitelist or blacklist specific files within a resource directory^[600-developer-java-3-party-java-maven01.md]. These rules define a filtering pattern that determines which files are processed by Maven^[600-developer-java-3-party-java-maven01.md].

Specific directives include:

  • <include>: Specifies files to be included (e.g., app1.xml)^[600-developer-java-3-party-java-maven01.md].
  • <exclude>: Specifies files to be omitted (e.g., app2.xml)^[600-developer-java-3-party-java-maven01.md].

When defining these rules, exclusion rules take precedence over inclusion rules^[600-developer-java-3-party-java-maven01.md].

Configuration example

The following configuration demonstrates how to apply filtering to a specific directory while restricting the operation to certain files^[600-developer-java-3-party-java-maven01.md].

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>app1.xml</include>
            </includes>
            <excludes>
                <exclude>app2.xml</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
</build>
  • Maven
  • [[Java Build]]
  • [[Dependency Management]]

Sources

  • 600-developer-java-3-party-java-maven01.md