Using Maven File Management API In A MOJO

This part explains how to use the Maven File Management API in a Maven Plugin.

Add File Management API dependency

The first step is to add the File Management API as a Maven dependency, i.e. in the pom.xml:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.shared</groupId>
      <artifactId>file-management</artifactId>
      <version>3.1.0</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>

Add FileSet in a MOJO

The second step is to create your MOJO and add a FileSet object:

/**
 * My MOJO
 */
@Mojo( name = "myGoal" )
public class MyMojo
    extends AbstractMojo
{
    /**
     * A list of <code>fileSet</code> rules to select files and directories.
     */
    @Parameter
    private FileSet[] filesets;

    /**
     * A specific <code>fileSet</code> rule to select files and directories.
     */
    @Parameter
    private FileSet fileset;

    ...
}

To use the FileSet object, you need to instantiate the FileSetManager.

FileSetManager fileSetManager = new FileSetManager();

String[] includedFiles = fileSetManager.getIncludedFiles( fileset );
String[] includedDir = fileSetManager.getIncludedDirectories( fileset );
String[] excludedFiles = fileSetManager.getExcludedFiles( fileset );
String[] excludedDir = fileSetManager.getExcludedDirectories( fileset );
fileSetManager.delete( fileset );

Configure your Maven Plugin

The last step is the Maven Plugin configuration.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>your-plugin-groupId</groupId>
        <artifactId>your-plugin-artifactId</artifactId>
        <version>your-plugin-version</version>
        <configuration>
          <!-- List of filesets -->
          <filesets>
            <fileset>
              <directory>some/relative/path</directory>
              <includes>
                <include>**/*.txt</include>
              </includes>
              <excludes>
                <exclude>**/log.log</exclude>
              </excludes>
              <followSymlinks>false</followSymlinks>
            </fileset>
          </filesets>

          <!-- Given fileset -->
          <fileset>
            <directory>some/relative/path</directory>
            <includes>
              <include>**/*.txt</include>
            </includes>
            <excludes>
              <exclude>**/log.log</exclude>
            </excludes>
            <followSymlinks>false</followSymlinks>
          </fileset>

          ...
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>