desruisseaux commented on code in PR #1002:
URL: 
https://github.com/apache/maven-compiler-plugin/pull/1002#discussion_r2597693996


##########
src/main/java/org/apache/maven/plugin/compiler/ToolExecutor.java:
##########
@@ -611,59 +685,97 @@ public boolean compile(final JavaCompiler compiler, final 
Options configuration,
                         
fileManager.setLocationFromPaths(StandardLocation.SOURCE_PATH, sourcePaths);
                     } else {
                         
fileManager.setLocationForModule(StandardLocation.MODULE_SOURCE_PATH, 
moduleName, sourcePaths);
+                        modulesNotPresentInNewVersion.put(moduleName, 
Boolean.FALSE);
                     }
-                    outputForRelease = outputDirectory; // Modified below if 
compiling a non-base release.
-                    if (isVersioned) {
-                        outputForRelease = 
Files.createDirectories(SourceDirectory.outputDirectoryForReleases(
-                                isModularProject, outputForRelease, 
unit.release));
-                        if (isClasspathProject) {
-                            /*
-                             * For a non-modular project, this block is 
executed at most once par compilation unit.
-                             * Add the paths to the classes compiled for 
previous versions.
-                             */
-                            List<Path> classpath = 
prependDependency(JavaPathType.CLASSES, latestOutputDirectory);
-                            
fileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
-                        } else {
-                            /*
-                             * For a modular project, this block can be 
executed an arbitrary number of times
-                             * (once per module).
-                             */
-                            Path latestOutputForModule = 
latestOutputDirectory.resolve(moduleName);
-                            JavaPathType.Modular pathType = 
JavaPathType.patchModule(moduleName);
-                            List<Path> paths = prependDependency(pathType, 
latestOutputForModule);
+                    /*
+                     * When compiling for the base Java version, the 
configuration for current module is finished.
+                     * The remaining of this loop is executed only for target 
Java versions after the base version.
+                     * In those cases, we need to add the paths to the classes 
compiled for the previous version.
+                     * For a non-modular project, always add the paths to the 
class-path. For a modular project,
+                     * add the paths to the module-path only the first time. 
After, we need to use patch-module.
+                     */
+                    if (latestOutputDirectory != null) {
+                        if (canAddLatestOutputToPath) {
+                            canAddLatestOutputToPath = isClasspathProject; // 
For next iteration.
+                            JavaPathType pathType = isClasspathProject ? 
JavaPathType.CLASSES : JavaPathType.MODULES;
+                            Deque<Path> paths = prependDependency(pathType, 
latestOutputDirectory);
+                            
fileManager.setLocationFromPaths(pathType.location().get(), paths);
+                        }
+                        /*
+                         * For a modular project, following block can be 
executed an arbitrary number of times
+                         * We need to declare that the sources that we are 
compiling are for patching a module.
+                         * But we also need to remember that these sources 
will need to be removed in the next
+                         * iteration, because they will be replaced by the 
compiled classes (the above block).
+                         */
+                        if (isModularProject) {
+                            final Deque<Path> paths = 
dependencies(JavaPathType.patchModule(moduleName));
+                            removeFirsts(paths, 
modulesWithSourcesAsPatches.put(moduleName, sourcePaths.size()));
+                            Path latestOutput = 
resolveModuleOutputDirectory(latestOutputDirectory, moduleName);
+                            if (Files.exists(latestOutput)) {
+                                paths.addFirst(latestOutput);
+                            }
+                            sourcePaths.forEach(paths::addFirst);
                             
fileManager.setLocationForModule(StandardLocation.PATCH_MODULE_PATH, 
moduleName, paths);
                         }
                     }
                 }
+                /*
+                 * If there are any module compiled for the previous Java 
version which are not compiled again
+                 * for the current version, we need to clear the source paths 
which were declared for leftover
+                 * modules.
+                 */
+                for (var iterator = 
modulesNotPresentInNewVersion.entrySet().iterator(); iterator.hasNext(); ) {
+                    Map.Entry<String, Boolean> entry = iterator.next();
+                    if (entry.getValue()) {
+                        String moduleName = entry.getKey();
+                        Deque<Path> paths = 
dependencies(JavaPathType.patchModule(moduleName));
+                        if (removeFirsts(paths, 
modulesWithSourcesAsPatches.remove(moduleName))) {
+                            
paths.addFirst(latestOutputDirectory.resolve(moduleName));
+                        } else if (paths.isEmpty()) {
+                            // Not sure why the following is needed, but it 
has been observed in real projects.
+                            paths.add(outputDirectory.resolve(moduleName));
+                        }
+                        
fileManager.setLocationForModule(StandardLocation.PATCH_MODULE_PATH, 
moduleName, paths);
+                        
fileManager.setLocationForModule(StandardLocation.MODULE_SOURCE_PATH, 
moduleName, Set.of());
+                        iterator.remove();
+                    } else {
+                        entry.setValue(Boolean.TRUE); // For compilation of 
next target version (if any).
+                    }
+                }
                 /*
                  * At this point, we finished to set the source paths. We have 
also modified the class-path or
                  * patched the modules with the output directories of codes 
compiled for lower Java releases.
-                 * The `defaultModuleName` is an adjustment done when the 
project is a Java module, but still
-                 * organized in a package source hierarchy instead of a module 
source hierarchy. Updating the
-                 * `unit.roots` map is not needed for this class, but done in 
case a `target/javac.args` file
-                 * will be written after the compilation.
+                 * The adjustment done below if for when the project is a Java 
module, but still organized in

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to