diff --git a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java index 298e5b7d..e9467651 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.HashSet; import java.util.Iterator; @@ -41,6 +42,7 @@ import java.util.Properties; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.handler.ArtifactHandler; @@ -1173,6 +1175,17 @@ public void execute() throws MojoExecutionException, CompilationFailureException if (useIncrementalCompilation) { incrementalBuildHelperRequest.outputDirectory(getOutputDirectory()); + // Cleanup the generated source files created by annotation processing + // they are regenerated by the java compiler. + if (getGeneratedSourcesDirectory() != null) { + try (Stream walk = + Files.walk(getGeneratedSourcesDirectory().toPath())) { + walk.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); + } catch (IOException ex) { + getLog().warn("I/O error deleting the annotation processing generated files: " + ex.getMessage()); + } + } + incrementalBuildHelper.beforeRebuildExecution(incrementalBuildHelperRequest); getLog().debug("incrementalBuildHelper#beforeRebuildExecution");