Maven Clean: exclude deletion from inside the target directory

I tried many options but could not do the job. One example (child pom):

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <configuration> <filesets> <fileset> <directory>target</directory> <useDefaultExcludes>true</useDefaultExcludes> <excludes> <exclude>myFolder</exclude> </excludes> </fileset> </filesets> </configuration> </plugin> 

Maven is always trying to delete my folder. Why?

+5
source share
2 answers

As @ AR.3 was also suggested in the answer here , the phase and purpose of clean is

By default, it detects and removes directories configured in project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory and project.reporting.outputDirectory.

However, if you want to exclude the deletion of a specific file, you can follow the reverse approach (a simple hack) to do this as follows:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> <configuration> <excludeDefaultDirectories>true</excludeDefaultDirectories> <filesets> <fileset> <directory>target</directory> <followSymlinks>false</followSymlinks> <useDefaultExcludes>true</useDefaultExcludes> <includes> <include><!--everything other that what you want to exclude--></include> </includes> </fileset> </filesets> </configuration> </plugin> 

Read more about excludeDefaultDirectories from a similar link -

Disables the removal of the default source directories configured for the project. If set to true, only files / directories selected through the parameter files will be deleted.

EDIT

Indeed, you can eliminate the deletion of a specific file using the direct approach:

 <configuration> <excludeDefaultDirectories>true</excludeDefaultDirectories> <filesets> <fileset> <directory>target</directory> <includes> <include>**</include> </includes> <excludes> <exclude><!-- folder you want to exclude --></exclude> </excludes> </fileset> </filesets> </configuration> 
+8
source

From the goal documentation clean:clean :

This is an attempt to clean the project working directory from files created during build. By default, it detects and removes directories configured in project.build.directory , project.build.outputDirectory , project.build.testOutputDirectory and project.reporting.outputDirectory .

Files outside the default can also be included in deletion by setting the filesets tag.

This means that no matter what you declare in the filesets element, the target directory will always be deleted ( EDIT if excludeDefaultDirectories not set to true, see below). Given the above description, a workaround is to do the following:

  • Temporarily override the above properties to point to something other than the default target directory before clearing directories.

  • Use the filesets mechanism to indicate which directories should be excluded from the target directory (just as you already did).

  • Restore properties after cleaning directories.

The pre-clean and post-clean lifecycle phases can be used to complete steps 1 and 3.

EDIT: (thanks to nullpointer for specifying it)

Setting the excludeDefaultDirectories target excludeDefaultDirectories to true will exclude the default remote directory, in which case you can use the filesets approach without hacking overriding Maven properties.

+3
source

Source: https://habr.com/ru/post/1260940/


All Articles