Skip to content

[MCOMPILER-538] Do not add target/generated-sources/annotations to the source roots #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

mensinda
Copy link

@mensinda mensinda commented Jun 15, 2023

Fix for MCOMPILER-538.

The generatedSourcesPath will still be added to the maven project paths.


Following this checklist to help us incorporate your
contribution quickly and easily:

  • Make sure there is a JIRA issue filed
    for the change (usually before you start working on it). Trivial changes like typos do not
    require a JIRA issue. Your pull request should address just this issue, without
    pulling in other changes.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Format the pull request title like [MCOMPILER-XXX] - Fixes bug in ApproximateQuantiles,
    where you replace MCOMPILER-XXX with the appropriate JIRA issue. Best practice
    is to use the JIRA issue title in the pull request title and in the first line of the
    commit message.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Run mvn clean verify to make sure basic checks pass. A more thorough check will
    be performed on your pull request automatically.
  • You have run the integration tests successfully (mvn -Prun-its clean verify).

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

@mensinda mensinda changed the title MCOMPILER-538: Do not add target/generated-sources/annotations to the source roots [MCOMPILER-538] - Do not add target/generated-sources/annotations to the source roots Jun 15, 2023
@mensinda mensinda force-pushed the optionalGeneratedSources branch from 1fb1448 to a47f454 Compare June 15, 2023 12:23
@elharo elharo changed the title [MCOMPILER-538] - Do not add target/generated-sources/annotations to the source roots [MCOMPILER-538] Do not add target/generated-sources/annotations to the source roots Dec 17, 2023
Copy link
Contributor

@elharo elharo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a test.

I need to think about the bug but I'm not 100% sure the issue is correct. This might be working as it should.

@jorsol
Copy link
Contributor

jorsol commented Dec 19, 2023

I have been checking the javac options, and testing with this change, it does seem that we don't need to specify the generated-sources/annotations in the -sourcepath argument, it should be more than enough to specify it on the -s option.

Copy link
Member

@slawekjaranowski slawekjaranowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can not confirm that fix resolve issue, IT will be appreciated

@Nthalk
Copy link

Nthalk commented Dec 22, 2023

All my projects that test upgrades to 3.12.0 fail because of this line, looking forward to when it's removed!

@slawekjaranowski
Copy link
Member

All my projects that test upgrades to 3.12.0 fail because of this line, looking forward to when it's removed!

@Nthalk We need more details. There is a version 3.12.1 under vote, will be available in a few day.
Please comments in JIRA

@Nthalk
Copy link

Nthalk commented Dec 23, 2023

All my projects that test upgrades to 3.12.0 fail because of this line, looking forward to when it's removed!

@Nthalk We need more details. There is a version 3.12.1 under vote, will be available in a few day. Please comments in JIRA

I'm using renovate for auto dependency upgrades on multiple projects, the simplest doesn't use any annotation processing and yields this error on mvn clean install:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.12.0:testCompile (default-testCompile) on project selenium-jquery: Fatal error compiling: basedir /home/iode-jenkins-agent/agent/workspace/IodeSystems_SeleniumJQuery_PR-69/target/generated-test-sources/test-annotations does not exist -> [Help 1]

The configuration I have is pretty simple, no funny business:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <configuration>
          <source>21</source>
          <target>21</target>
          <showWarnings>true</showWarnings>
        </configuration>
      </plugin>

Here is the output of mvn clean install

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.853 s
[INFO] Finished at: 2023-12-22T16:09:00-08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.12.0:testCompile (default-testCompile) on project selenium-jquery: Fatal error compiling: basedir /Users/nthalk/local/src/iodesystems/SeleniumJQuery/target/generated-test-sources/test-annotations does not exist -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.12.0:testCompile (default-testCompile) on project selenium-jquery: Fatal error compiling
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:333)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:52)
    at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:161)
    at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:73)
