zappee
zappee

Reputation: 22746

Remove directory with maven-clean-plugin

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.

Upvotes: 32

Views: 20548

Answers (3)

Volodya Lombrozo
Volodya Lombrozo

Reputation: 3476

Just to add a bit of clarification of all the other answers. There are several options how to remove a directory using maven-clean-plugin:

  1. Remove the entire target directory and some other directory that placed out of the target directory
<plugin>
  <artifactId>maven-clean-plugin</artifactId>
  <version>3.4.0</version>
  <executions>
    <execution>
      <goals>
        <goal>clean</goal>
      </goals>
      <configuration>
        <filesets>
          <fileset>
            <directory>relative/path/to/the/directory</directory>
          </fileset>
        </filesets>
      </configuration>
    </execution>
  </executions>
</plugin>
  1. Remove only a directory and keep the target directory (you need to set the excludeDefaultDirectories parameter)
<plugin>
  <artifactId>maven-clean-plugin</artifactId>
  <version>3.4.0</version>
  <executions>
    <execution>
      <goals>
        <goal>clean</goal>
      </goals>
      <configuration>
        <excludeDefaultDirectories>true</excludeDefaultDirectories>
        <filesets>
          <fileset>
            <directory>relative/path/to/the/directory</directory>
          </fileset>
        </filesets>
      </configuration>
    </execution>
  </executions>
</plugin>
  1. Remove a directory under the target directory. You can use the same excludeDefaultDirectories parameter. For example, if you need to remove only classes directory:
<plugin>
  <artifactId>maven-clean-plugin</artifactId>
  <version>3.4.0</version>
  <executions>
    <execution>
      <goals>
        <goal>clean</goal>
      </goals>
      <configuration>
        <excludeDefaultDirectories>true</excludeDefaultDirectories>
        <filesets>
          <fileset>
            <directory>target/classes</directory>
          </fileset>
        </filesets>
      </configuration>
    </execution>
  </executions>
</plugin>

There is the documentation where you can read more about other parameters.

Upvotes: 0

zappee
zappee

Reputation: 22746

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>

Upvotes: 48

Neha Prakash
Neha Prakash

Reputation: 21

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

<excludeDefaultDirectories>true</excludeDefaultDirectories>

Upvotes: 2

Related Questions