Filter a List of org.apache.maven.model.Resource

Lookup the component in your Mojo:

     * @component role="org.apache.maven.shared.filtering.MavenResourcesFiltering"
     *            role-hint="default"
     * @required
    private MavenResourcesFiltering mavenResourcesFiltering;

Apply filtering on your List of resources, see Introduction for the default FilterWrappers that are used.

        MavenResourcesExecution mavenResourcesExecution =
            new MavenResourcesExecution ( resources, outputDirectory, mavenProject,
                                          encoding, fileFilters,
                                          nonFilteredFileExtensions, mavenSession );

        mavenResourcesFiltering.filterResources( mavenResourcesExecution );

Add a new filtering token

You must use the other methods from the MavenResourcesFiltering component and construct your own List of FilterWrappers. The following example adds interpolation for the token @ @ using values coming from reflection with the Maven Project.

        // Create your FilterWrapper
        FileUtils.FilterWrapper filterWrapper = new FileUtils.FilterWrapper()
            public Reader getReader( Reader reader )
                Interpolator propertiesInterpolator =
                    new RegexBasedInterpolator(  "\\@", "(.+?)\\@" );
                ValueSource valueSource = new MavenProjectValueSource( mavenProject,
                                                                       true );
                propertiesInterpolator.addValueSource( valueSource );
                return new InterpolatorFilterReader( reader,
                                                     "@", "@" );

        // Add the new filterWrapper to your MavenResourcesExecution instance
        mavenResourcesExecution.addFilterWrapper( filterWrapper );

There is a helper method to simplify this. Here's how you would use it to do what we did above:

        mavenResourcesExecution.addFilerWrapper( new MavenProjectValueSource( mavenProject,
                                                                              true ),
                                                 "\\@", "(.+?)\\@", "@", "@" );

Note: If mavenResourcesExecution.useDefaultFilterWrappers is set to true, the default FilterWrappers will be added first.

Now it's time to filter the resources:

        // Apply filtering on your resources
        mavenResourcesFiltering.filterResources( mavenResourcesExecution );

Note: Maven Filtering uses the plexus-interpolation component.