I have two profiles in pom.xml, and I have some resource files which I have added into target resource directory: ${project.build.outputDirectory}/resources
during execution of the first profile. What I need to do is remove those resource files during execution of the second profile.
Is there any way to remove or delete existing files from target directory?

- 2,550
- 3
- 36
- 63

- 879
- 1
- 7
- 8
6 Answers
I do agree with Matthew's observations, but I got the impression that the original poster was asking how to automate execution of clean
during (normal) "execution" of a profile.
You can define a plugin execution for the Maven Clean Plugin. It is normally only bound to clean
, but by defining a plugin execution you can bind clean:clean
(that is the clean
goal of the clean
plugin) to whichever lifecycle phase you want. The documentation of the Maven Clean Plugin has an example of how to do this. The documentation also has an example of deleting additional files. Merged the two looks like this:
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>some/relative/path</directory>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>

- 133,037
- 18
- 149
- 215

- 8,540
- 4
- 41
- 63
-
15I've found you need to use `
true ` in the configuration otherwise entire `target` directory will get deleted, too. – daiscog Mar 06 '19 at 17:18 -
2Well, sure. But the `target` folder is something you'd want to see deleted upon an `mvn clean` anyway. – Sander Verhagen Mar 06 '19 at 18:25
-
7This isn't about executing `mvn clean`, this is about deleting a specific file/folder when executing an arbitrary phase, e.g., `mvn package` - you may not want to clean the whole project and have to recompile. – daiscog Mar 07 '19 at 09:29
-
1Fair enough, I had to go back and read my own answer better to get that :) – Sander Verhagen Mar 07 '19 at 17:52
-
1I think this answer is better than the accepted since it is a generic solution, and responds well to the question that is generic as well, and not a solution for the `maven-antrun-plugin`. @SanderVerhagen can you add the excludeDefaultDirectories as daiscog suggests? – Kikkomann Jul 28 '21 at 05:49
I got the solution..!!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset dir="${project.build.outputDirectory}/resources" includes="*.xml" />
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
for reference - http://maven.apache.org/guides/mini/guide-building-for-different-environments.html

- 20,649
- 9
- 66
- 83

- 879
- 1
- 7
- 8
-
1Generally, in Maven, you'd prefer a declarative solution, over just coding it out (which plugins for like Ant, Groovy, etc.) let you do. – Sander Verhagen Feb 19 '18 at 18:44
-
2This post is totally correct but should be updated to use the target since task is deprecated. – Mr Chow Sep 04 '18 at 12:29
-
2Yeah, you can just replace `tasks` with `target`, and this will work. I was trying to delete if a file exists, and I ended up using `
`. – Jack Jan 15 '19 at 23:37
mvn clean
will remove the target
directory (and therefore all the files in it). If you want to remove only certain files from the target
directory, a combination of:
excludeDefaultDirectories
to stop it from deleting the whole directory, andfilesets
to tell it what to delete
ref: http://maven.apache.org/plugins/maven-clean-plugin/clean-mojo.html

- 9,298
- 3
- 33
- 48
Solution with Apache Maven AntRun Plugin 1.8:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete
dir="${project.build.outputDirectory}/resources"
includeemptydirs="true"/>
</target>
</configuration>
</execution>
</executions>
</plugin>

- 51,456
- 28
- 233
- 198
-
This works for me also with deleting multiple files. For multiple files, i use
– noor Apr 24 '18 at 15:34
I needed only a couple of files deleted from the output directory, the following worked fine for me.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/appContextLocal.xml" />
<delete
file="${project.build.outputDirectory}/appContextServer.xml" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
I also figured that you can run any ant commands here replace what ever task you need in between the <tasks> .... </tasks>
and it will work.
List of ant tasks that you can perform are here
Ref: http://maven.apache.org/plugins/maven-antrun-plugin/usage.html

- 6,386
- 6
- 54
- 89
thanks to above answers. finally i came to something like:
if you want to just delete some directories in target folder, you have to create some construct like this.
this for instance deletes just all contents of folders:
- target/unpack
- gen-external-apklibs
excludeDefaultDirectories allows to not delete complete target folder.
i used it to clean up target folder before lint analysis.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>Deleting all unnecessary files before lint analysis</id>
<phase>verify</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>target/unpack</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>*</exclude>
</excludes>
</fileset>
<fileset>
<directory>gen-external-apklibs</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>*</exclude>
</excludes>
</fileset>
</filesets>
<verbose>true</verbose>
</configuration>
</plugin>

- 5,229
- 3
- 43
- 53