Caused by: org.apache.maven.plugin.MojoExecutionException: Fatal error compiling
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1189)
    at org.apache.maven.plugin.compiler.TestCompilerMojo.execute (TestCompilerMojo.java:204)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:52)
    at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:161)
    at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:73)
Caused by: java.lang.IllegalStateException: basedir /Users/nthalk/local/src/iodesystems/SeleniumJQuery/target/generated-test-sources/test-annotations does not exist
    at org.codehaus.plexus.util.DirectoryScanner.scan (DirectoryScanner.java:268)
    at org.codehaus.plexus.compiler.AbstractCompiler.getSourceFilesForSourceRoot (AbstractCompiler.java:168)
    at org.codehaus.plexus.compiler.AbstractCompiler.getSourceFiles (AbstractCompiler.java:194)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile (JavacCompiler.java:138)
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1186)
    at org.apache.maven.plugin.compiler.TestCompilerMojo.execute (TestCompilerMojo.java:204)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:126)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:328)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174)
    at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75)
    at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162)
    at org.apache.maven.plugin.DefaultMojosExecutionStrategy.execute (DefaultMojosExecutionStrategy.java:39)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:159)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:105)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:73)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:53)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:118)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:261)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:173)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:101)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:906)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:283)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:206)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:52)
    at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:161)
    at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:73)

@jorsol
Copy link
Contributor

jorsol commented Dec 23, 2023

@Nthalk version 3.12.1 is going to be released in a couple of days fixing that issue.

@mensinda mensinda force-pushed the optionalGeneratedSources branch from a47f454 to 5910df7 Compare November 4, 2024 13:13
@mensinda mensinda changed the base branch from master to maven-compiler-plugin-3.x November 4, 2024 13:14
@mensinda
Copy link
Author

mensinda commented Nov 4, 2024

I have rebased the PR and added a test case.

I am not really happy with the hardcoded paths in SourcePathReadGoal.java myself, but I couldn't figure out a better solution.

@mensinda
Copy link
Author

Not sure why the CI failed before and the logs are gone now. I have rebased the PR and mvn -Prun-its verify ran successfully on my machine.

@elharo @slawekjaranowski There also is a test case now, as requested.

Copy link
Contributor

@elharo elharo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to run mvn spotless:apply

@mensinda
Copy link
Author

@elharo I did that and there are no changes:

mvn spotless:apply:

[INFO] Scanning for projects...
[INFO]
[INFO] -----------< org.apache.maven.plugins:maven-compiler-plugin >-----------
[INFO] Building Apache Maven Compiler Plugin 3.14.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO]
[INFO] --- spotless:2.43.0:apply (default-cli) @ maven-compiler-plugin ---
[INFO] Spotless.Java is keeping 282 files clean - 0 were changed to be clean, 0 were already clean, 282 were skipped because caching determined they were already clean
[INFO] Spotless.Pom is keeping 1 files clean - 0 were changed to be clean, 0 were already clean, 1 were skipped because caching determined they were already clean
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.198 s
[INFO] Finished at: 2025-02-19T14:22:35+01:00
[INFO] ------------------------------------------------------------------------

git status:

On branch optionalGeneratedSources
Your branch is up to date with 'origin/optionalGeneratedSources'.

nothing to commit, working tree clean

@mensinda mensinda force-pushed the optionalGeneratedSources branch from 24847e5 to 5ff1b35 Compare February 19, 2025 13:41
@mensinda
Copy link
Author

Nevermind, I was using Java 17 multiline string literals in the test-case. The CI should run now.

@elharo
Copy link
Contributor

elharo commented Feb 19, 2025

Is the build working or is the CI borked again:

Error: Failed to execute goal com.kohlschutter.mavenplugins:copy-rename-maven-plugin:2.0.0:copy (set-source) on project code: The plugin com.kohlschutter.mavenplugins:copy-rename-maven-plugin:2.0.0 requires Maven version 3.8.8 -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.kohlschutter.mavenplugins:copy-rename-maven-plugin:2.0.0:copy (set-source) on project code: The plugin com.kohlschutter.mavenplugins:copy-rename-maven-plugin:2.0.0 requires Maven version 3.8.8

