diff --git a/src/it/setup_annotation-verify-plugin/src/main/java/org.apache.maven.plugins.compiler.it/SourcePathReadGoal.java b/src/it/setup_annotation-verify-plugin/src/main/java/org.apache.maven.plugins.compiler.it/SourcePathReadGoal.java index d3691fa29..46b7d2b6a 100644 --- a/src/it/setup_annotation-verify-plugin/src/main/java/org.apache.maven.plugins.compiler.it/SourcePathReadGoal.java +++ b/src/it/setup_annotation-verify-plugin/src/main/java/org.apache.maven.plugins.compiler.it/SourcePathReadGoal.java @@ -47,17 +47,12 @@ public class SourcePathReadGoal extends AbstractMojo { public void execute() throws MojoExecutionException, MojoFailureException { if (sourceClass != null) { getLog().info("Checking compile source roots for: '" + sourceClass + "'"); - List roots = project.getCompileSourceRoots(); - roots.add(project.getModel().getBuild().getOutputDirectory() + "/../generated-sources/annotations"); - assertGeneratedSourceFileFor(sourceClass, roots); + assertGeneratedSourceFileFor(sourceClass, project.getCompileSourceRoots()); } if (testSourceClass != null) { getLog().info("Checking test-compile source roots for: '" + testSourceClass + "'"); - List roots = project.getTestCompileSourceRoots(); - roots.add( - project.getModel().getBuild().getOutputDirectory() + "/../generated-test-sources/test-annotations"); - assertGeneratedSourceFileFor(testSourceClass, roots); + assertGeneratedSourceFileFor(testSourceClass, project.getTestCompileSourceRoots()); } } 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 563189252..b12f6a192 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -703,8 +703,39 @@ protected final Optional getModuleDeclaration(final Set sourceFiles) private boolean targetOrReleaseSet; @Override - @SuppressWarnings("checkstyle:MethodLength") public void execute() throws MojoExecutionException, CompilationFailureException { + try { + executeReal(); + } finally { + addGeneratedSourcesToProject(); + } + } + + private void addGeneratedSourcesToProject() { + File generatedSourcesDirectory = getGeneratedSourcesDirectory(); + if (generatedSourcesDirectory == null) { + return; + } + + String generatedSourcesPath = generatedSourcesDirectory.getAbsolutePath(); + + if (isTestCompile()) { + getLog().debug("Adding " + generatedSourcesPath + + " to the project test-compile source roots but NOT the actual test-compile source roots:\n " + + StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n ")); + + project.addTestCompileSourceRoot(generatedSourcesPath); + } else { + getLog().debug("Adding " + generatedSourcesPath + + " to the project compile source roots but NOT the actual compile source roots:\n " + + StringUtils.join(project.getCompileSourceRoots().iterator(), "\n ")); + + project.addCompileSourceRoot(generatedSourcesPath); + } + } + + @SuppressWarnings("checkstyle:MethodLength") + private void executeReal() throws MojoExecutionException, CompilationFailureException { // ---------------------------------------------------------------------- // Look up the compiler. This is done before other code than can // cause the mojo to return before the lookup is done possibly resulting