Maven clean failing to delete files

djthoms picture djthoms · Dec 5, 2017 · Viewed 9k times · Source

Attempting to configure the maven clean plugin to remove additional directories in my project but it is not working properly. Here is a trivial reproduction using maven 3.5.

Get started with the simple demo app: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Modify the pom.xml to configure maven-clean-plugin:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Maven Quick Start Archetype</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>auto-clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                        <configuration>
                            <verbose>true</verbose>
                            <fieldsets>
                                <fieldset>
                                    <directory>${basedir}</directory>
                                    <includes>
                                        <include>demo/**</include>
                                    </includes>
                                </fieldset>
                            </fieldsets>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Create a dummy directory called demo

 mkdir demo && touch demo/hello

Now run:

 mvn package
 mvn clean

You'll notice the demo directory is still present. I do not know why this is. I have read these SO articles:

and I have tried running different permutations of the goal, phase, and id:

  • clean
  • clean:clean
  • clean:auto-clean

and so on to no avail...

I did a simple sanity check to see what was passed to the mojo:

[DEBUG] Configuring mojo org.apache.maven.plugins:maven-clean-plugin:3.0.0:clean from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-clean-plugin:3.0.0, parent: sun.misc.Launcher$AppClassLoader@55f96302]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-clean-plugin:3.0.0:clean' with basic configurator -->
[DEBUG]   (f) directory = /Users/djthomps/Desktop/test/target
[DEBUG]   (f) excludeDefaultDirectories = false
[DEBUG]   (f) failOnError = true
[DEBUG]   (f) followSymLinks = false
[DEBUG]   (f) outputDirectory = /Users/djthomps/Desktop/test/target/classes
[DEBUG]   (f) reportDirectory = /Users/djthomps/Desktop/test/target/classes
[DEBUG]   (f) retryOnError = true
[DEBUG]   (f) skip = false
[DEBUG]   (f) testOutputDirectory = /Users/djthomps/Desktop/test/target/test-classes
[DEBUG]   (f) verbose = true
[DEBUG] -- end configuration --

and it looks like my configuration options are never set. Why?

Note: I have read the plugin documentation as well: https://maven.apache.org/plugins/maven-clean-plugin/examples/delete_additional_files.html. From what I understand what I want to do is possible.

Answer

Zionsof picture Zionsof · Dec 5, 2017

Maybe try using the fileset configuration, instead of fieldset, like this:

<configuration>
    <followSymLinks>false</followSymLinks>
    <filesets>
        <fileset>
            <directory>*all_this_directory*</directory>
        </fileset>
        <fileset>
            <directory>*only_some_of_this_directory*</directory>
            <includes>
                <include>file1_with_wildcard*</include>
                <include>file2</include>
                <!--continue the same...-->
            </includes>
        </fileset>
    </filesets>
</configuration>