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 0dea1592b91edc93243d7d46ae95b95e17619934
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Sun Sep 21 13:35:18 2025 +0200

    Apply to fork mode the same policy as the usual mode regarding when to 
write debug files.
---
 .../plugin/compiler/AbstractCompilerMojo.java      | 16 ++++++++++-
 .../apache/maven/plugin/compiler/CompilerMojo.java |  6 +++++
 .../apache/maven/plugin/compiler/ForkedTool.java   | 31 ++++++++++++++--------
 .../maven/plugin/compiler/TestCompilerMojo.java    |  6 +++++
 4 files changed, 47 insertions(+), 12 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 594d0b2..3fad7fe 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -472,6 +472,7 @@ public abstract class AbstractCompilerMojo implements Mojo {
     /**
      * Whether to show messages about what the compiler is doing.
      * If {@code true}, then the {@code -verbose} option will be added to 
compiler arguments.
+     * In addition, files such as {@code target/javac.args} will be generated 
even on successful compilation.
      *
      * @see <a 
href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#option-verbose";>javac
 -verbose</a>
      */
@@ -1096,12 +1097,16 @@ public abstract class AbstractCompilerMojo implements 
Mojo {
      * The debug file will contain the compiler options together with the list 
of source files to compile.
      *
      * <p>Note: debug logging should not be confused with the {@link #debug} 
flag.</p>
+     *
+     * @see CompilerMojo#debugFileName
+     * @see TestCompilerMojo#debugFileName
      */
     @Nullable
     protected abstract String getDebugFileName();
 
     /**
      * {@return the debug file name with its path, or null if none}.
+     * This method does not check if the debug file will be written, as the 
compilation result is not yet known.
      */
     final Path getDebugFilePath() {
         String filename = getDebugFileName();
@@ -1112,6 +1117,15 @@ public abstract class AbstractCompilerMojo implements 
Mojo {
         return 
Path.of(project.getBuild().getOutputDirectory()).resolveSibling(filename);
     }
 
+    /**
+     * Returns whether the debug file should be written after a successful 
build.
+     * By default, debug files are written only if the build failed.
+     * However, some options can change this behavior.
+     */
+    final boolean shouldWriteDebugFile() {
+        return verbose || logger.isDebugEnabled();
+    }
+
     /**
      * Runs the Java compiler. This method performs the following steps:
      *
@@ -1352,7 +1366,7 @@ public abstract class AbstractCompilerMojo implements 
Mojo {
          * In case of failure, or if debugging is enabled, dump the options to 
a file.
          * By default, the file will have the ".args" extension.
          */
-        if (!success || verbose || logger.isDebugEnabled()) {
+        if (!success || shouldWriteDebugFile()) {
             IOException suppressed = null;
             try {
                 writeDebugFile(executor, configuration, success);
diff --git a/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java 
b/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
index 0127d86..dcd7cb2 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
@@ -137,6 +137,10 @@ public class CompilerMojo extends AbstractCompilerMojo {
      * command-line by typing {@code javac @target/javac.args}.
      * The debug file will contain the compiler options together with the list 
of source files to compile.
      *
+     * <p>By default, this debug file is written only if the compilation of 
main code failed.
+     * The writing of the debug files can be forced by setting the {@link 
#verbose} flag to {@code true}
+     * or by specifying the {@code --verbose} option to Maven on the 
command-line.</p>
+     *
      * @since 3.10.0
      */
     @Parameter(defaultValue = "javac.args")
@@ -270,6 +274,8 @@ public class CompilerMojo extends AbstractCompilerMojo {
 
     /**
      * {@return the file where to dump the command-line when debug is 
activated or when the compilation failed}.
+     *
+     * @see #debugFileName
      */
     @Nullable
     @Override
diff --git a/src/main/java/org/apache/maven/plugin/compiler/ForkedTool.java 
b/src/main/java/org/apache/maven/plugin/compiler/ForkedTool.java
index 1a774a5..778ac21 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/ForkedTool.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/ForkedTool.java
@@ -62,6 +62,11 @@ class ForkedTool implements Tool, OptionChecker {
      */
     private final Path debugFilePath;
 
+    /**
+     * Whether to write the {@code target/javac.sh} file even on successful 
compilation.
+     */
+    private final boolean verbose;
+
     /**
      * Creates a new forked compiler.
      *
@@ -71,6 +76,7 @@ class ForkedTool implements Tool, OptionChecker {
         basedir = mojo.basedir;
         executable = Objects.requireNonNull(mojo.executable);
         debugFilePath = mojo.getDebugFilePath();
+        verbose = mojo.shouldWriteDebugFile();
     }
 
     /**
@@ -191,14 +197,24 @@ class ForkedTool implements Tool, OptionChecker {
     }
 
     /**
-     * Starts the process and wait for its completion.
-     * If a debug file has been specified, writes in that file the command 
which is about to be executed.
+     * Starts the process and waits for its completion.
+     * If the process fails and a debug file has been specified, writes the 
command in that file.
      *
      * @param builder builder of the process to start
      * @param out where to send additional compiler output
+     * @return the exit value of the process
      */
     private int start(ProcessBuilder builder, Appendable out) throws 
IOException {
-        if (debugFilePath != null) {
+        Process process = builder.start();
+        int status;
+        try {
+            status = process.waitFor();
+        } catch (InterruptedException e) {
+            out.append("Compilation has been interrupted by " + 
e).append(System.lineSeparator());
+            process.destroy();
+            status = 1;
+        }
+        if ((status != 0 || verbose) && debugFilePath != null) {
             // Use the path separator as a way to identify the operating 
system.
             final boolean windows = File.separatorChar == '\\';
             String filename = debugFilePath.getFileName().toString();
@@ -221,13 +237,6 @@ class ForkedTool implements Tool, OptionChecker {
                 debugFile.newLine();
             }
         }
-        Process process = builder.start();
-        try {
-            return process.waitFor();
-        } catch (InterruptedException e) {
-            out.append("Compilation has been interrupted by " + 
e).append(System.lineSeparator());
-            process.destroy();
-            return 1;
-        }
+        return status;
     }
 }
diff --git 
a/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java 
b/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
index 691b12f..afcbc00 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
@@ -225,6 +225,10 @@ public class TestCompilerMojo extends AbstractCompilerMojo 
{
      * from the command-line by typing {@code javac @target/javac-test.args}.
      * The debug file will contain the compiler options together with the list 
of source files to compile.
      *
+     * <p>By default, this debug file is written only if the compilation of 
test code failed.
+     * The writing of the debug files can be forced by setting the {@link 
#verbose} flag to {@code true}
+     * or by specifying the {@code --verbose} option to Maven on the 
command-line.</p>
+     *
      * @see CompilerMojo#debugFileName
      * @since 3.10.0
      */
@@ -355,6 +359,8 @@ public class TestCompilerMojo extends AbstractCompilerMojo {
 
     /**
      * {@return the file where to dump the command-line when debug is 
activated or when the compilation failed}.
+     *
+     * @see #debugFileName
      */
     @Nullable
     @Override

Reply via email to