在 MOJO 中使用 Maven 文件管理 API

本部分介绍如何在 Maven 插件中使用 Maven 文件管理 API。

添加文件管理 API 依赖项

第一步是将文件管理 API 添加为 Maven 依赖项,即在pom.xml中:

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

在 MOJO 中添加 FileSet

第二步是创建你的 MOJO 并添加一个FileSet对象:

/**
 * My MOJO
 *
 * @goal myGoal
 */
public class MyMojo
    extends AbstractMojo
{
    /**
     * A list of <code>fileSet</code> rules to select files and directories.
     *
     * @parameter
     */
    private List filesets;

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

    ...
}

要使用FileSet对象,您需要实例化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 );

配置你的 Maven 插件

最后一步是 Maven 插件配置。

<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>