47

in my project I have a folder called war. It is initially empty and it's under version control. The folder gets populated by Maven during its package phase and Eclipse's WST plugin deploys the web app from this folder.

What I want is to delete contents of the war folder during the clean phase but not the folder itself. How do I do that? So far I know how to delete the whole war folder:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <followSymLinks>false</followSymLinks>
        <filesets>
            <fileset>
                <directory>${basedir}/war</directory>
            </fileset>
        </filesets>
    </configuration>
</plugin>

How do I delete only contents of the war folder but not the folder itself? I know that by adding some existing excludes, the folder won't be deleted. But how to generally delete only contents of a folder?

Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
janhink
  • 4,943
  • 3
  • 29
  • 37

5 Answers5

77

Add this includes section to your fileset definition

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <filesets>
                    <fileset>
                        <directory>war</directory>
                        <includes>
                            <include>**/*</include>
                        </includes>
                        <followSymlinks>false</followSymlinks>
                    </fileset>
                </filesets>
            </configuration>
        </plugin>
    </plugins>
</build>
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • 1
    For me it only worked after adding the section, as described here: http://stackoverflow.com/questions/18637626/remove-or-delete-resource-files-from-target-directory-using-pom-file – Noam Manos Apr 19 '15 at 19:09
  • 2
    put only ** for also remove the folder (The question ask for delete contents, but who visit this page maybe want delete the folder and the contents). – Manuel Romeiro Jun 01 '18 at 15:31
6

In my case I needed to delete the "bin" directory, and all it's subdirectory contents. I did this in my parent pom.xml:

<fileset>
    <directory>${basedir}</directory>
      <includes>
        <include>**/bin/**</include>
      </includes> 
      <followSymlinks>false</followSymlinks>
</fileset>

This found any bin directory within my project, and deleted any contents of the bin folder.

Jason K
  • 155
  • 2
  • 6
3

If you wan to delete more than one directory recursively:

    <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>
                        <filesets>
                            <fileset>
                                <directory>${basedir}</directory>
                                <includes>**/dir1/**</includes>
                            </fileset>
                            <fileset>
                                <directory>${basedir}</directory>
                                <includes>**/dir2/**</includes>
                            </fileset>
                            <fileset>
                                <directory>${basedir}</directory>
                                <includes>**/dir3/**</includes>
                            </fileset>
                        </filesets>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Wojtek
  • 1,410
  • 2
  • 16
  • 31
2

You will have to use the maven ant plugin & add the task to delete the folder contents only. However why do you need the war file in version control? Every time you build your code base will show that there are some modifications/new files (classes generated) to be checked in. If you are using standard maven conventions the target folder is used for all output.

isobar
  • 1,165
  • 8
  • 10
  • I have configured Eclipse to use the `war` folder (in Deployment assembly) and I want every developer who check-outs the project to have the folder, so the Eclipse configuration is valid. The contents would be of course added to svn:ignore and will be populated by Maven. – janhink Apr 18 '11 at 12:03
0

to delete folder from src/main/webapp/folder use :

<configuration>
  <filesets>
    <fileset>
      <directory>src/main/webapp</directory>
      <includes>
        <include>folder/</include>
      </includes>
      <followSymlinks>false</followSymlinks>
    </fileset>
  </filesets>
</configuration>