'delete target directory in maven during mvn clean phase

I would like to delete target directory from project after performing mvn clean build life cycle. I have observed that after performing mvn clean generated resources under target folder gets deleted but not target directory. Any suggestion helps me a lot. Below is code snippet from pom.xml which I have used in my project

 <plugins>
              <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.0.0</version>
                    <goals>
                      <goal>clean</goal>
                    </goals>
                    <configuration>
                    <filesets><fileset>   
                    <directory>${basedir}/target</directory>
                    </fileset></filesets>
                    </configuration>
             </plugin>


Solution 1:[1]

The default behavior of an mvn clean is to delete the target directory. I am not sure why it doesn't do that in your case other than you might have parent POM that overrides the default behaviour:

From Maven's output when doing an mvn clean:

[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ xxx ---
[INFO] Deleting C:\dev\TradingStationT2\t2-platform\misc\target

I even verified it manually, the target directory does indeed get deleted.

Try doing an mvn help:effective-pom to see if the default behavior has been overridden. You should see something like this:

<plugin>
  <artifactId>maven-clean-plugin</artifactId>
  <version>2.6.1</version>
  <executions>
    <execution>
      <id>default-clean</id>
      <phase>clean</phase>
      <goals>
        <goal>clean</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Solution 2:[2]

To delete the target directory, run mvn clean on your project directly from the command line. That will delete the target directory as expected.

In contrast, running Run As > Maven clean from Eclipse for some reason leaves the target directory and subdirectories classes and test-classes. Even though the console messages you see in Eclipse are the same as a regular mvn clean, it seems this approach does something behind the scenes to recreate those directories for a maven project.

As for why, I'm not yet exactly sure. But it seems to have to do with the .classpath file. If you view that file you'll see those directory paths. It may also have to do with the "Maven Nature" that gets added into Eclipse when you create a Maven Project (see Properties > Project Natures > Maven Nature). Specifically, the Eclipse M2Eclipse plugin is what seems to drive that "Maven Nature", including the generation of those source folders.

Solution 3:[3]

Sometimes the IDE maybe just messed up in memory, so restart it once, it should resolve the issue as it did in my case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel
Solution 2
Solution 3 Anvita Shukla