This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-compiler-plugin.git

commit 53c1cc65b5fddaf9afda8a751dfe8395c9080b29
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Sat Sep 20 11:15:27 2025 +0200

    Improvement in the handling of some exceptions:
    
    * More helpful exception when the workaround for the `--patch-module` 
option didn't work.
    * Some safety against null values in the `basedir` and `mojoStatusPath` 
fields of the MOJO.
      While those fields should never be null, it happens sometime when testing 
snapshot versions of Maven core.
---
 .../plugin/compiler/AbstractCompilerMojo.java      | 36 +++++++++++++---------
 .../maven/plugin/compiler/IncrementalBuild.java    | 21 +++++++++++--
 .../plugin/compiler/WorkaroundForPatchModule.java  | 17 +++++++---
 3 files changed, 52 insertions(+), 22 deletions(-)

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 6663659..594d0b2 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -1713,15 +1713,18 @@ public abstract class AbstractCompilerMojo implements 
Mojo {
             return;
         }
         final var commandLine = new StringBuilder("For trying to compile from 
the command-line, use:");
-        final var chdir =
-                
Path.of(System.getProperty("user.dir")).relativize(basedir).toString();
-        if (!chdir.isEmpty()) {
-            boolean isWindows = (File.separatorChar == '\\');
-            commandLine
-                    .append(System.lineSeparator())
-                    .append("    ")
-                    .append(isWindows ? "chdir " : "cd ")
-                    .append(chdir);
+        Path dir = basedir;
+        if (dir != null) { // Should never be null, but it has been observed 
with some Maven versions.
+            dir = Path.of(System.getProperty("user.dir")).relativize(dir);
+            String chdir = dir.toString();
+            if (!chdir.isEmpty()) {
+                boolean isWindows = (File.separatorChar == '\\');
+                commandLine
+                        .append(System.lineSeparator())
+                        .append("    ")
+                        .append(isWindows ? "chdir " : "cd ")
+                        .append(chdir);
+            }
         }
         commandLine.append(System.lineSeparator()).append("    
").append(executable != null ? executable : compilerId);
         Path pathForRelease = debugFilePath;
@@ -1806,12 +1809,15 @@ public abstract class AbstractCompilerMojo implements 
Mojo {
      * @return the given path, potentially relative to the base directory
      */
     private Path relativize(Path file) {
-        Path root = project.getRootDirectory();
-        if (root != null && file.startsWith(root)) {
-            try {
-                file = basedir.relativize(file);
-            } catch (IllegalArgumentException e) {
-                // Ignore, keep the absolute path.
+        final Path dir = basedir;
+        if (dir != null) { // Should never be null, but it has been observed 
with some Maven versions.
+            Path root = project.getRootDirectory();
+            if (root != null && file.startsWith(root)) {
+                try {
+                    file = dir.relativize(file);
+                } catch (IllegalArgumentException e) {
+                    // Ignore, keep the absolute path.
+                }
             }
         }
         return file;
diff --git 
a/src/main/java/org/apache/maven/plugin/compiler/IncrementalBuild.java 
b/src/main/java/org/apache/maven/plugin/compiler/IncrementalBuild.java
index e0a8ee5..4de2486 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/IncrementalBuild.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/IncrementalBuild.java
@@ -32,6 +32,7 @@ import java.nio.file.StandardOpenOption;
 import java.nio.file.attribute.FileTime;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.List;
@@ -338,8 +339,11 @@ final class IncrementalBuild {
             throws IOException {
         this.sourceFiles = sourceFiles;
         this.saveSourceList = saveSourceList;
-        Path file = mojo.mojoStatusPath;
-        cacheFile = 
Files.createDirectories(file.getParent()).resolve(file.getFileName());
+        cacheFile = mojo.mojoStatusPath;
+        if (cacheFile != null) {
+            // Should never be null, but it has been observed to happen with 
some Maven versions.
+            Files.createDirectories(cacheFile.getParent());
+        }
         showCompilationChanges = mojo.showCompilationChanges;
         buildTime = System.currentTimeMillis();
         previousBuildTime = buildTime;
@@ -355,7 +359,10 @@ final class IncrementalBuild {
      * @throws IOException if an error occurred while deleting the file
      */
     public void deleteCache() throws IOException {
-        Files.deleteIfExists(cacheFile);
+        if (cacheFile != null) {
+            // Should never be null, but it has been observed to happen with 
some Maven versions.
+            Files.deleteIfExists(cacheFile);
+        }
     }
 
     /**
@@ -386,6 +393,10 @@ final class IncrementalBuild {
      */
     @SuppressWarnings({"checkstyle:InnerAssignment", "checkstyle:NeedBraces"})
     public void writeCache() throws IOException {
+        if (cacheFile == null) {
+            // Should never be null, but it has been observed to happen with 
some Maven versions.
+            return;
+        }
         try (DataOutputStream out = new DataOutputStream(new 
BufferedOutputStream(Files.newOutputStream(
                 cacheFile,
                 StandardOpenOption.WRITE,
@@ -432,6 +443,10 @@ final class IncrementalBuild {
      */
     @SuppressWarnings("checkstyle:NeedBraces")
     private Map<Path, SourceInfo> loadCache() throws IOException {
+        if (cacheFile == null) {
+            // Should never be null, but it has been observed to happen with 
some Maven versions.
+            return Collections.emptyMap(); // Not `Map.of()` because we need 
to allow `Map.remove(…)`.
+        }
         final Map<Path, SourceInfo> previousBuild;
         try (DataInputStream in = new DataInputStream(
                 new BufferedInputStream(Files.newInputStream(cacheFile, 
StandardOpenOption.READ)))) {
diff --git 
a/src/main/java/org/apache/maven/plugin/compiler/WorkaroundForPatchModule.java 
b/src/main/java/org/apache/maven/plugin/compiler/WorkaroundForPatchModule.java
index e659a84..a290991 100644
--- 
a/src/main/java/org/apache/maven/plugin/compiler/WorkaroundForPatchModule.java
+++ 
b/src/main/java/org/apache/maven/plugin/compiler/WorkaroundForPatchModule.java
@@ -32,6 +32,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 
@@ -126,7 +127,7 @@ final class WorkaroundForPatchModule extends 
ForwardingJavaFileManager<StandardJ
      * @param type the type of path together with the module name
      * @param paths the paths to set
      * @param cause the exception that occurred when invoking the standard API
-     * @throws CompilationFailureException if this workaround doesn't work 
neither
+     * @throws IllegalArgumentException if this workaround doesn't work neither
      */
     private static void specifyAsOption(
             StandardJavaFileManager fileManager,
@@ -135,10 +136,18 @@ final class WorkaroundForPatchModule extends 
ForwardingJavaFileManager<StandardJ
             UnsupportedOperationException cause)
             throws IOException {
 
-        var it = Arrays.asList(type.option(paths)).iterator();
-        if (!fileManager.handleOption(it.next(), it) || it.hasNext()) {
-            throw new CompilationFailureException("Cannot handle " + type, 
cause);
+        String message;
+        Iterator<String> it = Arrays.asList(type.option(paths)).iterator();
+        if (!fileManager.handleOption(it.next(), it)) {
+            message = "Failed to set the %s option for module %s";
+        } else if (it.hasNext()) {
+            message = "Unexpected number of arguments after the %s option for 
module %s";
+        } else {
+            return;
         }
+        JavaPathType rawType = type.rawType();
+        throw new IllegalArgumentException(
+                String.format(message, 
rawType.option().orElse(rawType.name()), type.moduleName()), cause);
     }
 
     /**

Reply via email to