gnodet commented on code in PR #12695:
URL: https://github.com/apache/maven/pull/12695#discussion_r3743159002


##########
impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java:
##########
@@ -0,0 +1,564 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.internal.build;
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.nio.file.AtomicMoveNotSupportedException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.maven.api.MonotonicClock;
+import org.apache.maven.api.build.report.BuildReport;
+import org.apache.maven.api.build.report.BuildStatus;
+import org.apache.maven.api.build.report.FailureReport;
+import org.apache.maven.api.build.report.LogEvent;
+import org.apache.maven.api.build.report.ModuleReport;
+import org.apache.maven.api.build.report.MojoReport;
+import org.apache.maven.eventspy.AbstractEventSpy;
+import org.apache.maven.execution.BuildFailure;
+import org.apache.maven.execution.BuildSuccess;
+import org.apache.maven.execution.BuildSummary;
+import org.apache.maven.execution.ExecutionEvent;
+import org.apache.maven.execution.MavenExecutionResult;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.logging.ProjectBuildLogAppender;
+import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.project.MavenProject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Collects build lifecycle events and produces a structured {@link 
BuildReport}
+ * at the end of the session.
+ * <p>
+ * Registered as an {@link org.apache.maven.eventspy.EventSpy} via {@code 
@Named}/{@code @Singleton},
+ * following the same pattern as {@code DefaultPluginValidationManager}.
+ * <p>
+ * Thread-safe: concurrent module builds (with {@code -T}) each write to their
+ * own entry in a {@link ConcurrentHashMap}.
+ * <p>
+ * Log capture: registers a callback on {@link ProjectBuildLogAppender} to
+ * receive the already-formed {@link LogEvent} objects produced by the main
+ * logging pipeline. Uses thread-based tracking to associate events with
+ * the currently-executing mojo or module.
+ *
+ * @since 4.1.0
+ */
+@Singleton
+@Named
+public final class BuildReportCollector extends AbstractEventSpy {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(BuildReportCollector.class);
+
+    static final String REPORT_DIR = "build-reports";
+    static final String REPORT_LATEST = "build-report-latest.json";
+
+    private static final int MAX_STACKTRACE_LINES = 30;
+
+    /**
+     * Maximum number of log events captured per scope (mojo, module, or 
build).
+     * Beyond this, events are dropped and a truncation notice is appended.
+     */
+    static final int MAX_LOG_EVENTS_PER_SCOPE = 500;

Review Comment:
   The Javadoc says "Beyond this, events are dropped and a truncation notice is 
appended" but `captureLogEvent` silently drops events without appending any 
truncation notice. Either append a synthetic `LogEvent` indicating truncation 
(e.g. with level WARN and message "... N events truncated"), or update the 
Javadoc to say events are silently dropped.



##########
impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportJsonWriter.java:
##########
@@ -0,0 +1,387 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.internal.build;
+
+import org.apache.maven.api.build.report.BuildReport;
+import org.apache.maven.api.build.report.FailureReport;
+import org.apache.maven.api.build.report.LogEvent;
+import org.apache.maven.api.build.report.ModuleReport;
+import org.apache.maven.api.build.report.MojoReport;
+import org.apache.maven.api.services.BuilderProblem;
+
+/**
+ * Serializes a {@link BuildReport} to JSON without any external library 
dependency.
+ * <p>
+ * The output is human-readable (indented with 2 spaces) and designed to be
+ * stable across Maven versions — field order is fixed, and new fields are
+ * appended at the end of each object.
+ */
+final class BuildReportJsonWriter {
+
+    private BuildReportJsonWriter() {}
+
+    /**
+     * Serialize the given report to a pretty-printed JSON string.
+     */
+    static String toJson(BuildReport report) {
+        StringBuilder sb = new StringBuilder(4096);
+        writeReport(sb, report, 0);
+        sb.append('\n');
+        return sb.toString();
+    }
+
+    private static void writeReport(StringBuilder sb, BuildReport report, int 
indent) {
+        sb.append("{\n");
+        writeField(sb, indent + 1, "formatVersion", report.formatVersion());
+        writeField(sb, indent + 1, "status", report.status().name());
+        writeField(sb, indent + 1, "duration", report.duration().toString());
+        writeField(sb, indent + 1, "startTime", report.startTime().toString());
+        writeField(sb, indent + 1, "mavenVersion", report.mavenVersion());
+        writeField(sb, indent + 1, "javaVersion", report.javaVersion());
+        writeStringArray(sb, indent + 1, "goals", report.goals());
+        writeField(sb, indent + 1, "project", report.project());
+        writeField(sb, indent + 1, "multiModule", report.multiModule());
+        writeField(sb, indent + 1, "threads", report.threads());
+
+        // modules array
+        writeIndent(sb, indent + 1);
+        sb.append("\"modules\": ");
+        if (report.modules().isEmpty()) {
+            sb.append("[]");
+        } else {
+            sb.append("[\n");
+            for (int i = 0; i < report.modules().size(); i++) {
+                writeIndent(sb, indent + 2);
+                writeModule(sb, report.modules().get(i), indent + 2);
+                if (i < report.modules().size() - 1) {
+                    sb.append(',');
+                }
+                sb.append('\n');
+            }
+            writeIndent(sb, indent + 1);
+            sb.append(']');
+        }
+        sb.append(",\n");
+
+        // failures array
+        writeIndent(sb, indent + 1);
+        sb.append("\"failures\": ");
+        if (report.failures().isEmpty()) {
+            sb.append("[]");
+        } else {
+            sb.append("[\n");
+            for (int i = 0; i < report.failures().size(); i++) {
+                writeIndent(sb, indent + 2);
+                writeFailure(sb, report.failures().get(i), indent + 2);
+                if (i < report.failures().size() - 1) {
+                    sb.append(',');
+                }
+                sb.append('\n');
+            }
+            writeIndent(sb, indent + 1);
+            sb.append(']');
+        }
+        sb.append(",\n");
+
+        // problems array
+        writeIndent(sb, indent + 1);
+        sb.append("\"problems\": ");
+        if (report.problems().isEmpty()) {
+            sb.append("[]");
+        } else {
+            sb.append("[\n");
+            for (int i = 0; i < report.problems().size(); i++) {
+                writeIndent(sb, indent + 2);
+                writeProblem(sb, report.problems().get(i), indent + 2);
+                if (i < report.problems().size() - 1) {
+                    sb.append(',');
+                }
+                sb.append('\n');
+            }
+            writeIndent(sb, indent + 1);
+            sb.append(']');
+        }
+        sb.append(",\n");
+
+        // output array — build-level log lines (outside any module)
+        writeOutputArray(sb, indent + 1, report.output());
+        sb.append('\n');
+
+        writeIndent(sb, indent);
+        sb.append('}');
+    }
+
+    private static void writeProblem(StringBuilder sb, BuilderProblem problem, 
int indent) {
+        sb.append("{\n");
+        writeField(sb, indent + 1, "severity", problem.getSeverity().name());
+        writeField(sb, indent + 1, "message", problem.getMessage());
+        String source = problem.getSource();
+        if (source != null && !source.isEmpty()) {
+            writeField(sb, indent + 1, "source", source);
+        }
+        if (problem.getLineNumber() > 0) {
+            writeField(sb, indent + 1, "line", problem.getLineNumber());
+        }
+        if (problem.getColumnNumber() > 0) {
+            writeField(sb, indent + 1, "column", problem.getColumnNumber());
+        }
+        // Remove the trailing comma from the last written field
+        int lastComma = sb.lastIndexOf(",\n");
+        if (lastComma > 0) {
+            sb.replace(lastComma, lastComma + 1, "");
+        }
+        writeIndent(sb, indent);
+        sb.append('}');
+    }
+
+    private static void writeModule(StringBuilder sb, ModuleReport module, int 
indent) {

Review Comment:
   The `hasMore` parameter is unused (annotated `@SuppressWarnings("unused")`) 
and all call sites pass `true`. If trailing comma control is no longer needed, 
the parameter should be removed to reduce confusion.
   
   ```suggestion
       private static void writeNullableField(StringBuilder sb, int indent, 
String key, String value) {
   ```



-- 
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