@mensinda mensinda force-pushed the optionalGeneratedSources branch from 5ff1b35 to d434ced Compare February 20, 2025 08:04
@mensinda
Copy link
Author

The current error is most likely because of the copy-rename-maven-plugin that I require for the IT to function.

I have added invoker.maven.version = 3.8.8+ to the properties to (hopefully) skip the test on the CIs with maven 3.6.3.

@mensinda
Copy link
Author

looks like the CI is green now.

Copy link
Member

@slawekjaranowski slawekjaranowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we try to add the same test to master branch?

@desruisseaux
Copy link
Contributor

Should we try to add the same test to master branch?

Probably. It is desirable that the master contains most of the tests of the 3.x branch.

@slawekjaranowski slawekjaranowski added the bug Something isn't working label Mar 7, 2025
@slawekjaranowski slawekjaranowski merged commit 0b8b00b into apache:maven-compiler-plugin-3.x Mar 7, 2025
88 checks passed
@github-actions github-actions bot added this to the 3.14.1 milestone Mar 7, 2025
@mensinda mensinda deleted the optionalGeneratedSources branch March 10, 2025 15:28
@oehme
Copy link

oehme commented Mar 10, 2025

The PR description says

The generatedSourcesPath will still be added to the maven project paths.

But this is not the case, the entire code related to source paths was removed. This means that downstream plugins no longer see the generated sources. For example, the source-jar will no longer contain the generated sources. I'm pretty sure that's not what you wanted and will upset anyone who relies on https://issues.apache.org/jira/browse/MCOMPILER-157. Wasn't the idea to only remove this line? That should have been enough to fix MCOMPILER-538 without any side effects.

@slawekjaranowski
Copy link
Member

The PR descripations says

The generatedSourcesPath will still be added to the maven project paths.

But this is not the case, the entire code related to source paths was removed. This means that downstream plugins no longer see the generated sources. For example, the source-jar will no longer contain the generated sources. I'm pretty sure that's not what you wanted and will upset anyone who relies on https://issues.apache.org/jira/browse/MCOMPILER-157. Wasn't the idea to only remove this line? That should have been enough to fix MCOMPILER-538 without any side effects.

Looks like missing tests for MCOMPILER-157 ...

@oehme
Copy link

oehme commented Mar 10, 2025

Actually, I think there is a test - SourcePathReadGoal - but that was changed as part of this PR too.

@mensinda
Copy link
Author

Ah, it has been so long that I completely forgot about that part in the description.

Sadly, removing the generated source path from the project is required. The new testcase fails if the relevant code sections are reverted:

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 d3691fa..46b7d2b 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<String> 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<String> 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 5631892..8a94573 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -824,6 +824,26 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {
             }
         }

