Skip to the content

Usage

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

Lookup the component in your Mojo:

    @Inject
    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
        FilterWrapper filterWrapper = new FilterWrapper() {
            @Override
            public Reader getReader(Reader reader) {
                Interpolator interpolator = new RegexBasedInterpolator("\\@", "(.+?)\\@");
                interpolator.addValueSource(
                        new PrefixedObjectValueSource(Arrays.asList("project", "pom"), mavenProject, true));
                return new InterpolatorFilterReader(reader, interpolator, "@", "@");
            }
        };

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

FilterWrapper is org.apache.maven.shared.filtering.FilterWrapper; the interpolation classes come from org.codehaus.plexus.interpolation.

There is a helper method to simplify this. It takes literal tokens rather than regular expressions, and an escape string (null for none):

        mavenResourcesExecution.addFilerWrapperWithEscaping(
                new PrefixedObjectValueSource(Arrays.asList("project", "pom"), mavenProject, true),
                "@", "@", null, false);

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.