Maven Clean:排除目标内的目录被删除

Maven Clean: excluding directory inside target from being deleted

我尝试了很多变体,但无法实现。一例(子pom.xml):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <configuration>
        <filesets>
            <fileset>
                <directory>target</directory>
                <useDefaultExcludes>true</useDefaultExcludes>
                <excludes>
                    <exclude>myFolder</exclude>
                </excludes>
            </fileset>
        </filesets>
    </configuration>
</plugin>

Maven 总是试图删除我的文件夹。为什么?

来自documentation of the clean:clean goal:

This attempts to clean a project's working directory of the files that were generated at build-time. By default, it discovers and deletes the directories configured in project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory, and project.reporting.outputDirectory.

Files outside the default may also be included in the deletion by configuring the filesets tag.

这意味着无论您在 filesets 元素中声明什么,target 目录将始终被删除(EDIT 除非 excludeDefaultDirectories设置为真,见下文编辑)。鉴于上述描述,解决方法是执行以下操作:

  1. 暂时覆盖上述属性以指向默认 target 目录以外的其他内容, 清理目录之前。

  2. 使用filesets机制来判断从target目录中排除哪些目录(与您已经做的一样)。

  3. 在清理目录后恢复属性。

pre-cleanpost-clean 生命周期阶段可用于执行步骤 1 和 3。

编辑:(感谢 nullpointer 指出)

将目标参数 excludeDefaultDirectories 设置为 true 将排除默认目录被删除,在这种情况下,您可以使用 filesets 方法而无需覆盖 Maven 属性。

正如@AR.3 在回答 中所建议的那样,clean 阶段和目标将 -

By default, it discovers and deletes the directories configured in project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory, and project.reporting.outputDirectory.

不过,如果你想排除特定文件被删除,你可以按照相反的方法(一个简单的技巧)来做到这一点 -

<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-clean-plugin</artifactId>
       <version>3.0.0</version>
       <configuration>
           <excludeDefaultDirectories>true</excludeDefaultDirectories>
           <filesets>
                <fileset>
                    <directory>target</directory>
                    <followSymlinks>false</followSymlinks>
                    <useDefaultExcludes>true</useDefaultExcludes>
                    <includes>
                          <include><!--everything other that what you want to exclude--></include>
                    </includes>
                 </fileset>
            </filesets>
        </configuration>
</plugin>

更多关于 excludeDefaultDirectories 来自 similar link -

Disables the deletion of the default output directories configured for a project. If set to true, only the files/directories selected via the parameter filesets will be deleted.

编辑

确实可以使用直接方法排除特定文件被删除:

<configuration>
    <excludeDefaultDirectories>true</excludeDefaultDirectories>
        <filesets>
            <fileset>
                <directory>target</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude><!-- folder you want to exclude --></exclude>
                </excludes>
            </fileset>
        </filesets>
</configuration>