+        String generatedSourcesPath = generatedSourcesDirectory.getAbsolutePath();
+
+        if (isTestCompile()) {
+            getLog().debug("Adding " + generatedSourcesPath + " to test-compile source roots:\n  "
+                    + StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n  "));
+
+            project.addTestCompileSourceRoot(generatedSourcesPath);
+
+            getLog().debug("New test-compile source roots:\n  "
+                    + StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n  "));
+        } else {
+            getLog().debug("Adding " + generatedSourcesPath + " to compile source roots:\n  "
+                    + StringUtils.join(project.getCompileSourceRoots().iterator(), "\n  "));
+
+            project.addCompileSourceRoot(generatedSourcesPath);
+
+            getLog().debug("New compile source roots:\n  "
+                    + StringUtils.join(project.getCompileSourceRoots().iterator(), "\n  "));
+        }
+
         compilerConfiguration.setSourceLocations(compileSourceRoots);

         compilerConfiguration.setAnnotationProcessors(annotationProcessors);

@mensinda
Copy link
Author

I did manage to add the path back in #311, but it feels hacky...

@oehme
Copy link

oehme commented Mar 11, 2025

You can do it without these hacks by moving the generated sources code after the stale source scanning code. The important part is that it needs to happen before the mojo returns, but after it has scanned its own sources.

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 5631892..27ea1f3 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -814,16 +814,6 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {
 
         compilerConfiguration.setProc(proc);
 
-        File generatedSourcesDirectory = getGeneratedSourcesDirectory();
-        compilerConfiguration.setGeneratedSourcesDirectory(
-                generatedSourcesDirectory != null ? generatedSourcesDirectory.getAbsoluteFile() : null);
-
-        if (generatedSourcesDirectory != null) {
-            if (!generatedSourcesDirectory.exists()) {
-                generatedSourcesDirectory.mkdirs();
-            }
-        }
-
         compilerConfiguration.setSourceLocations(compileSourceRoots);
 
         compilerConfiguration.setAnnotationProcessors(annotationProcessors);
@@ -933,9 +923,6 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {
                     getLog().info("Recompiling the module because of "
                             + MessageUtils.buffer().strong(cause) + ".");
                     compilerConfiguration.setSourceFiles(sources);
-                } else {
-                    getLog().info("Nothing to compile - all classes are up to date.");
-                    return;
                 }
             } catch (CompilerException e) {
                 throw new MojoExecutionException("Error while computing stale sources.", e);
@@ -964,11 +951,6 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {
                 throw new MojoExecutionException("Error while computing stale sources.", e);
             }
 
-            if (staleSources.isEmpty()) {
-                getLog().info("Nothing to compile - all classes are up to date.");
-                return;
-            }
-
             compilerConfiguration.setSourceFiles(staleSources);
 
             try {
@@ -988,6 +970,41 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {
             }
         }
 
+        File generatedSourcesDirectory = getGeneratedSourcesDirectory();
+        compilerConfiguration.setGeneratedSourcesDirectory(
+                generatedSourcesDirectory != null ? generatedSourcesDirectory.getAbsoluteFile() : null);
+
+        if (generatedSourcesDirectory != null) {
+            if (!generatedSourcesDirectory.exists()) {
+                generatedSourcesDirectory.mkdirs();
+            }
+
+            String generatedSourcesPath = generatedSourcesDirectory.getAbsolutePath();
+
+            if (isTestCompile()) {
+                getLog().debug("Adding " + generatedSourcesPath + " to test-compile source roots:\n  "
+                        + StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n  "));
+
+                project.addTestCompileSourceRoot(generatedSourcesPath);
+
+                getLog().debug("New test-compile source roots:\n  "
+                        + StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n  "));
+            } else {
+                getLog().debug("Adding " + generatedSourcesPath + " to compile source roots:\n  "
+                        + StringUtils.join(project.getCompileSourceRoots().iterator(), "\n  "));
+
+                project.addCompileSourceRoot(generatedSourcesPath);
+
+                getLog().debug("New compile source roots:\n  "
+                        + StringUtils.join(project.getCompileSourceRoots().iterator(), "\n  "));
+            }
+        }
+
+        if (compilerConfiguration.getSourceFiles().isEmpty()) {
+            getLog().info("Nothing to compile - all classes are up to date.");
+            return;
+        }
+

mensinda added a commit to mensinda/maven-compiler-plugin that referenced this pull request Mar 11, 2025
This is a followup for for apache#191 to add the generatedSourcesPath back
to the maven project paths as the final step of the mojo.
@mensinda
Copy link
Author

@oehme Thanks for the idea, I have added an alternative implementation / fix with #312.

slawekjaranowski pushed a commit that referenced this pull request Mar 14, 2025
This is a followup for for #191 to add the generatedSourcesPath back
to the maven project paths as the final step of the mojo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants