gnodet-bot commented on code in PR #13700: URL: https://github.com/apache/maven/pull/13700#discussion_r4368568069
########## api/maven-api-core/src/main/java/org/apache/maven/api/build/report/BuildReport.java: ########## @@ -0,0 +1,228 @@ +/* + * 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.api.build.report; + +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import org.apache.maven.api.BuildInvocation; +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Immutable; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.logging.LogEvent; +import org.apache.maven.api.services.BuilderProblem; + +/** + * A structured report of a Maven build execution, persisted to + * {@code target/build-report.json} at the end of every build. + * <p> + * The report captures metadata, per-module results (including mojo execution + * timings), and any failures. It is intended to be consumed by tools, IDEs, + * CI systems, and LLM agents without having to re-run the build or parse + * console output. + * + * @since 4.1.0 + * @see ModuleReport + * @see FailureReport + */ +@Experimental +@Immutable +public interface BuildReport { + + /** + * Semantic version of the report format (e.g. {@code "1.0"}). + * <p> + * Versioning policy: + * <ul> + * <li><b>Minor</b> increments (1.0 → 1.1) are purely additive — + * new fields may appear, but existing fields keep their name, type, and + * meaning. Consumers that ignore unknown fields are forward-compatible.</li> + * <li><b>Major</b> increments (1.x → 2.0) signal a breaking change — + * fields may be removed, renamed, or change type. Consumers should check + * the major version and refuse to parse an unsupported major.</li> + * </ul> + * + * @return the semver report version string, never {@code null} + * @since 4.1.0 + */ + @Nonnull + String reportVersion(); + + /** + * The invocation context of this build: flags, properties, and platform settings + * that were active when the session started. + * + * @return the build environment, never {@code null} + * @since 4.1.0 + */ + @Nonnull + BuildInvocation environment(); + + /** + * Schema version of the report format. Consumers should check this + * to handle forward compatibility. + * + * @return the format version, currently {@code 1} + */ + int formatVersion(); Review Comment: ⚠️ **Missing `@since 4.1.0`** — still not fixed from the previous review. `formatVersion()` has no `@since` tag. Both adjacent methods — `reportVersion()` (line 68) and `environment()` (line 75) — have `@since 4.1.0`. For a new public API on a `@since 4.1.0`-annotated interface, every method needs the tag. ```suggestion /** * Schema version of the report format. Consumers should check this * to handle forward compatibility. * * @return the format version, currently {@code 1} * @since 4.1.0 */ int formatVersion(); ``` ########## impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java: ########## @@ -0,0 +1,644 @@ +/* + * 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.BuildInvocation; +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.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.logging.LogEvent; +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; +import org.slf4j.MDC; + +/** + * 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"; + static final String TRACE_ID_MDC_KEY = "otel.trace_id"; + static final String SPAN_ID_MDC_KEY = "otel.span_id"; + + private static final int MAX_STACKTRACE_LINES = 30; + + /** + * Maximum number of log events captured per scope (mojo, module, or build). + * Beyond this, events are silently dropped to prevent unbounded memory growth. + */ + static final int MAX_LOG_EVENTS_PER_SCOPE = 500; + + // ---- Mutable state, populated during the build ---- + + /** Per-project mojo tracking: project key -> list of in-flight/completed mojos. */ + private final Map<String, List<MojoTiming>> mojoTimings = new ConcurrentHashMap<>(); + + /** Per-project start instants for duration computation. */ + private final Map<String, Instant> projectStartTimes = new ConcurrentHashMap<>(); + + /** Per-project trace context captured when the project starts. */ + private final Map<String, TraceContext> projectTraceContexts = new ConcurrentHashMap<>(); + + /** Per-mojo start instants for duration computation. */ + private final Map<String, Instant> mojoStartTimes = new ConcurrentHashMap<>(); + + /** Per-mojo trace context captured when the mojo starts. */ + private final Map<String, TraceContext> mojoTraceContexts = new ConcurrentHashMap<>(); + + /** Session-level state - set once on SessionStarted. */ + private volatile MavenSession session; + + /** Build environment - captured once at SessionStarted, immutable thereafter. */ + private volatile BuildInvocation buildEnvironment; + + // ---- Log capture state ---- + + /** + * Maps thread ID -> mojo key for the currently-executing mojo on that thread. + * Lifecycle events and mojo execution run on the same thread, so this is safe + * for parallel builds with {@code -T}. + */ + private final Map<Long, String> currentMojoByThread = new ConcurrentHashMap<>(); + + /** Per-mojo log buffers: mojo key -> captured log events. */ + private final Map<String, List<LogEvent>> mojoLogBuffers = new ConcurrentHashMap<>(); + + /** + * Maps thread ID -> project key for the currently-building project on that thread. + * Used to route log events that occur between mojo executions to the module-level buffer. + */ + private final Map<Long, String> currentProjectByThread = new ConcurrentHashMap<>(); + + /** Per-module log buffers: project key -> events captured outside any mojo. */ + private final Map<String, List<LogEvent>> moduleLogBuffers = new ConcurrentHashMap<>(); + + /** Build-level log buffer: events captured outside any module lifecycle. */ + private final List<LogEvent> buildLogBuffer = Collections.synchronizedList(new ArrayList<>()); + + @Override + public void onEvent(Object event) { + if (event instanceof ExecutionEvent executionEvent) { + switch (executionEvent.getType()) { + case SessionStarted: + onSessionStarted(executionEvent); + break; + case SessionEnded: + onSessionEnded(executionEvent); + break; + case ProjectStarted: + onProjectStarted(executionEvent); + break; + case ProjectSucceeded: + case ProjectFailed: + case ProjectSkipped: + onProjectFinished(executionEvent); + break; + case MojoStarted: + onMojoStarted(executionEvent); + break; + case MojoSucceeded: + case MojoFailed: + onMojoFinished(executionEvent); + break; + case MojoSkipped: + onMojoSkipped(executionEvent); + break; + default: + break; + } + } + } + + // ---- Event handlers ---- + + private void onSessionStarted(ExecutionEvent event) { + this.session = event.getSession(); + this.buildEnvironment = DefaultBuildInvocation.from(this.session); + installLogCapture(); + } + + private void onSessionEnded(ExecutionEvent event) { + removeLogCapture(); + + MavenSession endSession = event.getSession(); + if (endSession == null) { + return; + } + + try { + BuildReport report = buildReport(endSession); + writeReport(report, endSession); + } catch (Exception e) { + // Never let the report collector crash the build + LOGGER.debug("Failed to produce build report: {}", e.getMessage(), e); Review Comment: ⚠️ **Report failure is invisible in normal operation** — still not fixed from the previous review. The IO write failure (line 575) correctly uses `LOGGER.warn`. The outer catch here — which covers `buildReport()` assembly failures — still uses `DEBUG`. A user relying on `target/build-report.json` gets no indication when it silently fails to produce one. ```suggestion LOGGER.warn("Failed to produce build report: {}", e.getMessage(), e); ``` ########## impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java: ########## @@ -0,0 +1,644 @@ +/* + * 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.BuildInvocation; +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.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.logging.LogEvent; +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; +import org.slf4j.MDC; + +/** + * 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"; + static final String TRACE_ID_MDC_KEY = "otel.trace_id"; + static final String SPAN_ID_MDC_KEY = "otel.span_id"; + + private static final int MAX_STACKTRACE_LINES = 30; + + /** + * Maximum number of log events captured per scope (mojo, module, or build). + * Beyond this, events are silently dropped to prevent unbounded memory growth. + */ + static final int MAX_LOG_EVENTS_PER_SCOPE = 500; Review Comment: ⚠️ **Silent log-event truncation — consumers cannot detect incomplete lists** — still not fixed from the previous review. Events beyond `MAX_LOG_EVENTS_PER_SCOPE = 500` are silently dropped. The Javadoc on `MojoReport.output()` says "The list may be truncated" but there is no programmatic flag (`outputTruncated`, `droppedEventCount`) in the API or the JSON output. A consumer reading 500 events cannot distinguish "exactly 500 events" from "500 events capped". Stack traces handle this correctly: `truncateStackTrace()` appends `"... N more lines truncated"`. Log event lists need the same treatment — either a `boolean outputTruncated()` in each report interface, or a sentinel final entry when the limit is hit. ########## impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java: ########## @@ -0,0 +1,644 @@ +/* + * 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.BuildInvocation; +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.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.logging.LogEvent; +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; +import org.slf4j.MDC; + +/** + * 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"; + static final String TRACE_ID_MDC_KEY = "otel.trace_id"; + static final String SPAN_ID_MDC_KEY = "otel.span_id"; + + private static final int MAX_STACKTRACE_LINES = 30; + + /** + * Maximum number of log events captured per scope (mojo, module, or build). + * Beyond this, events are silently dropped to prevent unbounded memory growth. + */ + static final int MAX_LOG_EVENTS_PER_SCOPE = 500; + + // ---- Mutable state, populated during the build ---- + + /** Per-project mojo tracking: project key -> list of in-flight/completed mojos. */ + private final Map<String, List<MojoTiming>> mojoTimings = new ConcurrentHashMap<>(); + + /** Per-project start instants for duration computation. */ + private final Map<String, Instant> projectStartTimes = new ConcurrentHashMap<>(); + + /** Per-project trace context captured when the project starts. */ + private final Map<String, TraceContext> projectTraceContexts = new ConcurrentHashMap<>(); + + /** Per-mojo start instants for duration computation. */ + private final Map<String, Instant> mojoStartTimes = new ConcurrentHashMap<>(); + + /** Per-mojo trace context captured when the mojo starts. */ + private final Map<String, TraceContext> mojoTraceContexts = new ConcurrentHashMap<>(); + + /** Session-level state - set once on SessionStarted. */ + private volatile MavenSession session; + + /** Build environment - captured once at SessionStarted, immutable thereafter. */ + private volatile BuildInvocation buildEnvironment; + + // ---- Log capture state ---- + + /** + * Maps thread ID -> mojo key for the currently-executing mojo on that thread. + * Lifecycle events and mojo execution run on the same thread, so this is safe + * for parallel builds with {@code -T}. + */ + private final Map<Long, String> currentMojoByThread = new ConcurrentHashMap<>(); + + /** Per-mojo log buffers: mojo key -> captured log events. */ + private final Map<String, List<LogEvent>> mojoLogBuffers = new ConcurrentHashMap<>(); + + /** + * Maps thread ID -> project key for the currently-building project on that thread. + * Used to route log events that occur between mojo executions to the module-level buffer. + */ + private final Map<Long, String> currentProjectByThread = new ConcurrentHashMap<>(); + + /** Per-module log buffers: project key -> events captured outside any mojo. */ + private final Map<String, List<LogEvent>> moduleLogBuffers = new ConcurrentHashMap<>(); + + /** Build-level log buffer: events captured outside any module lifecycle. */ + private final List<LogEvent> buildLogBuffer = Collections.synchronizedList(new ArrayList<>()); + + @Override + public void onEvent(Object event) { + if (event instanceof ExecutionEvent executionEvent) { + switch (executionEvent.getType()) { + case SessionStarted: + onSessionStarted(executionEvent); + break; + case SessionEnded: + onSessionEnded(executionEvent); + break; + case ProjectStarted: + onProjectStarted(executionEvent); + break; + case ProjectSucceeded: + case ProjectFailed: + case ProjectSkipped: + onProjectFinished(executionEvent); + break; + case MojoStarted: + onMojoStarted(executionEvent); + break; + case MojoSucceeded: + case MojoFailed: + onMojoFinished(executionEvent); + break; + case MojoSkipped: + onMojoSkipped(executionEvent); + break; + default: + break; + } + } + } + + // ---- Event handlers ---- + + private void onSessionStarted(ExecutionEvent event) { + this.session = event.getSession(); + this.buildEnvironment = DefaultBuildInvocation.from(this.session); + installLogCapture(); + } + + private void onSessionEnded(ExecutionEvent event) { + removeLogCapture(); + + MavenSession endSession = event.getSession(); + if (endSession == null) { + return; + } + + try { + BuildReport report = buildReport(endSession); + writeReport(report, endSession); + } catch (Exception e) { + // Never let the report collector crash the build + LOGGER.debug("Failed to produce build report: {}", e.getMessage(), e); + } + } + + private void onProjectStarted(ExecutionEvent event) { + String key = projectKey(event.getProject()); + projectStartTimes.put(key, MonotonicClock.now()); + projectTraceContexts.put(key, currentTraceContext()); + mojoTimings.putIfAbsent(key, Collections.synchronizedList(new ArrayList<>())); + moduleLogBuffers.put(key, Collections.synchronizedList(new ArrayList<>())); + currentProjectByThread.put(Thread.currentThread().getId(), key); + } + + private void onProjectFinished(ExecutionEvent event) { + // Unregister the project from this thread so subsequent log events + // fall through to the build-level buffer + currentProjectByThread.remove(Thread.currentThread().getId()); + } + + private void onMojoStarted(ExecutionEvent event) { + String mKey = mojoKey(event.getProject(), event.getMojoExecution()); + mojoStartTimes.put(mKey, MonotonicClock.now()); + mojoTraceContexts.put(mKey, currentTraceContext()); + + // Register the current mojo for this thread so the log event sink + // can associate events with this mojo execution + currentMojoByThread.put(Thread.currentThread().getId(), mKey); + mojoLogBuffers.put(mKey, Collections.synchronizedList(new ArrayList<>())); + } + + private void onMojoFinished(ExecutionEvent event) { + MojoExecution mojo = event.getMojoExecution(); + MavenProject project = event.getProject(); + String mKey = mojoKey(project, mojo); + String pKey = projectKey(project); + + // Unregister the mojo from this thread + currentMojoByThread.remove(Thread.currentThread().getId()); + + Instant now = MonotonicClock.now(); + Instant startInstant = mojoStartTimes.remove(mKey); + TraceContext traceContext = mojoTraceContexts.remove(mKey); + if (traceContext == null) { + traceContext = TraceContext.EMPTY; + } + if (startInstant == null) { + startInstant = now; + } + Duration duration = Duration.between(startInstant, now); + + BuildStatus status = + event.getType() == ExecutionEvent.Type.MojoSucceeded ? BuildStatus.SUCCESS : BuildStatus.FAILURE; + + // Drain the log buffer for this mojo + List<LogEvent> logBuffer = mojoLogBuffers.remove(mKey); + List<LogEvent> output = logBuffer != null ? List.copyOf(logBuffer) : List.of(); + + MojoTiming timing = new MojoTiming( + mojo.getGroupId(), + mojo.getArtifactId(), + mojo.getVersion(), + mojo.getGoal(), + mojo.getExecutionId(), + mojo.getLifecyclePhase(), + status, + startInstant, + duration, + traceContext.traceId, + traceContext.spanId, + output); + + mojoTimings + .computeIfAbsent(pKey, k -> Collections.synchronizedList(new ArrayList<>())) + .add(timing); + } + + private void onMojoSkipped(ExecutionEvent event) { + MojoExecution mojo = event.getMojoExecution(); + MavenProject project = event.getProject(); + String pKey = projectKey(project); + + Instant now = MonotonicClock.now(); + TraceContext traceContext = TraceContext.EMPTY; + + MojoTiming timing = new MojoTiming( + mojo.getGroupId(), + mojo.getArtifactId(), + mojo.getVersion(), + mojo.getGoal(), + mojo.getExecutionId(), + mojo.getLifecyclePhase(), + BuildStatus.SKIPPED, + now, + Duration.ZERO, + traceContext.traceId, + traceContext.spanId, + List.of()); + + mojoTimings + .computeIfAbsent(pKey, k -> Collections.synchronizedList(new ArrayList<>())) + .add(timing); + } + + // ---- Structured log capture ---- + + /** + * Registers a callback on {@link ProjectBuildLogAppender} to receive the + * already-formed {@link LogEvent} objects from the main logging pipeline. + * This eliminates the need for a separate capture path and ensures the + * build report captures the same enriched events (with sequence number, + * source metadata) as the console output. + */ + private void installLogCapture() { + ProjectBuildLogAppender.setReportCapture(this::captureLogEvent); + } + + private void removeLogCapture() { + ProjectBuildLogAppender.setReportCapture(null); + } + + /** + * Routes a pre-formed {@link LogEvent} to the appropriate buffer + * (mojo, module, or build-level) based on the current thread's + * lifecycle context. + */ + private void captureLogEvent(LogEvent event) { + long threadId = Thread.currentThread().getId(); + + // 1. Mojo-level: event belongs to the currently-executing mojo on this thread + String mKey = currentMojoByThread.get(threadId); + if (mKey != null) { + List<LogEvent> buffer = mojoLogBuffers.get(mKey); + if (buffer != null && buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { + buffer.add(event); Review Comment: ⚠️ **NEW: TOCTOU race on mojo log buffer** `mojoLogBuffers` entries are `Collections.synchronizedList` wrappers, but the compound `size() < MAX` check-then-`add()` is not externally synchronized. In a parallel build (`-T N`), two threads logging to the same mojo key can both observe `size() == 499` and both call `add()`, producing 501 events. The code's own comment at lines 350–351 explicitly identifies this exact hazard for `buildLogBuffer` and fixes it with `synchronized (buildLogBuffer)` — that fix was not applied here. ```suggestion synchronized (buffer) { if (buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { buffer.add(event); } } ``` ########## impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java: ########## @@ -0,0 +1,644 @@ +/* + * 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.BuildInvocation; +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.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.logging.LogEvent; +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; +import org.slf4j.MDC; + +/** + * 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"; + static final String TRACE_ID_MDC_KEY = "otel.trace_id"; + static final String SPAN_ID_MDC_KEY = "otel.span_id"; + + private static final int MAX_STACKTRACE_LINES = 30; + + /** + * Maximum number of log events captured per scope (mojo, module, or build). + * Beyond this, events are silently dropped to prevent unbounded memory growth. + */ + static final int MAX_LOG_EVENTS_PER_SCOPE = 500; + + // ---- Mutable state, populated during the build ---- + + /** Per-project mojo tracking: project key -> list of in-flight/completed mojos. */ + private final Map<String, List<MojoTiming>> mojoTimings = new ConcurrentHashMap<>(); + + /** Per-project start instants for duration computation. */ + private final Map<String, Instant> projectStartTimes = new ConcurrentHashMap<>(); + + /** Per-project trace context captured when the project starts. */ + private final Map<String, TraceContext> projectTraceContexts = new ConcurrentHashMap<>(); + + /** Per-mojo start instants for duration computation. */ + private final Map<String, Instant> mojoStartTimes = new ConcurrentHashMap<>(); + + /** Per-mojo trace context captured when the mojo starts. */ + private final Map<String, TraceContext> mojoTraceContexts = new ConcurrentHashMap<>(); + + /** Session-level state - set once on SessionStarted. */ + private volatile MavenSession session; + + /** Build environment - captured once at SessionStarted, immutable thereafter. */ + private volatile BuildInvocation buildEnvironment; + + // ---- Log capture state ---- + + /** + * Maps thread ID -> mojo key for the currently-executing mojo on that thread. + * Lifecycle events and mojo execution run on the same thread, so this is safe + * for parallel builds with {@code -T}. + */ + private final Map<Long, String> currentMojoByThread = new ConcurrentHashMap<>(); + + /** Per-mojo log buffers: mojo key -> captured log events. */ + private final Map<String, List<LogEvent>> mojoLogBuffers = new ConcurrentHashMap<>(); + + /** + * Maps thread ID -> project key for the currently-building project on that thread. + * Used to route log events that occur between mojo executions to the module-level buffer. + */ + private final Map<Long, String> currentProjectByThread = new ConcurrentHashMap<>(); + + /** Per-module log buffers: project key -> events captured outside any mojo. */ + private final Map<String, List<LogEvent>> moduleLogBuffers = new ConcurrentHashMap<>(); + + /** Build-level log buffer: events captured outside any module lifecycle. */ + private final List<LogEvent> buildLogBuffer = Collections.synchronizedList(new ArrayList<>()); + + @Override + public void onEvent(Object event) { + if (event instanceof ExecutionEvent executionEvent) { + switch (executionEvent.getType()) { + case SessionStarted: + onSessionStarted(executionEvent); + break; + case SessionEnded: + onSessionEnded(executionEvent); + break; + case ProjectStarted: + onProjectStarted(executionEvent); + break; + case ProjectSucceeded: + case ProjectFailed: + case ProjectSkipped: + onProjectFinished(executionEvent); + break; + case MojoStarted: + onMojoStarted(executionEvent); + break; + case MojoSucceeded: + case MojoFailed: + onMojoFinished(executionEvent); + break; + case MojoSkipped: + onMojoSkipped(executionEvent); + break; + default: + break; + } + } + } + + // ---- Event handlers ---- + + private void onSessionStarted(ExecutionEvent event) { + this.session = event.getSession(); + this.buildEnvironment = DefaultBuildInvocation.from(this.session); + installLogCapture(); + } + + private void onSessionEnded(ExecutionEvent event) { + removeLogCapture(); + + MavenSession endSession = event.getSession(); + if (endSession == null) { + return; + } + + try { + BuildReport report = buildReport(endSession); + writeReport(report, endSession); + } catch (Exception e) { + // Never let the report collector crash the build + LOGGER.debug("Failed to produce build report: {}", e.getMessage(), e); + } + } + + private void onProjectStarted(ExecutionEvent event) { + String key = projectKey(event.getProject()); + projectStartTimes.put(key, MonotonicClock.now()); + projectTraceContexts.put(key, currentTraceContext()); + mojoTimings.putIfAbsent(key, Collections.synchronizedList(new ArrayList<>())); + moduleLogBuffers.put(key, Collections.synchronizedList(new ArrayList<>())); + currentProjectByThread.put(Thread.currentThread().getId(), key); + } + + private void onProjectFinished(ExecutionEvent event) { + // Unregister the project from this thread so subsequent log events + // fall through to the build-level buffer + currentProjectByThread.remove(Thread.currentThread().getId()); + } + + private void onMojoStarted(ExecutionEvent event) { + String mKey = mojoKey(event.getProject(), event.getMojoExecution()); + mojoStartTimes.put(mKey, MonotonicClock.now()); + mojoTraceContexts.put(mKey, currentTraceContext()); + + // Register the current mojo for this thread so the log event sink + // can associate events with this mojo execution + currentMojoByThread.put(Thread.currentThread().getId(), mKey); + mojoLogBuffers.put(mKey, Collections.synchronizedList(new ArrayList<>())); + } + + private void onMojoFinished(ExecutionEvent event) { + MojoExecution mojo = event.getMojoExecution(); + MavenProject project = event.getProject(); + String mKey = mojoKey(project, mojo); + String pKey = projectKey(project); + + // Unregister the mojo from this thread + currentMojoByThread.remove(Thread.currentThread().getId()); + + Instant now = MonotonicClock.now(); + Instant startInstant = mojoStartTimes.remove(mKey); + TraceContext traceContext = mojoTraceContexts.remove(mKey); + if (traceContext == null) { + traceContext = TraceContext.EMPTY; + } + if (startInstant == null) { + startInstant = now; + } + Duration duration = Duration.between(startInstant, now); + + BuildStatus status = + event.getType() == ExecutionEvent.Type.MojoSucceeded ? BuildStatus.SUCCESS : BuildStatus.FAILURE; + + // Drain the log buffer for this mojo + List<LogEvent> logBuffer = mojoLogBuffers.remove(mKey); + List<LogEvent> output = logBuffer != null ? List.copyOf(logBuffer) : List.of(); + + MojoTiming timing = new MojoTiming( + mojo.getGroupId(), + mojo.getArtifactId(), + mojo.getVersion(), + mojo.getGoal(), + mojo.getExecutionId(), + mojo.getLifecyclePhase(), + status, + startInstant, + duration, + traceContext.traceId, + traceContext.spanId, + output); + + mojoTimings + .computeIfAbsent(pKey, k -> Collections.synchronizedList(new ArrayList<>())) + .add(timing); + } + + private void onMojoSkipped(ExecutionEvent event) { + MojoExecution mojo = event.getMojoExecution(); + MavenProject project = event.getProject(); + String pKey = projectKey(project); + + Instant now = MonotonicClock.now(); + TraceContext traceContext = TraceContext.EMPTY; + + MojoTiming timing = new MojoTiming( + mojo.getGroupId(), + mojo.getArtifactId(), + mojo.getVersion(), + mojo.getGoal(), + mojo.getExecutionId(), + mojo.getLifecyclePhase(), + BuildStatus.SKIPPED, + now, + Duration.ZERO, + traceContext.traceId, + traceContext.spanId, + List.of()); + + mojoTimings + .computeIfAbsent(pKey, k -> Collections.synchronizedList(new ArrayList<>())) + .add(timing); + } + + // ---- Structured log capture ---- + + /** + * Registers a callback on {@link ProjectBuildLogAppender} to receive the + * already-formed {@link LogEvent} objects from the main logging pipeline. + * This eliminates the need for a separate capture path and ensures the + * build report captures the same enriched events (with sequence number, + * source metadata) as the console output. + */ + private void installLogCapture() { + ProjectBuildLogAppender.setReportCapture(this::captureLogEvent); + } + + private void removeLogCapture() { + ProjectBuildLogAppender.setReportCapture(null); + } + + /** + * Routes a pre-formed {@link LogEvent} to the appropriate buffer + * (mojo, module, or build-level) based on the current thread's + * lifecycle context. + */ + private void captureLogEvent(LogEvent event) { + long threadId = Thread.currentThread().getId(); + + // 1. Mojo-level: event belongs to the currently-executing mojo on this thread + String mKey = currentMojoByThread.get(threadId); + if (mKey != null) { + List<LogEvent> buffer = mojoLogBuffers.get(mKey); + if (buffer != null && buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { + buffer.add(event); + } + return; + } + + // 2. Module-level: project is active but no mojo is running + String pKey = currentProjectByThread.get(threadId); + if (pKey != null) { + List<LogEvent> buffer = moduleLogBuffers.get(pKey); + if (buffer != null && buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { + buffer.add(event); Review Comment: ⚠️ **NEW: Same TOCTOU race on module log buffer** Same unsynchronized compound check-then-add as the mojo buffer above. Apply the same fix: ```suggestion synchronized (buffer) { if (buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { buffer.add(event); } } ``` ########## impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java: ########## @@ -0,0 +1,644 @@ +/* + * 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.BuildInvocation; +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.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.logging.LogEvent; +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; +import org.slf4j.MDC; + +/** + * 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"; + static final String TRACE_ID_MDC_KEY = "otel.trace_id"; + static final String SPAN_ID_MDC_KEY = "otel.span_id"; + + private static final int MAX_STACKTRACE_LINES = 30; + + /** + * Maximum number of log events captured per scope (mojo, module, or build). + * Beyond this, events are silently dropped to prevent unbounded memory growth. + */ + static final int MAX_LOG_EVENTS_PER_SCOPE = 500; + + // ---- Mutable state, populated during the build ---- + + /** Per-project mojo tracking: project key -> list of in-flight/completed mojos. */ + private final Map<String, List<MojoTiming>> mojoTimings = new ConcurrentHashMap<>(); + + /** Per-project start instants for duration computation. */ + private final Map<String, Instant> projectStartTimes = new ConcurrentHashMap<>(); + + /** Per-project trace context captured when the project starts. */ + private final Map<String, TraceContext> projectTraceContexts = new ConcurrentHashMap<>(); + + /** Per-mojo start instants for duration computation. */ + private final Map<String, Instant> mojoStartTimes = new ConcurrentHashMap<>(); + + /** Per-mojo trace context captured when the mojo starts. */ + private final Map<String, TraceContext> mojoTraceContexts = new ConcurrentHashMap<>(); + + /** Session-level state - set once on SessionStarted. */ + private volatile MavenSession session; + + /** Build environment - captured once at SessionStarted, immutable thereafter. */ + private volatile BuildInvocation buildEnvironment; + + // ---- Log capture state ---- + + /** + * Maps thread ID -> mojo key for the currently-executing mojo on that thread. + * Lifecycle events and mojo execution run on the same thread, so this is safe + * for parallel builds with {@code -T}. + */ + private final Map<Long, String> currentMojoByThread = new ConcurrentHashMap<>(); + + /** Per-mojo log buffers: mojo key -> captured log events. */ + private final Map<String, List<LogEvent>> mojoLogBuffers = new ConcurrentHashMap<>(); + + /** + * Maps thread ID -> project key for the currently-building project on that thread. + * Used to route log events that occur between mojo executions to the module-level buffer. + */ + private final Map<Long, String> currentProjectByThread = new ConcurrentHashMap<>(); + + /** Per-module log buffers: project key -> events captured outside any mojo. */ + private final Map<String, List<LogEvent>> moduleLogBuffers = new ConcurrentHashMap<>(); + + /** Build-level log buffer: events captured outside any module lifecycle. */ + private final List<LogEvent> buildLogBuffer = Collections.synchronizedList(new ArrayList<>()); + + @Override + public void onEvent(Object event) { + if (event instanceof ExecutionEvent executionEvent) { + switch (executionEvent.getType()) { + case SessionStarted: + onSessionStarted(executionEvent); + break; + case SessionEnded: + onSessionEnded(executionEvent); + break; + case ProjectStarted: + onProjectStarted(executionEvent); + break; + case ProjectSucceeded: + case ProjectFailed: + case ProjectSkipped: + onProjectFinished(executionEvent); + break; + case MojoStarted: + onMojoStarted(executionEvent); + break; + case MojoSucceeded: + case MojoFailed: + onMojoFinished(executionEvent); + break; + case MojoSkipped: + onMojoSkipped(executionEvent); + break; + default: + break; + } + } + } + + // ---- Event handlers ---- + + private void onSessionStarted(ExecutionEvent event) { + this.session = event.getSession(); + this.buildEnvironment = DefaultBuildInvocation.from(this.session); + installLogCapture(); + } + + private void onSessionEnded(ExecutionEvent event) { + removeLogCapture(); + + MavenSession endSession = event.getSession(); + if (endSession == null) { + return; + } + + try { + BuildReport report = buildReport(endSession); + writeReport(report, endSession); + } catch (Exception e) { + // Never let the report collector crash the build + LOGGER.debug("Failed to produce build report: {}", e.getMessage(), e); Review Comment: ⚠️ **Report failure is invisible in normal operation** — still not fixed from the previous review. The IO write failure (line 575) correctly uses `LOGGER.warn`. The outer catch here — which covers `buildReport()` assembly failures — still uses `DEBUG`. A user relying on `target/build-report.json` gets no indication when it silently fails to produce one. ```suggestion LOGGER.warn("Failed to produce build report: {}", e.getMessage(), e); ``` ########## impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java: ########## @@ -0,0 +1,644 @@ +/* + * 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.BuildInvocation; +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.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.logging.LogEvent; +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; +import org.slf4j.MDC; + +/** + * 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"; + static final String TRACE_ID_MDC_KEY = "otel.trace_id"; + static final String SPAN_ID_MDC_KEY = "otel.span_id"; + + private static final int MAX_STACKTRACE_LINES = 30; + + /** + * Maximum number of log events captured per scope (mojo, module, or build). + * Beyond this, events are silently dropped to prevent unbounded memory growth. + */ + static final int MAX_LOG_EVENTS_PER_SCOPE = 500; Review Comment: ⚠️ **Silent log-event truncation — consumers cannot detect incomplete lists** — still not fixed from the previous review. Events beyond `MAX_LOG_EVENTS_PER_SCOPE = 500` are silently dropped. The Javadoc on `MojoReport.output()` says "The list may be truncated" but there is no programmatic flag (`outputTruncated`, `droppedEventCount`) in the API or the JSON output. A consumer reading 500 events cannot distinguish "exactly 500 events" from "500 events capped". Stack traces handle this correctly: `truncateStackTrace()` appends `"... N more lines truncated"`. Log event lists need the same treatment — either a `boolean outputTruncated()` in each report interface, or a sentinel final entry when the limit is hit. ########## api/maven-api-core/src/main/java/org/apache/maven/api/build/report/BuildReport.java: ########## @@ -0,0 +1,228 @@ +/* + * 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.api.build.report; + +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import org.apache.maven.api.BuildInvocation; +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Immutable; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.logging.LogEvent; +import org.apache.maven.api.services.BuilderProblem; + +/** + * A structured report of a Maven build execution, persisted to + * {@code target/build-report.json} at the end of every build. + * <p> + * The report captures metadata, per-module results (including mojo execution + * timings), and any failures. It is intended to be consumed by tools, IDEs, + * CI systems, and LLM agents without having to re-run the build or parse + * console output. + * + * @since 4.1.0 + * @see ModuleReport + * @see FailureReport + */ +@Experimental +@Immutable +public interface BuildReport { + + /** + * Semantic version of the report format (e.g. {@code "1.0"}). + * <p> + * Versioning policy: + * <ul> + * <li><b>Minor</b> increments (1.0 → 1.1) are purely additive — + * new fields may appear, but existing fields keep their name, type, and + * meaning. Consumers that ignore unknown fields are forward-compatible.</li> + * <li><b>Major</b> increments (1.x → 2.0) signal a breaking change — + * fields may be removed, renamed, or change type. Consumers should check + * the major version and refuse to parse an unsupported major.</li> + * </ul> + * + * @return the semver report version string, never {@code null} + * @since 4.1.0 + */ + @Nonnull + String reportVersion(); + + /** + * The invocation context of this build: flags, properties, and platform settings + * that were active when the session started. + * + * @return the build environment, never {@code null} + * @since 4.1.0 + */ + @Nonnull + BuildInvocation environment(); + + /** + * Schema version of the report format. Consumers should check this + * to handle forward compatibility. + * + * @return the format version, currently {@code 1} + */ + int formatVersion(); Review Comment: ⚠️ **Missing `@since 4.1.0`** — still not fixed from the previous review. `formatVersion()` has no `@since` tag. Both adjacent methods — `reportVersion()` (line 68) and `environment()` (line 75) — have `@since 4.1.0`. For a new public API on a `@since 4.1.0`-annotated interface, every method needs the tag. ```suggestion /** * Schema version of the report format. Consumers should check this * to handle forward compatibility. * * @return the format version, currently {@code 1} * @since 4.1.0 */ int formatVersion(); ``` ########## impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java: ########## @@ -0,0 +1,644 @@ +/* + * 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.BuildInvocation; +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.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.logging.LogEvent; +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; +import org.slf4j.MDC; + +/** + * 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"; + static final String TRACE_ID_MDC_KEY = "otel.trace_id"; + static final String SPAN_ID_MDC_KEY = "otel.span_id"; + + private static final int MAX_STACKTRACE_LINES = 30; + + /** + * Maximum number of log events captured per scope (mojo, module, or build). + * Beyond this, events are silently dropped to prevent unbounded memory growth. + */ + static final int MAX_LOG_EVENTS_PER_SCOPE = 500; + + // ---- Mutable state, populated during the build ---- + + /** Per-project mojo tracking: project key -> list of in-flight/completed mojos. */ + private final Map<String, List<MojoTiming>> mojoTimings = new ConcurrentHashMap<>(); + + /** Per-project start instants for duration computation. */ + private final Map<String, Instant> projectStartTimes = new ConcurrentHashMap<>(); + + /** Per-project trace context captured when the project starts. */ + private final Map<String, TraceContext> projectTraceContexts = new ConcurrentHashMap<>(); + + /** Per-mojo start instants for duration computation. */ + private final Map<String, Instant> mojoStartTimes = new ConcurrentHashMap<>(); + + /** Per-mojo trace context captured when the mojo starts. */ + private final Map<String, TraceContext> mojoTraceContexts = new ConcurrentHashMap<>(); + + /** Session-level state - set once on SessionStarted. */ + private volatile MavenSession session; + + /** Build environment - captured once at SessionStarted, immutable thereafter. */ + private volatile BuildInvocation buildEnvironment; + + // ---- Log capture state ---- + + /** + * Maps thread ID -> mojo key for the currently-executing mojo on that thread. + * Lifecycle events and mojo execution run on the same thread, so this is safe + * for parallel builds with {@code -T}. + */ + private final Map<Long, String> currentMojoByThread = new ConcurrentHashMap<>(); + + /** Per-mojo log buffers: mojo key -> captured log events. */ + private final Map<String, List<LogEvent>> mojoLogBuffers = new ConcurrentHashMap<>(); + + /** + * Maps thread ID -> project key for the currently-building project on that thread. + * Used to route log events that occur between mojo executions to the module-level buffer. + */ + private final Map<Long, String> currentProjectByThread = new ConcurrentHashMap<>(); + + /** Per-module log buffers: project key -> events captured outside any mojo. */ + private final Map<String, List<LogEvent>> moduleLogBuffers = new ConcurrentHashMap<>(); + + /** Build-level log buffer: events captured outside any module lifecycle. */ + private final List<LogEvent> buildLogBuffer = Collections.synchronizedList(new ArrayList<>()); + + @Override + public void onEvent(Object event) { + if (event instanceof ExecutionEvent executionEvent) { + switch (executionEvent.getType()) { + case SessionStarted: + onSessionStarted(executionEvent); + break; + case SessionEnded: + onSessionEnded(executionEvent); + break; + case ProjectStarted: + onProjectStarted(executionEvent); + break; + case ProjectSucceeded: + case ProjectFailed: + case ProjectSkipped: + onProjectFinished(executionEvent); + break; + case MojoStarted: + onMojoStarted(executionEvent); + break; + case MojoSucceeded: + case MojoFailed: + onMojoFinished(executionEvent); + break; + case MojoSkipped: + onMojoSkipped(executionEvent); + break; + default: + break; + } + } + } + + // ---- Event handlers ---- + + private void onSessionStarted(ExecutionEvent event) { + this.session = event.getSession(); + this.buildEnvironment = DefaultBuildInvocation.from(this.session); + installLogCapture(); + } + + private void onSessionEnded(ExecutionEvent event) { + removeLogCapture(); + + MavenSession endSession = event.getSession(); + if (endSession == null) { + return; + } + + try { + BuildReport report = buildReport(endSession); + writeReport(report, endSession); + } catch (Exception e) { + // Never let the report collector crash the build + LOGGER.debug("Failed to produce build report: {}", e.getMessage(), e); + } + } + + private void onProjectStarted(ExecutionEvent event) { + String key = projectKey(event.getProject()); + projectStartTimes.put(key, MonotonicClock.now()); + projectTraceContexts.put(key, currentTraceContext()); + mojoTimings.putIfAbsent(key, Collections.synchronizedList(new ArrayList<>())); + moduleLogBuffers.put(key, Collections.synchronizedList(new ArrayList<>())); + currentProjectByThread.put(Thread.currentThread().getId(), key); + } + + private void onProjectFinished(ExecutionEvent event) { + // Unregister the project from this thread so subsequent log events + // fall through to the build-level buffer + currentProjectByThread.remove(Thread.currentThread().getId()); + } + + private void onMojoStarted(ExecutionEvent event) { + String mKey = mojoKey(event.getProject(), event.getMojoExecution()); + mojoStartTimes.put(mKey, MonotonicClock.now()); + mojoTraceContexts.put(mKey, currentTraceContext()); + + // Register the current mojo for this thread so the log event sink + // can associate events with this mojo execution + currentMojoByThread.put(Thread.currentThread().getId(), mKey); + mojoLogBuffers.put(mKey, Collections.synchronizedList(new ArrayList<>())); + } + + private void onMojoFinished(ExecutionEvent event) { + MojoExecution mojo = event.getMojoExecution(); + MavenProject project = event.getProject(); + String mKey = mojoKey(project, mojo); + String pKey = projectKey(project); + + // Unregister the mojo from this thread + currentMojoByThread.remove(Thread.currentThread().getId()); + + Instant now = MonotonicClock.now(); + Instant startInstant = mojoStartTimes.remove(mKey); + TraceContext traceContext = mojoTraceContexts.remove(mKey); + if (traceContext == null) { + traceContext = TraceContext.EMPTY; + } + if (startInstant == null) { + startInstant = now; + } + Duration duration = Duration.between(startInstant, now); + + BuildStatus status = + event.getType() == ExecutionEvent.Type.MojoSucceeded ? BuildStatus.SUCCESS : BuildStatus.FAILURE; + + // Drain the log buffer for this mojo + List<LogEvent> logBuffer = mojoLogBuffers.remove(mKey); + List<LogEvent> output = logBuffer != null ? List.copyOf(logBuffer) : List.of(); + + MojoTiming timing = new MojoTiming( + mojo.getGroupId(), + mojo.getArtifactId(), + mojo.getVersion(), + mojo.getGoal(), + mojo.getExecutionId(), + mojo.getLifecyclePhase(), + status, + startInstant, + duration, + traceContext.traceId, + traceContext.spanId, + output); + + mojoTimings + .computeIfAbsent(pKey, k -> Collections.synchronizedList(new ArrayList<>())) + .add(timing); + } + + private void onMojoSkipped(ExecutionEvent event) { + MojoExecution mojo = event.getMojoExecution(); + MavenProject project = event.getProject(); + String pKey = projectKey(project); + + Instant now = MonotonicClock.now(); + TraceContext traceContext = TraceContext.EMPTY; + + MojoTiming timing = new MojoTiming( + mojo.getGroupId(), + mojo.getArtifactId(), + mojo.getVersion(), + mojo.getGoal(), + mojo.getExecutionId(), + mojo.getLifecyclePhase(), + BuildStatus.SKIPPED, + now, + Duration.ZERO, + traceContext.traceId, + traceContext.spanId, + List.of()); + + mojoTimings + .computeIfAbsent(pKey, k -> Collections.synchronizedList(new ArrayList<>())) + .add(timing); + } + + // ---- Structured log capture ---- + + /** + * Registers a callback on {@link ProjectBuildLogAppender} to receive the + * already-formed {@link LogEvent} objects from the main logging pipeline. + * This eliminates the need for a separate capture path and ensures the + * build report captures the same enriched events (with sequence number, + * source metadata) as the console output. + */ + private void installLogCapture() { + ProjectBuildLogAppender.setReportCapture(this::captureLogEvent); + } + + private void removeLogCapture() { + ProjectBuildLogAppender.setReportCapture(null); + } + + /** + * Routes a pre-formed {@link LogEvent} to the appropriate buffer + * (mojo, module, or build-level) based on the current thread's + * lifecycle context. + */ + private void captureLogEvent(LogEvent event) { + long threadId = Thread.currentThread().getId(); + + // 1. Mojo-level: event belongs to the currently-executing mojo on this thread + String mKey = currentMojoByThread.get(threadId); + if (mKey != null) { + List<LogEvent> buffer = mojoLogBuffers.get(mKey); + if (buffer != null && buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { + buffer.add(event); Review Comment: ⚠️ **NEW: TOCTOU race on mojo log buffer** `mojoLogBuffers` entries are `Collections.synchronizedList` wrappers, but the compound `size() < MAX` check-then-`add()` is not externally synchronized. In a parallel build (`-T N`), two threads logging to the same mojo key can both observe `size() == 499` and both call `add()`, producing 501 events. The code's own comment at lines 350–351 explicitly identifies this exact hazard for `buildLogBuffer` and fixes it with `synchronized (buildLogBuffer)` — that fix was not applied here. ```suggestion synchronized (buffer) { if (buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { buffer.add(event); } } ``` ########## impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java: ########## @@ -0,0 +1,644 @@ +/* + * 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.BuildInvocation; +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.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.logging.LogEvent; +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; +import org.slf4j.MDC; + +/** + * 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"; + static final String TRACE_ID_MDC_KEY = "otel.trace_id"; + static final String SPAN_ID_MDC_KEY = "otel.span_id"; + + private static final int MAX_STACKTRACE_LINES = 30; + + /** + * Maximum number of log events captured per scope (mojo, module, or build). + * Beyond this, events are silently dropped to prevent unbounded memory growth. + */ + static final int MAX_LOG_EVENTS_PER_SCOPE = 500; + + // ---- Mutable state, populated during the build ---- + + /** Per-project mojo tracking: project key -> list of in-flight/completed mojos. */ + private final Map<String, List<MojoTiming>> mojoTimings = new ConcurrentHashMap<>(); + + /** Per-project start instants for duration computation. */ + private final Map<String, Instant> projectStartTimes = new ConcurrentHashMap<>(); + + /** Per-project trace context captured when the project starts. */ + private final Map<String, TraceContext> projectTraceContexts = new ConcurrentHashMap<>(); + + /** Per-mojo start instants for duration computation. */ + private final Map<String, Instant> mojoStartTimes = new ConcurrentHashMap<>(); + + /** Per-mojo trace context captured when the mojo starts. */ + private final Map<String, TraceContext> mojoTraceContexts = new ConcurrentHashMap<>(); + + /** Session-level state - set once on SessionStarted. */ + private volatile MavenSession session; + + /** Build environment - captured once at SessionStarted, immutable thereafter. */ + private volatile BuildInvocation buildEnvironment; + + // ---- Log capture state ---- + + /** + * Maps thread ID -> mojo key for the currently-executing mojo on that thread. + * Lifecycle events and mojo execution run on the same thread, so this is safe + * for parallel builds with {@code -T}. + */ + private final Map<Long, String> currentMojoByThread = new ConcurrentHashMap<>(); + + /** Per-mojo log buffers: mojo key -> captured log events. */ + private final Map<String, List<LogEvent>> mojoLogBuffers = new ConcurrentHashMap<>(); + + /** + * Maps thread ID -> project key for the currently-building project on that thread. + * Used to route log events that occur between mojo executions to the module-level buffer. + */ + private final Map<Long, String> currentProjectByThread = new ConcurrentHashMap<>(); + + /** Per-module log buffers: project key -> events captured outside any mojo. */ + private final Map<String, List<LogEvent>> moduleLogBuffers = new ConcurrentHashMap<>(); + + /** Build-level log buffer: events captured outside any module lifecycle. */ + private final List<LogEvent> buildLogBuffer = Collections.synchronizedList(new ArrayList<>()); + + @Override + public void onEvent(Object event) { + if (event instanceof ExecutionEvent executionEvent) { + switch (executionEvent.getType()) { + case SessionStarted: + onSessionStarted(executionEvent); + break; + case SessionEnded: + onSessionEnded(executionEvent); + break; + case ProjectStarted: + onProjectStarted(executionEvent); + break; + case ProjectSucceeded: + case ProjectFailed: + case ProjectSkipped: + onProjectFinished(executionEvent); + break; + case MojoStarted: + onMojoStarted(executionEvent); + break; + case MojoSucceeded: + case MojoFailed: + onMojoFinished(executionEvent); + break; + case MojoSkipped: + onMojoSkipped(executionEvent); + break; + default: + break; + } + } + } + + // ---- Event handlers ---- + + private void onSessionStarted(ExecutionEvent event) { + this.session = event.getSession(); + this.buildEnvironment = DefaultBuildInvocation.from(this.session); + installLogCapture(); + } + + private void onSessionEnded(ExecutionEvent event) { + removeLogCapture(); + + MavenSession endSession = event.getSession(); + if (endSession == null) { + return; + } + + try { + BuildReport report = buildReport(endSession); + writeReport(report, endSession); + } catch (Exception e) { + // Never let the report collector crash the build + LOGGER.debug("Failed to produce build report: {}", e.getMessage(), e); + } + } + + private void onProjectStarted(ExecutionEvent event) { + String key = projectKey(event.getProject()); + projectStartTimes.put(key, MonotonicClock.now()); + projectTraceContexts.put(key, currentTraceContext()); + mojoTimings.putIfAbsent(key, Collections.synchronizedList(new ArrayList<>())); + moduleLogBuffers.put(key, Collections.synchronizedList(new ArrayList<>())); + currentProjectByThread.put(Thread.currentThread().getId(), key); + } + + private void onProjectFinished(ExecutionEvent event) { + // Unregister the project from this thread so subsequent log events + // fall through to the build-level buffer + currentProjectByThread.remove(Thread.currentThread().getId()); + } + + private void onMojoStarted(ExecutionEvent event) { + String mKey = mojoKey(event.getProject(), event.getMojoExecution()); + mojoStartTimes.put(mKey, MonotonicClock.now()); + mojoTraceContexts.put(mKey, currentTraceContext()); + + // Register the current mojo for this thread so the log event sink + // can associate events with this mojo execution + currentMojoByThread.put(Thread.currentThread().getId(), mKey); + mojoLogBuffers.put(mKey, Collections.synchronizedList(new ArrayList<>())); + } + + private void onMojoFinished(ExecutionEvent event) { + MojoExecution mojo = event.getMojoExecution(); + MavenProject project = event.getProject(); + String mKey = mojoKey(project, mojo); + String pKey = projectKey(project); + + // Unregister the mojo from this thread + currentMojoByThread.remove(Thread.currentThread().getId()); + + Instant now = MonotonicClock.now(); + Instant startInstant = mojoStartTimes.remove(mKey); + TraceContext traceContext = mojoTraceContexts.remove(mKey); + if (traceContext == null) { + traceContext = TraceContext.EMPTY; + } + if (startInstant == null) { + startInstant = now; + } + Duration duration = Duration.between(startInstant, now); + + BuildStatus status = + event.getType() == ExecutionEvent.Type.MojoSucceeded ? BuildStatus.SUCCESS : BuildStatus.FAILURE; + + // Drain the log buffer for this mojo + List<LogEvent> logBuffer = mojoLogBuffers.remove(mKey); + List<LogEvent> output = logBuffer != null ? List.copyOf(logBuffer) : List.of(); + + MojoTiming timing = new MojoTiming( + mojo.getGroupId(), + mojo.getArtifactId(), + mojo.getVersion(), + mojo.getGoal(), + mojo.getExecutionId(), + mojo.getLifecyclePhase(), + status, + startInstant, + duration, + traceContext.traceId, + traceContext.spanId, + output); + + mojoTimings + .computeIfAbsent(pKey, k -> Collections.synchronizedList(new ArrayList<>())) + .add(timing); + } + + private void onMojoSkipped(ExecutionEvent event) { + MojoExecution mojo = event.getMojoExecution(); + MavenProject project = event.getProject(); + String pKey = projectKey(project); + + Instant now = MonotonicClock.now(); + TraceContext traceContext = TraceContext.EMPTY; + + MojoTiming timing = new MojoTiming( + mojo.getGroupId(), + mojo.getArtifactId(), + mojo.getVersion(), + mojo.getGoal(), + mojo.getExecutionId(), + mojo.getLifecyclePhase(), + BuildStatus.SKIPPED, + now, + Duration.ZERO, + traceContext.traceId, + traceContext.spanId, + List.of()); + + mojoTimings + .computeIfAbsent(pKey, k -> Collections.synchronizedList(new ArrayList<>())) + .add(timing); + } + + // ---- Structured log capture ---- + + /** + * Registers a callback on {@link ProjectBuildLogAppender} to receive the + * already-formed {@link LogEvent} objects from the main logging pipeline. + * This eliminates the need for a separate capture path and ensures the + * build report captures the same enriched events (with sequence number, + * source metadata) as the console output. + */ + private void installLogCapture() { + ProjectBuildLogAppender.setReportCapture(this::captureLogEvent); + } + + private void removeLogCapture() { + ProjectBuildLogAppender.setReportCapture(null); + } + + /** + * Routes a pre-formed {@link LogEvent} to the appropriate buffer + * (mojo, module, or build-level) based on the current thread's + * lifecycle context. + */ + private void captureLogEvent(LogEvent event) { + long threadId = Thread.currentThread().getId(); + + // 1. Mojo-level: event belongs to the currently-executing mojo on this thread + String mKey = currentMojoByThread.get(threadId); + if (mKey != null) { + List<LogEvent> buffer = mojoLogBuffers.get(mKey); + if (buffer != null && buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { + buffer.add(event); + } + return; + } + + // 2. Module-level: project is active but no mojo is running + String pKey = currentProjectByThread.get(threadId); + if (pKey != null) { + List<LogEvent> buffer = moduleLogBuffers.get(pKey); + if (buffer != null && buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { + buffer.add(event); Review Comment: ⚠️ **NEW: Same TOCTOU race on module log buffer** Same unsynchronized compound check-then-add as the mojo buffer above. Apply the same fix: ```suggestion synchronized (buffer) { if (buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { buffer.add(event); } } ``` -- 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]
