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

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 6fed6653d9 Add command line information to Verifier log files for 
easier IT reproduction (#11366)
6fed6653d9 is described below

commit 6fed6653d97e54386f9c91a78847cce2c0020624
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Nov 26 16:02:55 2025 +0100

    Add command line information to Verifier log files for easier IT 
reproduction (#11366)
    
    When running integration tests, the Verifier dumps output to a log file 
(usually log.txt).
    This change adds the full command line used for execution at the beginning 
of the log file
    to make it easy to reproduce the test outside the IT environment.
    
    The command line information includes:
    - Full path to Maven executable with all arguments
    - Environment variables (MAVEN_OPTS, MAVEN_SKIP_RC, custom env vars)
    - JVM system properties
    - Working directory
    - Execution mode (EMBEDDED, FORKED, AUTO)
    
    The information is prepended to the log file after Maven execution 
completes,
    ensuring it appears at the top without being overwritten by Maven's logging.
    
    Example output:
    # Command line used for this execution:
    /opt/maven/bin/mvn -l log.txt --errors --batch-mode validate
    # MAVEN_OPTS=-Duser.home=/path/to/user.home
    # Working directory: /path/to/test/project
    # Execution mode: AUTO
    
    * Skip command line info in log files when quiet logging is enabled
    
    The MavenITmng4387QuietLoggingTest was failing because it expects empty log 
files
    when using the -q (quiet) flag, but the command line information was still 
being
    added to the log file.
    
    This change adds detection for quiet logging (-q or --quiet flags) and skips
    adding the command line information when quiet logging is enabled, 
respecting
    the intent of quiet logging to minimize output.
    
    Changes:
    - Added isQuietLogging() method to detect -q/--quiet flags
    - Modified execute() to skip command line info when quiet logging is enabled
    - Maintains full functionality for normal logging while respecting quiet 
mode
---
 .../main/java/org/apache/maven/it/Verifier.java    | 104 +++++++++++++++++++++
 1 file changed, 104 insertions(+)

diff --git 
a/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/Verifier.java
 
b/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/Verifier.java
index e33cb61cb9..1904d97152 100644
--- 
a/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/Verifier.java
+++ 
b/its/core-it-support/maven-it-helper/src/main/java/org/apache/maven/it/Verifier.java
@@ -270,7 +270,32 @@ public void execute() throws VerificationException {
             stdout = new ByteArrayOutputStream();
             stderr = new ByteArrayOutputStream();
             ExecutorRequest request = 
builder.stdOut(stdout).stdErr(stderr).build();
+
+            // Store the command line to prepend to log file after execution
+            // Skip adding command line info if quiet logging is enabled 
(respects -q flag)
+            String commandLineHeader = null;
+            if (logFileName != null && !isQuietLogging(args)) {
+                try {
+                    commandLineHeader = formatCommandLine(request, mode);
+                } catch (Exception e) {
+                    // Don't fail the execution if we can't format the command 
line, just log it
+                    System.err.println("Warning: Could not format command 
line: " + e.getMessage());
+                }
+            }
+
             int ret = executorHelper.execute(mode, request);
+
+            // After execution, prepend the command line to the log file
+            if (commandLineHeader != null && Files.exists(logFile)) {
+                try {
+                    String existingContent = Files.readString(logFile, 
StandardCharsets.UTF_8);
+                    String newContent = commandLineHeader + existingContent;
+                    Files.writeString(logFile, newContent, 
StandardCharsets.UTF_8);
+                } catch (IOException e) {
+                    // Don't fail the execution if we can't write the command 
line, just log it
+                    System.err.println("Warning: Could not prepend command 
line to log file: " + e.getMessage());
+                }
+            }
             if (ret > 0) {
                 String dump;
                 try {
@@ -418,6 +443,85 @@ private String getLogContents(Path logFile) {
         }
     }
 
+    /**
+     * Formats the command line that would be executed for the given 
ExecutorRequest and mode.
+     * This provides a human-readable representation of the Maven command for 
debugging purposes.
+     */
+    private String formatCommandLine(ExecutorRequest request, 
ExecutorHelper.Mode mode) {
+        StringBuilder cmdLine = new StringBuilder();
+
+        cmdLine.append("# Command line: ");
+        // Add the Maven executable path
+        Path mavenExecutable = request.installationDirectory()
+                .resolve("bin")
+                
.resolve(System.getProperty("os.name").toLowerCase().contains("windows")
+                    ? request.command() + ".cmd"
+                    : request.command());
+        cmdLine.append(mavenExecutable.toString());
+
+        // Add MAVEN_ARGS if they would be used (only for forked mode)
+        if (mode == ExecutorHelper.Mode.FORKED || mode == 
ExecutorHelper.Mode.AUTO) {
+            String mavenArgsEnv = System.getenv("MAVEN_ARGS");
+            if (mavenArgsEnv != null && !mavenArgsEnv.isEmpty()) {
+                cmdLine.append(" ").append(mavenArgsEnv);
+            }
+        }
+
+        // Add the arguments
+        for (String arg : request.arguments()) {
+            cmdLine.append(" ");
+            // Quote arguments that contain spaces
+            if (arg.contains(" ")) {
+                cmdLine.append("\"").append(arg).append("\"");
+            } else {
+                cmdLine.append(arg);
+            }
+        }
+
+        // Add environment variables that would be set
+        if (request.environmentVariables().isPresent() && 
!request.environmentVariables().get().isEmpty()) {
+            cmdLine.append("\n# Environment variables:");
+            for (Map.Entry<String, String> entry : 
request.environmentVariables().get().entrySet()) {
+                cmdLine.append("\n# 
").append(entry.getKey()).append("=").append(entry.getValue());
+            }
+        }
+
+        // Add JVM arguments that would be set via MAVEN_OPTS
+        List<String> jvmArgs = new ArrayList<>();
+        if 
(!request.userHomeDirectory().equals(ExecutorRequest.getCanonicalPath(Paths.get(System.getProperty("user.home")))))
 {
+            jvmArgs.add("-Duser.home=" + 
request.userHomeDirectory().toString());
+        }
+        if (request.jvmArguments().isPresent()) {
+            jvmArgs.addAll(request.jvmArguments().get());
+        }
+        if (request.jvmSystemProperties().isPresent()) {
+            
jvmArgs.addAll(request.jvmSystemProperties().get().entrySet().stream()
+                    .map(e -> "-D" + e.getKey() + "=" + e.getValue())
+                    .toList());
+        }
+
+        if (!jvmArgs.isEmpty()) {
+            cmdLine.append("\n# MAVEN_OPTS=").append(String.join(" ", 
jvmArgs));
+        }
+
+        if (request.skipMavenRc()) {
+            cmdLine.append("\n# MAVEN_SKIP_RC=true");
+        }
+
+        cmdLine.append("\n# Working directory: 
").append(request.cwd().toString());
+        cmdLine.append("\n# Execution mode: ").append(mode.toString());
+
+        cmdLine.append("\n");
+        return cmdLine.toString();
+    }
+
+    /**
+     * Checks if quiet logging is enabled by looking for the -q or --quiet 
flag in the arguments.
+     */
+    private boolean isQuietLogging(List<String> args) {
+        return args.contains("-q") || args.contains("--quiet");
+    }
+
     public String getLogFileName() {
         return logFileName;
     }

Reply via email to