Class CleanOrphansMojo

java.lang.Object
org.apache.maven.plugins.clean.CleanOrphansMojo
All Implemented Interfaces:
org.apache.maven.api.plugin.Mojo

@Mojo(name="purge-check", defaultPhase="initialize") public class CleanOrphansMojo extends Object implements org.apache.maven.api.plugin.Mojo
Removes orphaned build output directories left behind by sub-projects that have been deleted or renamed since the last build.

When a sub-project is removed from the reactor (e.g. after a git pull or a branch switch), its target/ directory may remain on disk even though its pom.xml is gone. Because the sub-project is no longer part of the reactor, mvn clean cannot know about it and will skip it. This goal detects such orphaned directories and removes them.

Detection heuristic: a direct child directory of the current project's basedir is considered orphaned when its only non-hidden child is the build output directory (typically target/). A freshly checked-out or live sub-project always has at least a pom.xml alongside its build directory, so a directory whose sole visible content is a target/ folder can safely be assumed to be a leftover. The entire child directory (not just its build output subdirectory) is removed, since an orphaned directory with no source files has no reason to remain on disk.

Since:
4.0.0-beta-3
  • Constructor Details

    • CleanOrphansMojo

      public CleanOrphansMojo()
  • Method Details

    • execute

      public void execute()
      Scans direct children of the project basedir for orphaned build output directories and deletes them.
      Specified by:
      execute in interface org.apache.maven.api.plugin.Mojo
      Throws:
      org.apache.maven.api.plugin.MojoException - if an orphaned directory cannot be deleted and failOnError is true