26

Can I delete directory with maven-clean-plugin?

The following configuration deletes files from the given directory but the directory itself will be remained:

<plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <filesets>
            <fileset>
                <directory>src/main/javascript/node_modules</directory>
                <includes>
                    <include>**/*</include>
                </includes>
                <followSymlinks>false</followSymlinks>
            </fileset>
        </filesets>
    </configuration>
</plugin>

I have checked the plugin's documentation but I can not see any way to delete the directory: http://maven.apache.org/plugins-archives/maven-clean-plugin-2.6.1/clean-mojo.html

I need to delete the directory as well.

zappee
  • 20,148
  • 14
  • 73
  • 129
  • According to the documentation some/relative/path is totally is equivalent to ${basedir}/some/relative/path. Link: https://maven.apache.org/plugins/maven-clean-plugin/examples/delete_additional_files.html – zappee Dec 14 '17 at 21:26

2 Answers2

40

Finally I have figured out what is the solution. The file pattern what I used was wrong.

The following file mask deletes files from the given directory but the folder will be remained:

<include>**/*</include>

This pattern deletes the directory as well:

<include>**</include>
zappee
  • 20,148
  • 14
  • 73
  • 129
2

Use true for 'excludeDefaultDirectories>'in configuration tag. maven plugin version should not be less than 2.3

<excludeDefaultDirectories>true</excludeDefaultDirectories>
  • Please use the most recent version of the plugins currently this is 3.0.0 The following will show the most recent versions of the plugins: https://maven.apache.org/plugins/ – khmarbaise Dec 14 '17 at 08:54
  • Unfortunately it does not solved my problem. The directory is still there. – zappee Dec 14 '17 at 21:28
  • `` is used to prevent `maven-clean-plugin` from removing the `target` directory, that it would normally remove in the built-in "default-clean" task. This is useful if you want to use `maven-clean-plugin` to remove files during the build process while keeping whatever made it to `target` by that time. – Guss Dec 07 '22 at 09:00