diff --git a/apache-maven/pom.xml b/apache-maven/pom.xml index 927ba48766ca..1d51f405f9e2 100644 --- a/apache-maven/pom.xml +++ b/apache-maven/pom.xml @@ -78,13 +78,6 @@ under the License. ${slf4jVersion} runtime - - - org.slf4j - jul-to-slf4j - ${slf4jVersion} - runtime - org.apache.maven.resolver maven-resolver-connector-basic diff --git a/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/Options.java b/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/Options.java index d2bf596cd916..7fa14a596b18 100644 --- a/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/Options.java +++ b/api/maven-api-cli/src/main/java/org/apache/maven/api/cli/Options.java @@ -182,6 +182,43 @@ public interface Options { @Nonnull Optional color(); + /** + * Returns the console output mode. + *

+ * Supported modes: + *

+ * + * @return an {@link Optional} containing the console mode, or empty if not set + * @since 4.1.0 + */ + @Nonnull + Optional console(); + + /** + * Returns the warning display mode. + *

+ * Controls how build warnings (diagnostics) are displayed: + *

    + *
  • {@code "summary"} (default) — collect warnings, show deduplicated summary at end of build
  • + *
  • {@code "all"} — show warnings inline as they occur AND show summary at end
  • + *
  • {@code "none"} — suppress the diagnostic summary entirely
  • + *
  • {@code "fail"} — show summary AND fail the build if any warnings exist
  • + *
+ * + * @return an {@link Optional} containing the warning mode, or empty if not set + * @since 4.1.0 + */ + @Nonnull + Optional warningMode(); + /** * Indicates whether Maven should operate in offline mode. * @@ -220,4 +257,4 @@ default void warnAboutDeprecatedOptions(@Nonnull ParserRequest request, @Nonnull * @param printWriter the string consumer to use for output */ void displayHelp(@Nonnull ParserRequest request, @Nonnull Consumer printWriter); -} +} \ No newline at end of file diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/BuildReport.java b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/BuildReport.java new file mode 100644 index 000000000000..7cd7975b8871 --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/BuildReport.java @@ -0,0 +1,195 @@ +/* + * 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.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; +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. + *

+ * 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 +public interface BuildReport { + + /** + * Schema version of the report format. Consumers should check this + * to handle forward compatibility. + * + * @return the format version, currently {@code 1} + */ + int formatVersion(); + + /** + * The overall build status. + * + * @return the build outcome, never {@code null} + */ + @Nonnull + BuildStatus status(); + + /** + * Wall-clock duration of the entire build. + * + * @return the total duration, never {@code null} + */ + @Nonnull + Duration duration(); + + /** + * When the build started (wall-clock time). + * + * @return the start instant, never {@code null} + */ + @Nonnull + Instant startTime(); + + /** + * The Maven version that produced this report. + * + * @return the Maven version string, never {@code null} + */ + @Nonnull + String mavenVersion(); + + /** + * The Java version used for the build. + * + * @return the Java version string, never {@code null} + */ + @Nonnull + String javaVersion(); + + /** + * The goals or phases that were requested. + * + * @return the list of goals, never {@code null} + */ + @Nonnull + List goals(); + + /** + * The GAV of the top-level project ({@code groupId:artifactId:version}). + * + * @return the project identifier, never {@code null} + */ + @Nonnull + String project(); + + /** + * Whether this was a multi-module (reactor) build. + * + * @return {@code true} for multi-module builds + */ + boolean multiModule(); + + /** + * The degree of concurrency ({@code -T} flag), or 1 for sequential builds. + * + * @return the thread count + */ + int threads(); + + /** + * Per-module build results, in reactor execution order. + * + * @return the module reports, never {@code null} + */ + @Nonnull + List modules(); + + /** + * Failures that occurred during the build, if any. + * + * @return the failure reports, never {@code null}; empty if the build succeeded + */ + @Nonnull + List failures(); + + /** + * Structured problems (warnings, errors) reported during the build by + * Maven itself or by plugins. + * + * @return the problems, never {@code null}; empty if none were reported + * @since 4.1.0 + */ + @Nonnull + List problems(); + + /** + * Structured log events captured outside of any module's lifecycle — + * Maven startup messages, reactor ordering, and the final reactor summary. + *

+ * For per-module events see {@link ModuleReport#output()}, and for + * per-mojo events see {@link MojoReport#output()}. + *

+ * Together, {@code BuildReport.output()}, {@code ModuleReport.output()}, + * and {@code MojoReport.output()} form a non-overlapping partition of + * the full build log. + * + * @return the captured log events, never {@code null}; may be empty + */ + @Nonnull + List output(); + + /** + * Find a module report by its GAV identifier. + *

+ * The identifier format is {@code "groupId:artifactId:version"}, matching + * the format returned by {@link ModuleReport#id()} and used in + * {@link FailureReport#module()}. + * + * @param moduleId the module GAV string + * (e.g. {@code "org.apache.maven:maven-core:4.1.0-SNAPSHOT"}) + * @return the matching module report, or empty if not found + */ + @Nonnull + default Optional findModule(String moduleId) { + Objects.requireNonNull(moduleId); + return modules().stream().filter(m -> moduleId.equals(m.id())).findFirst(); + } + + /** + * Find the module report that corresponds to a given failure. + * + * @param failure the failure report + * @return the matching module report, or empty if not found + */ + @Nonnull + default Optional findModule(FailureReport failure) { + Objects.requireNonNull(failure); + return findModule(failure.module()); + } +} diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/BuildStatus.java b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/BuildStatus.java new file mode 100644 index 000000000000..1ee25f8cb3a6 --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/BuildStatus.java @@ -0,0 +1,44 @@ +/* + * 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 org.apache.maven.api.annotations.Experimental; + +/** + * The outcome of a build, module, or mojo execution. + * + * @since 4.1.0 + */ +@Experimental +public enum BuildStatus { + /** + * Completed successfully. + */ + SUCCESS, + + /** + * Failed with an error. + */ + FAILURE, + + /** + * Skipped (e.g. because a dependency failed). + */ + SKIPPED +} diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/FailureReport.java b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/FailureReport.java new file mode 100644 index 000000000000..20e5d95b8aea --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/FailureReport.java @@ -0,0 +1,89 @@ +/* + * 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.Instant; + +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.Nullable; + +/** + * Details about a build failure. + * + * @since 4.1.0 + * @see BuildReport#failures() + */ +@Experimental +public interface FailureReport { + + /** + * The GAV of the module where the failure occurred + * ({@code groupId:artifactId:version}). + * + * @return the module identifier, never {@code null} + */ + @Nonnull + String module(); + + /** + * The mojo that failed, formatted as {@code artifactId:version:goal} + * (e.g. {@code "maven-compiler-plugin:3.15.0:compile"}). + * + * @return the mojo identifier, or {@code null} if the failure was not mojo-specific + */ + @Nullable + String mojo(); + + /** + * When the failure occurred (wall-clock time). + * + * @return the failure instant, never {@code null} + */ + @Nonnull + Instant timestamp(); + + /** + * The simple class name of the root cause exception + * (e.g. {@code "MojoFailureException"}, {@code "LifecycleExecutionException"}). + *

+ * Useful for programmatic triage — tools can pattern-match on known + * exception types without parsing the message. + * + * @return the exception type name, or {@code null} if unavailable + */ + @Nullable + String exceptionType(); + + /** + * The exception message. + * + * @return the error message, never {@code null} + */ + @Nonnull + String message(); + + /** + * The exception stack trace, truncated to a reasonable length. + * + * @return the stack trace string, or {@code null} if unavailable + */ + @Nullable + String stackTrace(); +} diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/LogEvent.java b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/LogEvent.java new file mode 100644 index 000000000000..e8575ded3d01 --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/LogEvent.java @@ -0,0 +1,167 @@ +/* + * 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.Instant; + +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.Nullable; + +/** + * A structured log event captured during the build. + *

+ * Each event carries the log level, timestamp, message, and optionally + * the logger name and a stack trace. This replaces raw log line strings + * in the build report, enabling programmatic filtering by level and + * correlation by timestamp. + *

+ * Events originating from the Maven Log API or from JUL + * ({@code java.util.logging}) carry additional source metadata: the + * source class name, source method name, and thread identifier. + * For Log API events the source class name is the mojo implementation + * FQCN; for JUL events it comes from {@code LogRecord}. Events from + * direct SLF4J logging have these fields set to {@code null}. + * + * @since 4.1.0 + */ +@Experimental +public interface LogEvent { + + /** + * When this log event was produced (wall-clock time). + * + * @return the event instant, never {@code null} + */ + @Nonnull + Instant timestamp(); + + /** + * The severity level of this log event. + * + * @return the log level, never {@code null} + */ + @Nonnull + LogLevel level(); + + /** + * The log message, without level prefix or timestamp formatting. + * + * @return the formatted message, never {@code null} + */ + @Nonnull + String message(); + + /** + * The name of the logger that produced this event + * (e.g. {@code "org.apache.maven.plugins.compiler.CompilerMojo"}). + * + * @return the logger name, or {@code null} if unavailable + */ + @Nullable + String loggerName(); + + /** + * The stack trace associated with this event, if an exception was logged. + *

+ * The trace is formatted as a multi-line string and may be truncated + * for very deep stack traces. + * + * @return the stack trace string, or {@code null} if no exception was logged + */ + @Nullable + String stackTrace(); + + /** + * The fully formatted log line as rendered for console output, including + * the level prefix, timestamp, and any ANSI styling applied by the logger. + *

+ * This is the string that would be printed to the terminal in verbose mode. + * Console renderers that just need pass-through output can use this directly, + * while renderers that apply custom formatting (e.g. rich mode) can use the + * structured fields ({@link #level()}, {@link #message()}) instead. + *

+ * May be {@code null} if the event was created outside the SLF4J pipeline + * (e.g. in tests or by programmatic construction). + * + * @return the formatted log line, or {@code null} + */ + @Nullable + String formattedMessage(); + + // ---- Source metadata (populated for Log API and JUL events) ---- + + /** + * The fully qualified class name of the source that issued the log call. + *

+ * For Maven Log API events this is the mojo implementation class name. + * For JUL events it is the value from {@code LogRecord.getSourceClassName()}. + * For direct SLF4J logging it is {@code null}. + * + * @return the source class name, or {@code null} + * @since 4.1.0 + */ + @Nullable + default String sourceClassName() { + return null; + } + + /** + * The method name of the source that issued the log call. + *

+ * For Maven Log API events this is resolved via {@link StackWalker}. + * For JUL events it is the value from {@code LogRecord.getSourceMethodName()}. + * For direct SLF4J logging it is {@code null}. + * + * @return the source method name, or {@code null} + * @since 4.1.0 + */ + @Nullable + default String sourceMethodName() { + return null; + } + + /** + * The thread identifier from which this log event originated. + *

+ * Populated for both Log API and JUL events. Returns {@code -1} + * if the thread ID is not available (i.e. for direct SLF4J events). + * + * @return the thread ID, or {@code -1} if unavailable + * @since 4.1.0 + */ + default long threadId() { + return -1; + } + + /** + * A monotonically increasing sequence number for total ordering of + * log events, useful when multiple events share the same timestamp. + *

+ * Assigned by the logging pipeline when the event is captured, + * providing a global ordering across all event sources (Log API, + * JUL, and direct SLF4J). + * + * @return the sequence number, always non-negative + * @since 4.1.0 + */ + default long sequenceNumber() { + return -1; + } +} diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/LogLevel.java b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/LogLevel.java new file mode 100644 index 000000000000..684ea610a5bc --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/LogLevel.java @@ -0,0 +1,36 @@ +/* + * 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 org.apache.maven.api.annotations.Experimental; + +/** + * Log severity levels, mirroring the standard SLF4J levels. + * + * @since 4.1.0 + * @see LogEvent#level() + */ +@Experimental +public enum LogLevel { + TRACE, + DEBUG, + INFO, + WARN, + ERROR +} diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/ModuleReport.java b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/ModuleReport.java new file mode 100644 index 000000000000..7746545a4217 --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/ModuleReport.java @@ -0,0 +1,134 @@ +/* + * 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.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; + +/** + * Build results for a single module in a reactor build. + * + * @since 4.1.0 + * @see BuildReport#modules() + */ +@Experimental +public interface ModuleReport { + + /** + * The module's group ID. + * + * @return the group ID, never {@code null} + */ + @Nonnull + String groupId(); + + /** + * The module's artifact ID. + * + * @return the artifact ID, never {@code null} + */ + @Nonnull + String artifactId(); + + /** + * The module's version. + * + * @return the version string, never {@code null} + */ + @Nonnull + String version(); + + /** + * The build outcome for this module. + * + * @return the status, never {@code null} + */ + @Nonnull + BuildStatus status(); + + /** + * When this module started building (wall-clock time). + * + * @return the start instant, never {@code null} + */ + @Nonnull + Instant startTime(); + + /** + * How long this module took to build. + * + * @return the duration, never {@code null} + */ + @Nonnull + Duration duration(); + + /** + * The mojo executions that ran within this module, in execution order. + * + * @return the mojo reports, never {@code null} + */ + @Nonnull + List mojos(); + + /** + * Structured log events captured during this module's build lifecycle + * but outside any mojo execution — dependency resolution messages, + * resource copying, and other Maven infrastructure output. + *

+ * For per-mojo events see {@link MojoReport#output()}. + * + * @return the captured log events, never {@code null}; may be empty + */ + @Nonnull + List output(); + + /** + * The module identifier formatted as {@code "groupId:artifactId:version"}. + *

+ * This matches the format used by {@link FailureReport#module()}, allowing + * direct lookup from a failure report. + * + * @return the GAV string, never {@code null} + */ + @Nonnull + default String id() { + return groupId() + ":" + artifactId() + ":" + version(); + } + + /** + * Find a mojo execution by its identifier string. + *

+ * The identifier format is {@code "artifactId:version:goal"}, matching + * the format used by {@link FailureReport#mojo()}. + * + * @param mojoId the mojo identifier (e.g. {@code "maven-compiler-plugin:3.15.0:compile"}) + * @return the matching mojo report, or empty if not found + */ + @Nonnull + default Optional findMojo(String mojoId) { + Objects.requireNonNull(mojoId); + return mojos().stream().filter(m -> mojoId.equals(m.id())).findFirst(); + } +} diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/MojoReport.java b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/MojoReport.java new file mode 100644 index 000000000000..76001babbd6b --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/MojoReport.java @@ -0,0 +1,138 @@ +/* + * 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 org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.Nullable; + +/** + * Report for a single mojo (plugin goal) execution within a module. + * + * @since 4.1.0 + * @see ModuleReport#mojos() + */ +@Experimental +public interface MojoReport { + + /** + * The plugin's group ID. + * + * @return the group ID, never {@code null} + */ + @Nonnull + String groupId(); + + /** + * The plugin's artifact ID. + * + * @return the artifact ID, never {@code null} + */ + @Nonnull + String artifactId(); + + /** + * The plugin version. + * + * @return the version string, never {@code null} + */ + @Nonnull + String version(); + + /** + * The goal that was executed (e.g. {@code "compile"}, {@code "test"}). + * + * @return the goal name, never {@code null} + */ + @Nonnull + String goal(); + + /** + * The execution ID (e.g. {@code "default-compile"}). + * + * @return the execution ID, or {@code null} if not set + */ + @Nullable + String executionId(); + + /** + * The lifecycle phase this mojo was bound to (e.g. {@code "compile"}, {@code "test"}). + * + * @return the phase name, or {@code null} if invoked directly + */ + @Nullable + String phase(); + + /** + * The outcome of this mojo execution. + * + * @return the status, never {@code null} + */ + @Nonnull + BuildStatus status(); + + /** + * When this mojo execution started (wall-clock time). + * + * @return the start instant, never {@code null} + */ + @Nonnull + Instant startTime(); + + /** + * How long this mojo execution took. + * + * @return the duration, never {@code null} + */ + @Nonnull + Duration duration(); + + /** + * Structured log events captured during this mojo's execution. + *

+ * The list may be truncated if the mojo produced excessive output. + *

+ * This captures all SLF4J output that occurred on the mojo's execution + * thread between the mojo's start and finish events, regardless of + * whether the mojo used the legacy {@code Mojo.getLog()}, the Maven 4 + * injected {@code Log}, or plain SLF4J. + * + * @return the captured log events, never {@code null}; may be empty + * @since 4.1.0 + */ + @Nonnull + List output(); + + /** + * The mojo identifier formatted as {@code "artifactId:version:goal"}. + *

+ * This matches the format used by {@link FailureReport#mojo()}, allowing + * direct lookup via {@link ModuleReport#findMojo(String)}. + * + * @return the mojo identifier string, never {@code null} + */ + @Nonnull + default String id() { + return artifactId() + ":" + version() + ":" + goal(); + } +} diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/package-info.java b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/package-info.java new file mode 100644 index 000000000000..dd7d0572dd40 --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/build/report/package-info.java @@ -0,0 +1,38 @@ +/* + * 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. + */ + +/** + * Structured build report data model. + *

+ * The {@link org.apache.maven.api.build.report.BuildReport} is the root of a structured + * representation of a Maven build execution. It is persisted to + * {@code target/build-report.json} at the end of every build and can be consumed + * by tools, CI systems, IDEs, and LLM agents without re-running the build or + * parsing console output. + *

+ * Build problems (warnings, errors) are represented as + * {@link org.apache.maven.api.services.BuilderProblem} instances and included + * in the report for downstream analysis. + * + * @since 4.1.0 + */ +@Experimental +package org.apache.maven.api.build.report; + +import org.apache.maven.api.annotations.Experimental; diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/plugin/Log.java b/api/maven-api-core/src/main/java/org/apache/maven/api/plugin/Log.java index 23175d07b1f7..9c020506f151 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/plugin/Log.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/plugin/Log.java @@ -38,70 +38,48 @@ public interface Log { /** * {@return true if the trace error level is enabled} - *

- * The default implementation returns {@code false} for backward - * compatibility with existing {@code Log} implementations. + * @since 4.1.0 */ - default boolean isTraceEnabled() { - return false; - } + boolean isTraceEnabled(); /** - * Sends a message to the user at the trace error level. + * Sends a message to the user in the trace error level. *

- * Trace is the most verbose level, intended for Maven core internals - * such as resolver negotiation, model interpolation, and lifecycle - * ordering details. Use {@link #debug(CharSequence)} instead for - * messages that help users investigate their build - * (for instance, why a module was recompiled). - *

- * The default implementation is a no-op for backward compatibility. + * Trace is intended for Maven core internals and low-level framework + * diagnostics. Plugin authors should normally use {@link #debug} for + * developer-facing diagnostic output. * * @param content the message to log + * @since 4.1.0 */ - default void trace(CharSequence content) {} + void trace(CharSequence content); /** * Sends a message (and accompanying exception) to the user at the trace error level. - * The error's stacktrace will be output when this error level is enabled. - *

- * The default implementation is a no-op for backward compatibility. * * @param content the message to log * @param error the error that caused this log + * @since 4.1.0 */ - default void trace(CharSequence content, Throwable error) {} + void trace(CharSequence content, Throwable error); /** - * Sends an exception to the user at the trace error level. - * The stack trace for this exception will be output when this error level is enabled. - *

- * The default implementation is a no-op for backward compatibility. + * Sends an exception to the user in the trace error level. * * @param error the error that caused this log + * @since 4.1.0 */ - default void trace(Throwable error) {} + void trace(Throwable error); /** - * Sends a lazily-computed message at the trace error level. - * The supplier is only evaluated if trace is enabled. - *

- * The default implementation is a no-op for backward compatibility. - * - * @param content the message supplier + * @since 4.1.0 */ - default void trace(Supplier content) {} + void trace(Supplier content); /** - * Sends a lazily-computed message (and accompanying exception) at the trace error level. - * The supplier is only evaluated if trace is enabled. - *

- * The default implementation is a no-op for backward compatibility. - * - * @param content the message supplier - * @param error the error that caused this log + * @since 4.1.0 */ - default void trace(Supplier content, Throwable error) {} + void trace(Supplier content, Throwable error); /** * {@return true if the debug error level is enabled} @@ -111,10 +89,9 @@ default void trace(Supplier content, Throwable error) {} /** * Sends a message to the user in the debug error level. *

- * Debug is intended for messages that help users investigate - * their build — for example, why a module was recompiled or what - * classpath was resolved. For Maven core internals, use - * {@link #trace(CharSequence)} instead. + * Debug is the recommended level for diagnostic output that helps + * plugin users troubleshoot build problems (e.g. resolved paths, + * computed values). For Maven core internals, prefer {@link #trace}. * * @param content the message to log */ @@ -242,22 +219,16 @@ default void trace(Supplier content, Throwable error) {} /** * Returns a child logger whose name is derived from this logger's name - * by appending a dot and the given suffix. - * - *

For example, if a plugin's logger is named - * {@code "org.apache.maven.plugins.compiler.CompilerMojo"}, - * then {@code child("diagnostics")} returns a logger named - * {@code "org.apache.maven.plugins.compiler.CompilerMojo.diagnostics"}. - * This lets sub-components log under an independently filterable name - * without requiring a separate injection point.

- * - *

The default implementation returns {@code this}, so existing - * {@code Log} implementations continue to work without changes. - * Implementations that wrap a hierarchical logging backend (such as - * SLF4J) should override this to create a real child logger.

+ * by appending {@code "." + name}. This allows plugins to create + * sub-loggers for different concerns while keeping hierarchical level + * control (e.g. setting the level for the parent silences the children). + *

+ * The default implementation returns {@code this} so that existing + * implementations continue to work without changes. * - * @param name the suffix to append (must not be {@code null} or blank) - * @return a child logger — never {@code null} + * @param name the child logger name segment (must not be {@code null}) + * @return a child {@code Log}, never {@code null} + * @since 4.1.0 */ default Log child(String name) { return this; diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/BuilderProblem.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/BuilderProblem.java index 9e4cb45ae9dc..e486cbaf2b2d 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/services/BuilderProblem.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/BuilderProblem.java @@ -22,15 +22,25 @@ import org.apache.maven.api.annotations.Immutable; import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.annotations.Nullable; +import org.apache.maven.api.annotations.ThreadSafe; /** - * Describes a problem that was encountered during project building. A problem can either be an exception that was - * thrown or a simple string message. In addition, a problem carries a hint about its source. + * Describes a problem that was encountered during project building or + * build execution. A problem can either be an exception that was thrown + * or a simple string message. In addition, a problem carries a hint + * about its source. + *

+ * Since 4.1.0, problems can optionally carry a deduplication + * {@link #getKey() key}, an actionable {@link #getSuggestion() suggestion}, + * and a {@link #getDocumentationUrl() documentation URL}. These fields + * enable richer build reports and a deduplicated warning summary at + * the end of the build. * * @since 4.0.0 */ @Experimental @Immutable +@ThreadSafe public interface BuilderProblem { /** @@ -94,6 +104,68 @@ public interface BuilderProblem { @Nonnull Severity getSeverity(); + /** + * Gets a stable deduplication key for this problem. + *

+ * When multiple modules produce the same warning (e.g. a deprecated + * POM element), reporting it with the same key allows the build report + * to count occurrences instead of repeating the message. A key such as + * {@code "deprecated-modules"} or {@code "compiler.unchecked:Foo.java:42"} + * should be unique per logical problem but identical across modules + * that encounter the same issue. + *

+ * If this returns {@code null}, the problem is not deduplicated. + * + * @return the deduplication key, or {@code null} if not applicable + * @since 4.1.0 + */ + @Nullable + default String getKey() { + return null; + } + + /** + * Gets an actionable suggestion for resolving this problem. + *

+ * For example, a deprecation warning for {@code } might + * suggest {@code "Use instead of "}. + * + * @return the suggestion text, or {@code null} if no suggestion is available + * @since 4.1.0 + */ + @Nullable + default String getSuggestion() { + return null; + } + + /** + * Gets a URL pointing to documentation relevant to this problem. + *

+ * For example, a warning about the deprecated {@code system} scope + * might link to the Maven dependency scope migration guide. + * + * @return the documentation URL, or {@code null} if not available + * @since 4.1.0 + */ + @Nullable + default String getDocumentationUrl() { + return null; + } + + /** + * Creates a new builder for constructing {@link BuilderProblem} instances. + *

+ * This is the recommended way for plugins and extensions to create + * structured problems to report via {@link DiagnosticReporter}. + * + * @return a new builder, never {@code null} + * @since 4.1.0 + */ + @Nonnull + static Builder builder() { + return new Builder(); + } + /** * The different severity levels for a problem, in decreasing order. * @@ -103,6 +175,206 @@ public interface BuilderProblem { enum Severity { FATAL, // ERROR, // - WARNING // + WARNING, // + INFO // + } + + /** + * A builder for constructing immutable {@link BuilderProblem} instances. + *

+ * Example usage: + *

{@code
+     * BuilderProblem problem = BuilderProblem.builder()
+     *     .severity(Severity.WARNING)
+     *     .message("source/target value 8 is obsolete")
+     *     .key("compiler:obsolete-source-target")
+     *     .source("maven-compiler-plugin:3.15.0:compile")
+     *     .suggestion("Update maven.compiler.source to 11 or higher")
+     *     .documentationUrl("https://maven.apache.org/plugins/maven-compiler-plugin/")
+     *     .build();
+     * }
+ * + * @since 4.1.0 + */ + final class Builder { + private String source = ""; + private int lineNumber = -1; + private int columnNumber = -1; + private Exception exception; + private String message = ""; + private Severity severity = Severity.WARNING; + private String key; + private String suggestion; + private String documentationUrl; + + Builder() {} + + @Nonnull + public Builder source(@Nullable String source) { + this.source = source != null ? source : ""; + return this; + } + + @Nonnull + public Builder lineNumber(int lineNumber) { + this.lineNumber = lineNumber; + return this; + } + + @Nonnull + public Builder columnNumber(int columnNumber) { + this.columnNumber = columnNumber; + return this; + } + + @Nonnull + public Builder exception(@Nullable Exception exception) { + this.exception = exception; + return this; + } + + @Nonnull + public Builder message(@Nonnull String message) { + this.message = message; + return this; + } + + @Nonnull + public Builder severity(@Nonnull Severity severity) { + this.severity = severity; + return this; + } + + @Nonnull + public Builder key(@Nullable String key) { + this.key = key; + return this; + } + + @Nonnull + public Builder suggestion(@Nullable String suggestion) { + this.suggestion = suggestion; + return this; + } + + @Nonnull + public Builder documentationUrl(@Nullable String documentationUrl) { + this.documentationUrl = documentationUrl; + return this; + } + + @Nonnull + public BuilderProblem build() { + return new DefaultProblem( + source, lineNumber, columnNumber, exception, message, severity, key, suggestion, documentationUrl); + } + + /** + * Immutable problem implementation returned by the builder. + * This is intentionally package-private — callers use the + * {@link BuilderProblem} interface. + */ + @SuppressWarnings("checkstyle:ParameterNumber") + private record DefaultProblem( + String source, + int lineNumber, + int columnNumber, + Exception exception, + String message, + Severity severity, + String key, + String suggestion, + String documentationUrl) + implements BuilderProblem { + + @Override + @Nonnull + public String getSource() { + return source != null ? source : ""; + } + + @Override + public int getLineNumber() { + return lineNumber; + } + + @Override + public int getColumnNumber() { + return columnNumber; + } + + @Override + @Nonnull + public String getLocation() { + StringBuilder buffer = new StringBuilder(256); + if (source != null && !source.isEmpty()) { + buffer.append(source); + } + if (lineNumber > 0) { + if (!buffer.isEmpty()) { + buffer.append(", "); + } + buffer.append("line ").append(lineNumber); + } + if (columnNumber > 0) { + if (!buffer.isEmpty()) { + buffer.append(", "); + } + buffer.append("column ").append(columnNumber); + } + return buffer.toString(); + } + + @Override + @Nullable + public Exception getException() { + return exception; + } + + @Override + @Nonnull + public String getMessage() { + return message != null ? message : ""; + } + + @Override + @Nonnull + public Severity getSeverity() { + return severity != null ? severity : Severity.WARNING; + } + + @Override + @Nullable + public String getKey() { + return key; + } + + @Override + @Nullable + public String getSuggestion() { + return suggestion; + } + + @Override + @Nullable + public String getDocumentationUrl() { + return documentationUrl; + } + + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(128); + buffer.append('[').append(getSeverity()).append("]"); + String msg = getMessage(); + if (!msg.isEmpty()) { + buffer.append(" ").append(msg); + } + String loc = getLocation(); + if (!loc.isEmpty()) { + buffer.append(" @ ").append(loc); + } + return buffer.toString(); + } + } } -} +} \ No newline at end of file diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/services/DiagnosticReporter.java b/api/maven-api-core/src/main/java/org/apache/maven/api/services/DiagnosticReporter.java new file mode 100644 index 000000000000..2b3f13f863c9 --- /dev/null +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/services/DiagnosticReporter.java @@ -0,0 +1,111 @@ +/* + * 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.services; + +import org.apache.maven.api.Service; +import org.apache.maven.api.annotations.Experimental; +import org.apache.maven.api.annotations.Nonnull; +import org.apache.maven.api.annotations.Nullable; +import org.apache.maven.api.annotations.ThreadSafe; + +/** + * Service for reporting structured build diagnostics (warnings, errors, + * informational messages) that will appear in the build report and in + * the {@code mvnlog} output. + *

+ * This is the recommended way for Maven 4 plugins and extensions to + * report build problems with structured metadata (deduplication key, + * actionable suggestion, documentation URL). Problems reported through + * this service are: + *

    + *
  • Deduplicated by {@link BuilderProblem#getKey() key} across modules
  • + *
  • Included in the JSON build report ({@code build-report-*.json})
  • + *
  • Shown in the warning summary at the end of the build
  • + *
  • Displayed with structured details by {@code mvnlog --diagnostics}
  • + *
+ *

+ * Plugins can inject this service via {@code @Inject} or retrieve it + * from the session: + *

{@code
+ * @Inject DiagnosticReporter diagnosticReporter;
+ *
+ * public void execute() {
+ *     diagnosticReporter.report(BuilderProblem.builder()
+ *         .severity(BuilderProblem.Severity.WARNING)
+ *         .message("source/target value 8 is obsolete")
+ *         .key("compiler:obsolete-source-target")
+ *         .source("maven-compiler-plugin:3.15.0:compile")
+ *         .suggestion("Update maven.compiler.source to 11 or higher")
+ *         .documentationUrl("https://maven.apache.org/plugins/maven-compiler-plugin/")
+ *         .build());
+ * }
+ * }
+ * + * @since 4.1.0 + * @see BuilderProblem#builder() + */ +@Experimental +@ThreadSafe +public interface DiagnosticReporter extends Service { + + /** + * Reports a structured build problem. + *

+ * If the problem has a non-null {@link BuilderProblem#getKey() key} + * and a problem with the same key has already been reported, the + * duplicate is counted but not stored again. + * + * @param problem the problem to report; must not be {@code null} + */ + void report(@Nonnull BuilderProblem problem); + + /** + * Convenience method to report a warning with all structured fields. + * + * @param message the warning message + * @param key deduplication key, or {@code null} + * @param source source hint (e.g. plugin GAV), or {@code null} + * @param suggestion actionable fix suggestion, or {@code null} + * @param documentationUrl URL to relevant documentation, or {@code null} + */ + default void warning( + @Nonnull String message, + @Nullable String key, + @Nullable String source, + @Nullable String suggestion, + @Nullable String documentationUrl) { + report(BuilderProblem.builder() + .severity(BuilderProblem.Severity.WARNING) + .message(message) + .key(key) + .source(source) + .suggestion(suggestion) + .documentationUrl(documentationUrl) + .build()); + } + + /** + * Convenience method to report a simple warning message. + * + * @param message the warning message + */ + default void warning(@Nonnull String message) { + warning(message, null, null, null, null); + } +} \ No newline at end of file diff --git a/compat/maven-embedder/pom.xml b/compat/maven-embedder/pom.xml index 2df8588ec116..56730ccb4326 100644 --- a/compat/maven-embedder/pom.xml +++ b/compat/maven-embedder/pom.xml @@ -163,11 +163,6 @@ under the License. commons-cli - - ch.qos.logback - logback-classic - true - org.jline jansi-core diff --git a/impl/maven-cli/pom.xml b/impl/maven-cli/pom.xml index 5d7304af5e3a..56ac62ed6f3e 100644 --- a/impl/maven-cli/pom.xml +++ b/impl/maven-cli/pom.xml @@ -195,19 +195,10 @@ under the License. org.slf4j slf4j-api - - org.slf4j - jul-to-slf4j - commons-cli commons-cli - - ch.qos.logback - logback-classic - true - org.junit.jupiter diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/event/ExecutionEventLogger.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/ExecutionEventLogger.java index bb8cefdfff34..81a5184d86c0 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/event/ExecutionEventLogger.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/ExecutionEventLogger.java @@ -25,13 +25,13 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; -import java.util.ArrayList; import java.util.List; import java.util.Objects; import org.apache.maven.api.MonotonicClock; import org.apache.maven.api.services.MessageBuilder; import org.apache.maven.api.services.MessageBuilderFactory; +import org.apache.maven.cling.utils.CLIReportingUtils; import org.apache.maven.execution.AbstractExecutionListener; import org.apache.maven.execution.BuildFailure; import org.apache.maven.execution.BuildSuccess; @@ -205,53 +205,44 @@ private void logReactorSummary(MavenSession session) { List projects = session.getProjects(); + StringBuilder buffer = new StringBuilder(128); + String skippedMessage = builder().warning("SKIPPED").build(); String successMessage = builder().success("SUCCESS").build(); String failureMessage = builder().failure("FAILURE").build(); String unknownMessage = builder().warning("UNKNOWN").build(); - List entries = new ArrayList<>(projects.size()); + boolean lastWasSkipped = false; for (MavenProject project : projects) { BuildSummary buildSummary = result.getBuildSummary(project); String statusMessage; - int group; - if (buildSummary instanceof BuildSuccess) { + boolean shouldSkip = result.hasExceptions(); + if (buildSummary == null) { + statusMessage = skippedMessage; + } else if (buildSummary instanceof BuildSuccess) { statusMessage = successMessage; - group = 1; } else if (buildSummary instanceof BuildFailure) { statusMessage = failureMessage; - group = 2; - } else if (buildSummary == null) { - statusMessage = skippedMessage; - group = 0; + shouldSkip = false; } else { statusMessage = unknownMessage; - group = 0; } - entries.add(new ReactorSummaryEntry(project, buildSummary, group, statusMessage)); - } - - ReactorSummaryRequest request = new ReactorSummaryRequest(entries, new StringBuilder(128), isSingleVersion); - logReactorSummaryGroup(request, 0); - logReactorSummaryGroup(request, 1); - logReactorSummaryGroup(request, 2); - } - - private void logReactorSummaryGroup(ReactorSummaryRequest request, int group) { - StringBuilder buffer = request.buffer(); - - for (ReactorSummaryEntry entry : request.entries()) { - if (entry.group() != group) { + if (shouldSkip) { + lastWasSkipped = true; continue; } + if (lastWasSkipped) { + logger.info("..."); + lastWasSkipped = false; + } - buffer.append(entry.project().getName()); + buffer.append(project.getName()); buffer.append(' '); - if (!request.isSingleVersion()) { - buffer.append(entry.project().getVersion()); + if (!isSingleVersion) { + buffer.append(project.getVersion()); buffer.append(' '); } @@ -262,25 +253,19 @@ private void logReactorSummaryGroup(ReactorSummaryRequest request, int group) { buffer.append(' '); } - buffer.append(entry.statusMessage()); - if (entry.buildSummary() != null) { - formatBuildTime(buffer, entry.buildSummary()); + buffer.append(statusMessage); + if (buildSummary != null) { + formatBuildTime(buffer, buildSummary); } - if (entry.buildSummary() instanceof BuildFailure) { - logger.error(buffer.toString()); - } else { - logger.info(buffer.toString()); - } + logger.info(buffer.toString()); buffer.setLength(0); } - } - - private record ReactorSummaryRequest( - List entries, StringBuilder buffer, boolean isSingleVersion) {} - private record ReactorSummaryEntry( - MavenProject project, BuildSummary buildSummary, int group, String statusMessage) {} + if (lastWasSkipped) { + logger.info("..."); + } + } private void formatBuildTime(StringBuilder buffer, BuildSummary buildSummary) { buffer.append(" ["); @@ -297,17 +282,12 @@ private void logResult(MavenSession session) { infoLine('-'); MessageBuilder buffer = builder(); - boolean failure = session.getResult().hasExceptions(); - if (failure) { + if (session.getResult().hasExceptions()) { buffer.failure("BUILD FAILURE"); } else { buffer.success("BUILD SUCCESS"); } - if (failure) { - logger.error(buffer.toString()); - } else { - logger.info(buffer.toString()); - } + logger.info(buffer.toString()); } private MessageBuilder builder() { @@ -325,6 +305,15 @@ private void logStats(MavenSession session) { logger.info("Total time: {}{}", formatDuration(time), wallClock); + // On failure, show Maven and Java version to help with bug reports (MNG-7372) + if (session.getResult().hasExceptions()) { + logger.info("Maven: {}", CLIReportingUtils.showVersionMinimal()); + logger.info( + "Java: {} ({})", + System.getProperty("java.version", ""), + System.getProperty("java.vendor", "")); + } + ZonedDateTime rounded = finish.truncatedTo(ChronoUnit.SECONDS).atZone(ZoneId.systemDefault()); logger.info("Finished at: {}", formatTimestamp(rounded)); } @@ -550,4 +539,4 @@ public void forkedProjectStarted(ExecutionEvent event) { infoLine('>'); } } -} +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/event/MachineBuildEventListener.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/MachineBuildEventListener.java new file mode 100644 index 000000000000..26f87f09e38b --- /dev/null +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/MachineBuildEventListener.java @@ -0,0 +1,273 @@ +/* + * 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.cling.event; + +import java.util.function.Consumer; + +import org.apache.maven.api.MonotonicClock; +import org.apache.maven.api.build.report.LogEvent; +import org.apache.maven.execution.ExecutionEvent; +import org.apache.maven.logging.BuildEventListener; +import org.eclipse.aether.transfer.TransferEvent; + +/** + * A machine-readable build event listener that outputs one JSON object per line + * to the configured writer. Each line is a self-contained JSON object with an + * {@code "event"} field identifying its type. + *

+ * This listener handles the {@link BuildEventListener} events: log messages, + * transfer progress, and execution failures. Session and project lifecycle events + * are emitted by the companion {@link MachineExecutionEventLogger}. + *

+ * The JSON lines format is designed for piping to external tools (CI systems, + * LLM agents, IDE integrations) that consume structured build events in real time. + *

+ * Example output: + *

+ * {"event":"log","timestamp":"...","message":"Compiling 42 source files"}
+ * {"event":"transfer.started","timestamp":"...","artifact":"core-4.1.0.jar","size":524288}
+ * {"event":"transfer.progressed","timestamp":"...","artifact":"core-4.1.0.jar","transferred":262144,"total":524288}
+ * {"event":"transfer.completed","timestamp":"...","artifact":"core-4.1.0.jar","transferred":524288}
+ * 
+ * + * Selected via {@code --console=machine}. + * + * @since 4.1.0 + * @see MachineExecutionEventLogger + */ +public class MachineBuildEventListener implements BuildEventListener { + + private final Consumer output; + + /** + * Creates a new MachineBuildEventListener. + * + * @param output the consumer that receives each JSON line (typically writes to terminal/stdout) + */ + public MachineBuildEventListener(Consumer output) { + this.output = output; + } + + /** + * Emit a pre-built JSON line to the output. Thread-safe — output is serialized + * to prevent interleaved lines from parallel builds. + * + * @param json the complete JSON object string (no trailing newline) + */ + public synchronized void emitEvent(String json) { + output.accept(json); + } + + @Override + public void sessionStarted(ExecutionEvent event) { + // Handled by MachineExecutionEventLogger.sessionStarted() + } + + @Override + public void projectStarted(String projectId) { + // Handled by MachineExecutionEventLogger.projectStarted() + } + + @Override + public void projectLogMessage(String projectId, LogEvent event) { + emitEvent(new JsonLine("log") + .field("level", event.level().name()) + .field("module", projectId) + .field("logger", event.loggerName()) + .field("message", event.message()) + .build()); + } + + @Override + public void projectFinished(String projectId) { + // Handled by MachineExecutionEventLogger.projectSucceeded/Failed/Skipped() + } + + @Override + public void executionFailure(String projectId, boolean halted, String exception) { + emitEvent(new JsonLine("execution.failure") + .field("module", projectId) + .field("halted", halted) + .field("error", exception) + .build()); + } + + @Override + public void mojoStarted(ExecutionEvent event) { + // Handled by MachineExecutionEventLogger.mojoStarted() + } + + @Override + public void finish(int exitCode) throws Exception { + // No-op — build.finished is emitted by MachineExecutionEventLogger.sessionEnded() + } + + @Override + public void fail(Throwable t) throws Exception { + // No-op — build.finished is emitted by MachineExecutionEventLogger.sessionEnded() + } + + @Override + public void log(String msg) { + emitEvent(new JsonLine("log").field("message", msg).build()); + } + + @Override + public void transfer(String projectId, TransferEvent event) { + String resource = event.getResource().getResourceName(); + String artifactName = extractArtifactName(resource); + long contentLength = event.getResource().getContentLength(); + + switch (event.getType()) { + case INITIATED: + case STARTED: + JsonLine started = new JsonLine("transfer.started").field("artifact", artifactName); + if (projectId != null) { + started.field("module", projectId); + } + if (contentLength > 0) { + started.field("size", contentLength); + } + started.field("url", resource); + emitEvent(started.build()); + break; + case PROGRESSED: + JsonLine progressed = new JsonLine("transfer.progressed").field("artifact", artifactName); + progressed.field("transferred", event.getTransferredBytes()); + if (contentLength > 0) { + progressed.field("total", contentLength); + } + emitEvent(progressed.build()); + break; + case SUCCEEDED: + JsonLine succeeded = new JsonLine("transfer.completed").field("artifact", artifactName); + succeeded.field("transferred", event.getTransferredBytes()); + emitEvent(succeeded.build()); + break; + case FAILED: + JsonLine failed = new JsonLine("transfer.failed").field("artifact", artifactName); + if (event.getException() != null) { + failed.field("error", event.getException().getMessage()); + } + emitEvent(failed.build()); + break; + default: + break; + } + } + + // ---- Helpers ---- + + private static String extractArtifactName(String resourceName) { + if (resourceName == null) { + return "unknown"; + } + int lastSlash = resourceName.lastIndexOf('/'); + return lastSlash >= 0 ? resourceName.substring(lastSlash + 1) : resourceName; + } + + // ---- JSON line builder ---- + + /** + * Lightweight builder for single-line JSON objects. Builds a flat JSON object + * with an {@code "event"} type and a {@code "timestamp"} field, plus any + * additional fields. Thread-safe when used within a single thread per instance. + */ + static class JsonLine { + private final StringBuilder sb; + + JsonLine(String eventType) { + sb = new StringBuilder(256); + sb.append("{\"event\":\""); + sb.append(eventType); + sb.append("\",\"timestamp\":\""); + sb.append(MonotonicClock.now().toString()); + sb.append('"'); + } + + JsonLine field(String key, String value) { + if (value != null) { + sb.append(",\"").append(key).append("\":"); + writeJsonString(sb, value); + } + return this; + } + + JsonLine field(String key, long value) { + sb.append(",\"").append(key).append("\":").append(value); + return this; + } + + JsonLine field(String key, double value) { + sb.append(",\"").append(key).append("\":").append(value); + return this; + } + + JsonLine field(String key, boolean value) { + sb.append(",\"").append(key).append("\":").append(value); + return this; + } + + String build() { + sb.append('}'); + return sb.toString(); + } + + /** + * Write a JSON-escaped string value (with surrounding quotes) to the builder. + */ + private static void writeJsonString(StringBuilder sb, String value) { + sb.append('"'); + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + switch (c) { + case '"': + sb.append("\\\""); + break; + case '\\': + sb.append("\\\\"); + break; + case '\n': + sb.append("\\n"); + break; + case '\r': + sb.append("\\r"); + break; + case '\t': + sb.append("\\t"); + break; + case '\b': + sb.append("\\b"); + break; + case '\f': + sb.append("\\f"); + break; + default: + if (c < 0x20) { + sb.append("\\u"); + sb.append(String.format("%04x", (int) c)); + } else { + sb.append(c); + } + } + } + sb.append('"'); + } + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/event/MachineExecutionEventLogger.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/MachineExecutionEventLogger.java new file mode 100644 index 000000000000..0b246e496404 --- /dev/null +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/MachineExecutionEventLogger.java @@ -0,0 +1,306 @@ +/* + * 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.cling.event; + +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +import org.apache.maven.api.MonotonicClock; +import org.apache.maven.cling.event.MachineBuildEventListener.JsonLine; +import org.apache.maven.execution.AbstractExecutionListener; +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.MavenSession; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.project.MavenProject; + +/** + * Execution event logger for machine-readable output ({@code --console=machine}). + *

+ * Emits one JSON line per lifecycle event to the shared + * {@link MachineBuildEventListener#emitEvent(String)} writer. This logger + * handles the {@link org.apache.maven.execution.ExecutionListener} events: + * session start/end, project start/success/failure/skip, and mojo + * start/success/failure/skip. + *

+ * Together with {@link MachineBuildEventListener} (which handles log messages, + * transfers, and execution failures), this provides a complete, typed event + * stream suitable for piping to external tools, CI systems, and LLM agents. + *

+ * Example output: + *

+ * {"event":"build.started","timestamp":"...","projectCount":12,"goals":"clean install"}
+ * {"event":"module.started","timestamp":"...","module":"maven-core","groupId":"org.apache.maven","artifactId":"maven-core","version":"4.1.0-SNAPSHOT","index":1,"total":12}
+ * {"event":"mojo.started","timestamp":"...","module":"maven-core","plugin":"maven-compiler-plugin","goal":"compile","phase":"compile"}
+ * {"event":"mojo.succeeded","timestamp":"...","module":"maven-core","plugin":"maven-compiler-plugin","goal":"compile","duration":1.2}
+ * {"event":"module.succeeded","timestamp":"...","module":"maven-core","duration":2.1}
+ * {"event":"build.finished","timestamp":"...","status":"SUCCESS","duration":32.1,"total":12,"passed":12,"failed":0,"skipped":0}
+ * 
+ * + * Selected via {@code --console=machine}. + * + * @since 4.1.0 + * @see MachineBuildEventListener + */ +public class MachineExecutionEventLogger extends AbstractExecutionListener { + + private final MachineBuildEventListener machineBel; + + // Track mojo start times for duration calculation + private final Map mojoStartTimes = new ConcurrentHashMap<>(); + + // Reactor state + private volatile int totalProjects; + private volatile int currentVisitedProjectCount; + private volatile Instant buildStartTime; + + public MachineExecutionEventLogger(MachineBuildEventListener machineBel) { + this.machineBel = Objects.requireNonNull(machineBel, "machineBel cannot be null"); + } + + // ---- Session lifecycle ---- + + @Override + public void sessionStarted(ExecutionEvent event) { + MavenSession session = event.getSession(); + List projects = session.getProjects(); + List allProjects = session.getAllProjects(); + + totalProjects = allProjects.size(); + currentVisitedProjectCount = allProjects.size() - projects.size(); + buildStartTime = MonotonicClock.now(); + + String goals = session.getRequest().getGoals().stream().collect(Collectors.joining(" ")); + + JsonLine line = new JsonLine("build.started") + .field("projectCount", totalProjects) + .field("goals", goals); + + List profiles = session.getRequest().getActiveProfiles(); + if (profiles != null && !profiles.isEmpty()) { + line.field("profiles", String.join(",", profiles)); + } + + machineBel.emitEvent(line.build()); + } + + @Override + public void sessionEnded(ExecutionEvent event) { + MavenSession session = event.getSession(); + + int passed = 0; + int failed = 0; + int skipped = 0; + for (MavenProject project : session.getProjects()) { + BuildSummary summary = session.getResult().getBuildSummary(project); + if (summary instanceof BuildSuccess) { + passed++; + } else if (summary instanceof BuildFailure) { + failed++; + } else { + skipped++; + } + } + + String status = session.getResult().hasExceptions() ? "FAILURE" : "SUCCESS"; + double duration = 0; + if (buildStartTime != null) { + duration = Duration.between(buildStartTime, MonotonicClock.now()).toMillis() / 1000.0; + } + + machineBel.emitEvent(new JsonLine("build.finished") + .field("status", status) + .field("duration", duration) + .field("total", totalProjects) + .field("passed", passed) + .field("failed", failed) + .field("skipped", skipped) + .build()); + } + + // ---- Module lifecycle ---- + + @Override + public void projectStarted(ExecutionEvent event) { + MavenProject project = event.getProject(); + int index; + synchronized (this) { + index = ++currentVisitedProjectCount; + } + + machineBel.emitEvent(new JsonLine("module.started") + .field("module", project.getName()) + .field("groupId", project.getGroupId()) + .field("artifactId", project.getArtifactId()) + .field("version", project.getVersion()) + .field("index", index) + .field("total", totalProjects) + .build()); + } + + @Override + public void projectSucceeded(ExecutionEvent event) { + logModuleFinished(event, "module.succeeded"); + } + + @Override + public void projectFailed(ExecutionEvent event) { + logModuleFinished(event, "module.failed"); + } + + @Override + public void projectSkipped(ExecutionEvent event) { + MavenProject project = event.getProject(); + machineBel.emitEvent(new JsonLine("module.skipped") + .field("module", project.getName()) + .build()); + } + + // ---- Mojo lifecycle ---- + + @Override + public void mojoStarted(ExecutionEvent event) { + MavenProject project = event.getProject(); + MojoExecution mojo = event.getMojoExecution(); + + String mojoKey = project.getArtifactId() + ":" + mojo.getExecutionId() + ":" + mojo.getGoal(); + mojoStartTimes.put(mojoKey, MonotonicClock.now()); + + machineBel.emitEvent(new JsonLine("mojo.started") + .field("module", project.getName()) + .field("plugin", mojo.getArtifactId()) + .field("goal", mojo.getGoal()) + .field("phase", mojo.getLifecyclePhase()) + .field("executionId", mojo.getExecutionId()) + .build()); + } + + @Override + public void mojoSucceeded(ExecutionEvent event) { + logMojoFinished(event, "mojo.succeeded"); + } + + @Override + public void mojoFailed(ExecutionEvent event) { + MavenProject project = event.getProject(); + MojoExecution mojo = event.getMojoExecution(); + + String mojoKey = project.getArtifactId() + ":" + mojo.getExecutionId() + ":" + mojo.getGoal(); + Instant start = mojoStartTimes.remove(mojoKey); + + JsonLine line = new JsonLine("mojo.failed") + .field("module", project.getName()) + .field("plugin", mojo.getArtifactId()) + .field("goal", mojo.getGoal()); + if (start != null) { + double duration = Duration.between(start, MonotonicClock.now()).toMillis() / 1000.0; + line.field("duration", duration); + } + if (event.getException() != null) { + line.field("error", event.getException().getMessage()); + } + machineBel.emitEvent(line.build()); + } + + @Override + public void mojoSkipped(ExecutionEvent event) { + MavenProject project = event.getProject(); + MojoExecution mojo = event.getMojoExecution(); + + machineBel.emitEvent(new JsonLine("mojo.skipped") + .field("module", project.getName()) + .field("plugin", mojo.getArtifactId()) + .field("goal", mojo.getGoal()) + .build()); + } + + // ---- Fork lifecycle (machine mode emits these for completeness) ---- + + @Override + public void forkStarted(ExecutionEvent event) { + MavenProject project = event.getProject(); + MojoExecution mojo = event.getMojoExecution(); + + machineBel.emitEvent(new JsonLine("fork.started") + .field("module", project.getName()) + .field("plugin", mojo.getArtifactId()) + .field("goal", mojo.getGoal()) + .build()); + } + + @Override + public void forkSucceeded(ExecutionEvent event) { + MavenProject project = event.getProject(); + machineBel.emitEvent(new JsonLine("fork.succeeded") + .field("module", project.getName()) + .build()); + } + + @Override + public void forkFailed(ExecutionEvent event) { + MavenProject project = event.getProject(); + JsonLine line = new JsonLine("fork.failed").field("module", project.getName()); + if (event.getException() != null) { + line.field("error", event.getException().getMessage()); + } + machineBel.emitEvent(line.build()); + } + + // ---- Helpers ---- + + private void logModuleFinished(ExecutionEvent event, String eventType) { + MavenProject project = event.getProject(); + MavenSession session = event.getSession(); + BuildSummary summary = session.getResult().getBuildSummary(project); + + JsonLine line = new JsonLine(eventType).field("module", project.getName()); + if (summary != null) { + line.field("duration", summary.getExecTime().toMillis() / 1000.0); + } + if ("module.failed".equals(eventType) && event.getException() != null) { + line.field("error", event.getException().getMessage()); + } + machineBel.emitEvent(line.build()); + } + + private void logMojoFinished(ExecutionEvent event, String eventType) { + MavenProject project = event.getProject(); + MojoExecution mojo = event.getMojoExecution(); + + String mojoKey = project.getArtifactId() + ":" + mojo.getExecutionId() + ":" + mojo.getGoal(); + Instant start = mojoStartTimes.remove(mojoKey); + + JsonLine line = new JsonLine(eventType) + .field("module", project.getName()) + .field("plugin", mojo.getArtifactId()) + .field("goal", mojo.getGoal()); + if (start != null) { + double duration = Duration.between(start, MonotonicClock.now()).toMillis() / 1000.0; + line.field("duration", duration); + } + machineBel.emitEvent(line.build()); + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/event/PlainExecutionEventLogger.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/PlainExecutionEventLogger.java new file mode 100644 index 000000000000..cd1cfec3f3c7 --- /dev/null +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/PlainExecutionEventLogger.java @@ -0,0 +1,323 @@ +/* + * 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.cling.event; + +import java.time.Duration; +import java.util.List; +import java.util.Objects; + +import org.apache.maven.api.MonotonicClock; +import org.apache.maven.api.services.MessageBuilder; +import org.apache.maven.api.services.MessageBuilderFactory; +import org.apache.maven.cling.utils.CLIReportingUtils; +import org.apache.maven.execution.AbstractExecutionListener; +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.project.MavenProject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.maven.cling.utils.CLIReportingUtils.formatDuration; + +/** + * Compact execution event logger for CI and batch environments. + *

+ * Produces one line per completed module instead of the verbose per-mojo + * output of {@link ExecutionEventLogger}. Designed for CI log viewers + * and LLM-based tools where signal density matters more than verbosity. + *

+ * Example output: + *

+ * [INFO] maven-api-core ................................ SUCCESS [  2.1s]
+ * [INFO] maven-core ..................................... FAILURE [  5.3s]
+ * [INFO]
+ * [INFO] BUILD FAILURE
+ * [INFO] Total time:  32.1s
+ * 
+ * + * Selected via {@code --console=plain} or automatically in CI environments. + * + * @since 4.1.0 + * @see ExecutionEventLogger + */ +public class PlainExecutionEventLogger extends AbstractExecutionListener { + + private static final int MAX_LOG_PREFIX_SIZE = 8; // "[ERROR] " + private static final int PROJECT_STATUS_SUFFIX_SIZE = 20; // "SUCCESS [ 0.000 s]" + private static final int MIN_TERMINAL_WIDTH = 60; + private static final int DEFAULT_TERMINAL_WIDTH = 80; + private static final int MAX_TERMINAL_WIDTH = 130; + private static final int MAX_PADDED_BUILD_TIME_DURATION_LENGTH = 9; + + private final MessageBuilderFactory messageBuilderFactory; + private final Logger logger; + private int terminalWidth; + private int lineLength; + private int maxProjectNameLength; + private int totalProjects; + private volatile int currentVisitedProjectCount; + + public PlainExecutionEventLogger(MessageBuilderFactory messageBuilderFactory) { + this(messageBuilderFactory, LoggerFactory.getLogger(PlainExecutionEventLogger.class)); + } + + public PlainExecutionEventLogger(MessageBuilderFactory messageBuilderFactory, Logger logger) { + this(messageBuilderFactory, logger, -1); + } + + public PlainExecutionEventLogger(MessageBuilderFactory messageBuilderFactory, Logger logger, int terminalWidth) { + this.logger = Objects.requireNonNull(logger, "logger cannot be null"); + this.messageBuilderFactory = messageBuilderFactory; + this.terminalWidth = terminalWidth; + } + + private void init() { + if (maxProjectNameLength == 0) { + if (terminalWidth < 0) { + terminalWidth = messageBuilderFactory.getTerminalWidth(); + } + terminalWidth = Math.min( + MAX_TERMINAL_WIDTH, + Math.max(terminalWidth <= 0 ? DEFAULT_TERMINAL_WIDTH : terminalWidth, MIN_TERMINAL_WIDTH)); + lineLength = terminalWidth - MAX_LOG_PREFIX_SIZE; + maxProjectNameLength = lineLength - PROJECT_STATUS_SUFFIX_SIZE; + } + } + + private MessageBuilder builder() { + return messageBuilderFactory.builder(); + } + + private static String chars(char c, int count) { + return String.valueOf(c).repeat(Math.max(0, count)); + } + + private void infoMain(String msg) { + logger.info(builder().strong(msg).toString()); + } + + // ---- Session lifecycle ---- + + @Override + public void projectDiscoveryStarted(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + logger.info("Scanning for projects..."); + } + } + + @Override + public void sessionStarted(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + List projects = event.getSession().getProjects(); + List allProjects = event.getSession().getAllProjects(); + + currentVisitedProjectCount = allProjects.size() - projects.size(); + totalProjects = allProjects.size(); + } + } + + @Override + public void sessionEnded(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + logger.info(""); + logResult(event.getSession()); + logStats(event.getSession()); + } + } + + // ---- Module lifecycle: one line per completed module ---- + + @Override + public void projectStarted(ExecutionEvent event) { + // In plain mode, we only log when a project finishes (succeeded/failed/skipped) + } + + @Override + public void projectSucceeded(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + logProjectLine(event, "SUCCESS"); + } + } + + @Override + public void projectFailed(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + logProjectLine(event, "FAILURE"); + } + } + + @Override + public void projectSkipped(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + logProjectLine(event, "SKIPPED"); + } + } + + // ---- Mojo lifecycle: suppressed in plain mode ---- + + @Override + public void mojoStarted(ExecutionEvent event) { + // Suppressed in plain mode — plugin execution details go to build report + } + + @Override + public void mojoSkipped(ExecutionEvent event) { + if (logger.isWarnEnabled()) { + logger.warn( + "Goal '{}' requires online mode for execution but Maven is currently offline, skipping", + event.getMojoExecution().getGoal()); + } + } + + @Override + public void forkStarted(ExecutionEvent event) { + // Suppressed in plain mode + } + + @Override + public void forkSucceeded(ExecutionEvent event) { + // Suppressed in plain mode + } + + // ---- Formatting helpers ---- + + private void logProjectLine(ExecutionEvent event, String status) { + MavenProject project = event.getProject(); + MavenSession session = event.getSession(); + MavenExecutionResult result = session.getResult(); + BuildSummary buildSummary = result.getBuildSummary(project); + + StringBuilder buffer = new StringBuilder(128); + buffer.append(project.getName()); + buffer.append(' '); + + if (totalProjects > 1) { + int number; + synchronized (this) { + number = ++currentVisitedProjectCount; + } + String progress = "[" + number + "/" + totalProjects + "]"; + buffer.append(progress); + buffer.append(' '); + } + + // Pad with dots to align status + if (buffer.length() <= maxProjectNameLength) { + while (buffer.length() < maxProjectNameLength) { + buffer.append('.'); + } + buffer.append(' '); + } + + // Status with color + MessageBuilder mb = builder(); + mb.a(buffer); + switch (status) { + case "SUCCESS": + mb.success(status); + break; + case "FAILURE": + mb.failure(status); + break; + default: + mb.warning(status); + break; + } + + // Duration + if (buildSummary != null) { + mb.a(" ["); + String duration = formatDuration(buildSummary.getExecTime()); + int padSize = MAX_PADDED_BUILD_TIME_DURATION_LENGTH - duration.length(); + if (padSize > 0) { + mb.a(chars(' ', padSize)); + } + mb.a(duration); + mb.a(']'); + } + + logger.info(mb.toString()); + } + + private void logResult(MavenSession session) { + MessageBuilder buffer = builder(); + if (session.getResult().hasExceptions()) { + buffer.failure("BUILD FAILURE"); + } else { + buffer.success("BUILD SUCCESS"); + } + + int passed = 0; + int failed = 0; + int skipped = 0; + for (MavenProject project : session.getProjects()) { + BuildSummary summary = session.getResult().getBuildSummary(project); + if (summary instanceof BuildSuccess) { + passed++; + } else if (summary instanceof BuildFailure) { + failed++; + } else { + skipped++; + } + } + + logger.info(buffer.toString()); + + // Compact stats line: "12 modules | 11 passed | 1 failed | 0 skipped" + if (totalProjects > 1) { + StringBuilder stats = new StringBuilder(); + stats.append(totalProjects).append(" modules"); + stats.append(" | ").append(passed).append(" passed"); + if (failed > 0) { + stats.append(" | ").append(failed).append(" failed"); + } + if (skipped > 0) { + stats.append(" | ").append(skipped).append(" skipped"); + } + logger.info(stats.toString()); + } + } + + private void logStats(MavenSession session) { + Duration time = Duration.between(session.getRequest().getStartInstant(), MonotonicClock.now()); + String wallClock = session.getRequest().getDegreeOfConcurrency() > 1 ? " (Wall Clock)" : ""; + logger.info("Total time: {}{}", formatDuration(time), wallClock); + + // On failure, show Maven and Java version to help with bug reports (MNG-7372) + if (session.getResult().hasExceptions()) { + logger.info("Maven: {}", CLIReportingUtils.showVersionMinimal()); + logger.info( + "Java: {} ({})", + System.getProperty("java.version", ""), + System.getProperty("java.vendor", "")); + } + + logger.info("Full report: target/build-reports/build-report-latest.json"); + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/event/RichBuildEventListener.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/RichBuildEventListener.java new file mode 100644 index 000000000000..56931fc0f638 --- /dev/null +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/RichBuildEventListener.java @@ -0,0 +1,785 @@ +/* + * 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.cling.event; + +import java.io.PrintWriter; +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.maven.api.MonotonicClock; +import org.apache.maven.api.build.report.LogEvent; +import org.apache.maven.api.build.report.LogLevel; +import org.apache.maven.execution.ExecutionEvent; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.logging.BuildEventListener; +import org.apache.maven.project.MavenProject; +import org.eclipse.aether.transfer.TransferEvent; +import org.jline.terminal.Terminal; +import org.jline.utils.Display; + +/** + * A rich terminal build event listener using JLine's {@link Display} in + * non-fullscreen mode — the same approach as mvnd. + *

+ * The status area is rendered at the current cursor position using + * {@link Display#updateAnsi}. When log output arrives, the display is + * cleared (updated with empty lines), the log line is printed normally, + * and then the status is redrawn below it. JLine handles all the cursor + * math (moving up, erasing changed lines, etc.) and only repaints what + * actually changed. + *

+ * At the end of the build, the display is cleared and nothing remains + * on screen — the summary then prints as normal scrolling text. + *

+ * The status area has a fixed height based on the degree of concurrency, + * so the separator and summary line stay anchored at the bottom. Active + * projects are packed to the top of the slot area; empty lines fill the + * gap between the last active project and the separator. + *

+ * Falls back to simple log passthrough on dumb terminals. + * + * @since 4.1.0 + * @see PlainExecutionEventLogger + * @see ExecutionEventLogger + */ +public class RichBuildEventListener implements BuildEventListener { + + // ---- ANSI colors ---- + + private static final String ESC = "\033["; + private static final String CYAN = ESC + "36m"; + private static final String YELLOW = ESC + "33m"; + private static final String BLUE = ESC + "34m"; + private static final String GREEN = ESC + "32m"; + private static final String RED = ESC + "31m"; + private static final String BOLD = ESC + "1m"; + private static final String DIM = ESC + "2m"; + private static final String RESET = ESC + "0m"; + + // ---- Terminal & output ---- + + private final Terminal terminal; + private final PrintWriter writer; + private final boolean supported; + + // ---- JLine Display ---- + + /** JLine display in non-fullscreen mode — handles cursor math. */ + private volatile Display display; + /** Whether the display is currently active. */ + private volatile boolean displayActive; + /** Fixed number of lines in the status area (set once in initReactor). */ + private volatile int statusHeight; + + // ---- Reactor state ---- + + private volatile int totalProjects; + private volatile int completedProjects; + private volatile Instant buildStartTime; + /** One-line header shown at the top of the status area. */ + private volatile String headerLine; + + // ---- Project display ---- + + private final Map activeProjects = new ConcurrentHashMap<>(); + private final List projectOrder = new ArrayList<>(); + private final Map projectNames = new ConcurrentHashMap<>(); + + // ---- Active downloads ---- + + private final Map activeTransfers = new ConcurrentHashMap<>(); + + // ---- Synchronization ---- + + /** Guards all terminal output and slot mutations. */ + private final Object outputLock = new Object(); + + // ---- Periodic refresh ---- + + /** Scheduler for 1-second display refresh so elapsed timers stay live. */ + private volatile ScheduledExecutorService refreshScheduler; + /** Handle for the periodic refresh task. */ + private volatile ScheduledFuture refreshFuture; + + // ---- Warning tracking ---- + + /** Number of WARN-level messages seen during the build. */ + private final AtomicInteger warningCount = new AtomicInteger(); + + /** Number of ERROR-level messages seen during the build. */ + private final AtomicInteger errorCount = new AtomicInteger(); + + // ---- Constructor ---- + + /** + * Creates a new RichBuildEventListener. + * + * @param terminal the JLine terminal for output + * @param output fallback output consumer (unused — kept for API compat) + */ + public RichBuildEventListener(Terminal terminal, java.util.function.Consumer output) { + this.terminal = terminal; + this.writer = terminal.writer(); + // Support ANSI if terminal type is not "dumb" and has reasonable size + String type = terminal.getType(); + this.supported = type != null && !Terminal.TYPE_DUMB.equals(type) && terminal.getWidth() > 0; + } + + // ---- Reactor lifecycle ---- + + /** + * Initialize reactor state from the session. Called by {@link RichExecutionEventLogger} + * during {@code sessionStarted}. + */ + public void initReactor(MavenSession session) { + List allProjects = session.getAllProjects(); + List projects = session.getProjects(); + + this.totalProjects = allProjects.size(); + this.completedProjects = allProjects.size() - projects.size(); + this.buildStartTime = MonotonicClock.now(); + + for (MavenProject project : allProjects) { + projectOrder.add(project.getArtifactId()); + projectNames.put(project.getArtifactId(), project.getName()); + } + + // Build header line + this.headerLine = buildHeaderLine(session); + + // Slot count = degree of concurrency (capped for sanity) + int concurrency = 1; + try { + concurrency = Math.max(1, session.getRequest().getDegreeOfConcurrency()); + } catch (Exception e) { + // fallback to 1 + } + int slotCount = Math.min(concurrency, 8); + // Fixed height: 1 header + N project slots + 1 separator + 1 summary + this.statusHeight = slotCount + 3; + + if (supported) { + setupDisplay(); + } + } + + private String buildHeaderLine(MavenSession session) { + StringBuilder h = new StringBuilder(); + h.append(' ').append(BOLD); + + // Maven version + String mavenVersion = null; + if (session.getSystemProperties() != null) { + mavenVersion = session.getSystemProperties().getProperty("maven.version"); + } + if (mavenVersion != null) { + h.append("Maven ").append(mavenVersion); + } else { + h.append("Maven"); + } + h.append(RESET); + + // Project name + MavenProject top = session.getTopLevelProject(); + if (top != null) { + h.append(DIM).append(" ─ ").append(RESET); + h.append("building "); + h.append(CYAN).append(top.getName()).append(RESET); + if (top.getVersion() != null) { + h.append(' ').append(DIM).append(top.getVersion()).append(RESET); + } + } + + // Goals + List goals = session.getGoals(); + if (goals != null && !goals.isEmpty()) { + h.append(DIM).append(" ─ ").append(RESET); + h.append(YELLOW).append(String.join(" ", goals)).append(RESET); + } + + return h.toString(); + } + + /** + * Set up the JLine Display in non-fullscreen mode. + */ + private void setupDisplay() { + synchronized (outputLock) { + display = new Display(terminal, false); + display.resize(statusHeight, terminal.getWidth()); + displayActive = true; + display.updateAnsi(buildStatusLines(), 0); + } + + // Start a 1-second periodic refresh so that elapsed-time counters + // stay live even when no build events are arriving (e.g. during + // a slow mojo execution with no log output). + ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, r -> { + Thread t = new Thread(r, "maven-rich-display-refresh"); + t.setDaemon(true); + return t; + }); + executor.setRemoveOnCancelPolicy(true); + refreshScheduler = executor; + refreshFuture = refreshScheduler.scheduleAtFixedRate(this::redraw, 1, 1, TimeUnit.SECONDS); + } + + /** + * Tear down the status display. Called by {@link RichExecutionEventLogger} + * during {@code sessionEnded} before printing the summary. + *

+ * The flush at the end is critical: {@link Display} writes through + * {@link Terminal#writer()} (a {@code PrintWriter} that does not auto-flush), + * while subsequent log output from SLF4J goes through {@code System.out} + * (which does auto-flush on {@code println}). Without the flush, + * the clear sequences sit in the writer's buffer while the summary text + * reaches the terminal first via {@code System.out} — then the belated + * clear erases the summary the user was supposed to see. + */ + public void tearDown() { + // Stop the periodic refresh first (outside outputLock to avoid deadlock) + if (refreshFuture != null) { + refreshFuture.cancel(false); + refreshFuture = null; + } + if (refreshScheduler != null) { + refreshScheduler.shutdownNow(); + refreshScheduler = null; + } + + synchronized (outputLock) { + if (!displayActive) { + return; + } + displayActive = false; + + // Clear the display area: update with empty lines, cursor at top + display.updateAnsi(Collections.nCopies(statusHeight, ""), 0); + // Erase from cursor to end of screen — removes any leftover artifacts + writer.print("\033[J"); + // Flush immediately so the clear reaches the terminal BEFORE + // any subsequent log output that goes through System.out + writer.flush(); + } + } + + // ---- BuildEventListener interface ---- + + @Override + public void sessionStarted(ExecutionEvent event) { + // Reactor init is handled via initReactor() called from RichExecutionEventLogger + } + + @Override + public void projectStarted(String projectId) { + activeProjects.put(projectId, new ProjectState(projectId, MonotonicClock.now())); + redraw(); + } + + @Override + public void projectFinished(String projectId) { + activeProjects.remove(projectId); + completedProjects++; + redraw(); + } + + @Override + public void projectLogMessage(String projectId, LogEvent event) { + // In rich mode, suppress INFO/DEBUG/TRACE/WARN — the status bar provides + // live progress and warnings are summarized at the end of the build. + // Only ERROR passes through to the terminal immediately. + if (event.level() == LogLevel.WARN) { + warningCount.incrementAndGet(); + return; + } + if (event.level() == LogLevel.INFO || event.level() == LogLevel.DEBUG || event.level() == LogLevel.TRACE) { + return; + } + if (event.level() == LogLevel.ERROR) { + errorCount.incrementAndGet(); + } + String output = event.formattedMessage(); + if (output == null) { + output = event.message(); + } + printAboveStatus(output); + } + + /** + * Returns the number of WARN-level log messages seen during the build. + */ + public int getWarningCount() { + return warningCount.get(); + } + + /** + * Returns the number of ERROR-level log messages seen during the build. + */ + public int getErrorCount() { + return errorCount.get(); + } + + @Override + public void log(String msg) { + printAboveStatus(msg); + } + + @Override + public void mojoStarted(ExecutionEvent event) { + String projectId = event.getProject().getArtifactId(); + ProjectState state = activeProjects.get(projectId); + if (state != null) { + state.currentMojo = event.getMojoExecution().getArtifactId() + ":" + + event.getMojoExecution().getGoal(); + } + synchronized (outputLock) { + if (displayActive) { + display.updateAnsi(buildStatusLines(), 0); + } + } + } + + @Override + public void executionFailure(String projectId, boolean halted, String exception) { + ProjectState state = activeProjects.get(projectId); + if (state != null) { + state.failed = true; + } + synchronized (outputLock) { + if (displayActive) { + display.updateAnsi(buildStatusLines(), 0); + } + } + } + + @Override + public void transfer(String projectId, TransferEvent event) { + String resource = event.getResource().getResourceName(); + + switch (event.getType()) { + case INITIATED: + case STARTED: + String artifactName = extractArtifactName(resource); + activeTransfers.put( + resource, + new TransferInfo(artifactName, 0, event.getResource().getContentLength())); + redraw(); + break; + case PROGRESSED: + TransferInfo info = activeTransfers.get(resource); + if (info != null) { + info.transferred = event.getTransferredBytes(); + // Only update every ~50KB to avoid too-frequent redraws + if (info.transferred - info.lastUpdateBytes > 51200) { + info.lastUpdateBytes = info.transferred; + redraw(); + } + } + break; + case SUCCEEDED: + case FAILED: + activeTransfers.remove(resource); + redraw(); + break; + default: + break; + } + } + + @Override + public void finish(int exitCode) throws Exception { + tearDown(); + } + + @Override + public void fail(Throwable t) throws Exception { + tearDown(); + } + + // ---- Display helpers ---- + + /** + * Print a message above the status area: clear the display, print + * the message as normal scrolling text, then redraw the status below. + */ + private void printAboveStatus(String msg) { + synchronized (outputLock) { + if (displayActive) { + // Clear status area so the message prints where it was + display.updateAnsi(Collections.nCopies(statusHeight, ""), 0); + display.reset(); + // Print the log message (scrolls normally) + writer.println(msg); + writer.flush(); + // Redraw status below the new output + display.updateAnsi(buildStatusLines(), 0); + } else { + writer.println(msg); + writer.flush(); + } + } + } + + private void redraw() { + synchronized (outputLock) { + if (displayActive) { + display.updateAnsi(buildStatusLines(), 0); + } + } + } + + // ---- Status line building ---- + + /** + * Build exactly {@link #statusHeight} status lines. + *

+ * For small reactors (modules fit as individual indicators): + * Layout: header + active slots + padding + separator + summary (indicators + counter). + *

+ * For large reactors (progress bar mode): + * Layout: header + active slots + padding + progress bar (full-width, acts as separator + counter). + * The progress bar replaces the separator — no redundant horizontal rule. + */ + private List buildStatusLines() { + int termWidth = Math.max(terminal.getWidth(), 40); + + // Collect active projects sorted by start time for visual stability + List active = new ArrayList<>(activeProjects.values()); + active.sort((a, b) -> a.startTime.compareTo(b.startTime)); + + // Determine if we're in progress bar mode (reactor too large for per-module indicators) + int maxIndicators = Math.min(projectOrder.size(), (termWidth - 40) / 3); + boolean useProgressBar = totalProjects > 1 && maxIndicators > 0 && totalProjects > maxIndicators; + + // Number of project slot lines: subtract header (1) + bottom lines (2 for separator+summary, 1 for bar) + int slotCount = statusHeight - (useProgressBar ? 2 : 3); + + List lines = new ArrayList<>(statusHeight); + + // Header line + lines.add(headerLine != null ? headerLine : ""); + + // Active projects packed to the top (up to slotCount) + int projectsShown = 0; + for (ProjectState state : active) { + if (projectsShown >= slotCount) { + break; + } + lines.add(formatProjectSlot(state)); + projectsShown++; + } + + // Empty padding lines at the bottom of the slot area + for (int i = projectsShown; i < slotCount; i++) { + lines.add(""); + } + + if (useProgressBar) { + // Progress bar replaces separator + summary as a single full-width line + lines.add(buildProgressBarLine(termWidth)); + } else { + // Separator line (always at the same position) + lines.add(DIM + "─".repeat(Math.min(termWidth, 120)) + RESET); + // Summary line (per-module indicators + counter + elapsed + downloads) + lines.add(buildSummaryLine(termWidth)); + } + + return lines; + } + + private String formatProjectSlot(ProjectState state) { + StringBuilder b = new StringBuilder(); + if (state.failed) { + b.append(RED).append(" ✗ ").append(RESET); + } else { + b.append(CYAN).append(" ● ").append(RESET); + } + b.append(BOLD); + b.append(projectNames.getOrDefault(state.projectId, state.projectId)); + b.append(RESET); + if (state.currentMojo != null) { + b.append(" ").append(YELLOW).append(state.currentMojo).append(RESET); + } + Duration elapsed = Duration.between(state.startTime, MonotonicClock.now()); + b.append(" ").append(DIM).append(formatCompactDuration(elapsed)).append(RESET); + return b.toString(); + } + + /** + * Build a full-width progress bar line for large reactors. + * Replaces both the separator and summary — one line with the proportional + * bar, counter, elapsed time, and download status. + */ + private String buildProgressBarLine(int termWidth) { + // Build the suffix first so we know how much width the bar can use + StringBuilder suffix = new StringBuilder(); + suffix.append(" ["); + suffix.append(completedProjects).append('/').append(totalProjects); + suffix.append(']'); + if (buildStartTime != null) { + Duration elapsed = Duration.between(buildStartTime, MonotonicClock.now()); + suffix.append(" ").append(formatCompactDuration(elapsed)); + } + if (!activeTransfers.isEmpty()) { + suffix.append(" ↓ "); + if (activeTransfers.size() == 1) { + TransferInfo ti = activeTransfers.values().iterator().next(); + suffix.append(ti.artifactName); + if (ti.totalBytes > 0) { + suffix.append(' ') + .append(formatBytes(ti.transferred)) + .append('/') + .append(formatBytes(ti.totalBytes)); + } + } else { + suffix.append(activeTransfers.size()).append(" artifacts"); + } + } + int suffixLen = suffix.length(); + + // Bar fills from column 0 to (lineWidth - suffixLen) + int lineWidth = Math.min(termWidth, 120); + int barWidth = Math.max(10, lineWidth - suffixLen); + + int activeCount = activeProjects.size(); + int doneChars = (int) ((long) completedProjects * barWidth / totalProjects); + int activeChars = (int) ((long) activeCount * barWidth / totalProjects); + if (activeCount > 0 && activeChars < 1) { + activeChars = 1; + } + if (doneChars + activeChars > barWidth) { + activeChars = barWidth - doneChars; + } + int remainChars = barWidth - doneChars - activeChars; + + StringBuilder s = new StringBuilder(); + s.append(GREEN).append("━".repeat(doneChars)).append(RESET); + s.append(YELLOW).append("━".repeat(activeChars)).append(RESET); + s.append(DIM).append("─".repeat(remainChars)).append(RESET); + + // Append suffix with styling + s.append(" ["); + s.append(BOLD) + .append(completedProjects) + .append('/') + .append(totalProjects) + .append(RESET); + s.append(']'); + if (buildStartTime != null) { + Duration elapsed = Duration.between(buildStartTime, MonotonicClock.now()); + s.append(" ").append(DIM).append(formatCompactDuration(elapsed)).append(RESET); + } + if (!activeTransfers.isEmpty()) { + s.append(" ").append(BLUE).append("↓ ").append(RESET); + if (activeTransfers.size() == 1) { + TransferInfo ti = activeTransfers.values().iterator().next(); + s.append(ti.artifactName); + if (ti.totalBytes > 0) { + s.append(' ') + .append(DIM) + .append(formatBytes(ti.transferred)) + .append('/') + .append(formatBytes(ti.totalBytes)) + .append(RESET); + } + } else { + s.append(activeTransfers.size()).append(" artifacts"); + } + } + + return s.toString(); + } + + /** + * Build the summary line for small reactors (per-module indicators + counter). + */ + private String buildSummaryLine(int termWidth) { + StringBuilder s = new StringBuilder(" "); + + // Per-module indicators — each module gets its own symbol + if (totalProjects > 1) { + for (String pid : projectOrder) { + if (activeProjects.containsKey(pid)) { + ProjectState ps = activeProjects.get(pid); + if (ps != null && ps.failed) { + s.append(RED).append("✗ ").append(RESET); + } else { + s.append(YELLOW).append("● ").append(RESET); + } + } else if (projectOrder.indexOf(pid) < completedProjects) { + s.append(GREEN).append("✓ ").append(RESET); + } else { + s.append(DIM).append("○ ").append(RESET); + } + } + } + + // Progress counter + s.append('['); + s.append(BOLD) + .append(completedProjects) + .append('/') + .append(totalProjects) + .append(RESET); + s.append(']'); + + // Elapsed time + if (buildStartTime != null) { + Duration elapsed = Duration.between(buildStartTime, MonotonicClock.now()); + s.append(" ").append(DIM).append(formatCompactDuration(elapsed)).append(RESET); + } + + // Downloads (merged into summary line to keep height fixed) + if (!activeTransfers.isEmpty()) { + s.append(" ").append(BLUE).append("↓ ").append(RESET); + if (activeTransfers.size() == 1) { + TransferInfo ti = activeTransfers.values().iterator().next(); + s.append(ti.artifactName); + if (ti.totalBytes > 0) { + s.append(' ') + .append(DIM) + .append(formatBytes(ti.transferred)) + .append('/') + .append(formatBytes(ti.totalBytes)) + .append(RESET); + } + } else { + s.append(activeTransfers.size()).append(" artifacts"); + } + } + + return s.toString(); + } + + // ---- Helpers ---- + + /** + * Truncate a string containing ANSI escape sequences to + * {@code maxVisible} visible characters. If truncation occurs, + * a RESET is appended to close any open styling. + */ + static String truncateAnsi(String s, int maxVisible) { + StringBuilder out = new StringBuilder(s.length()); + int visible = 0; + int i = 0; + while (i < s.length()) { + char c = s.charAt(i); + if (c == '\033') { + // Start of escape sequence — copy through without counting + out.append(c); + i++; + if (i < s.length()) { + char next = s.charAt(i); + if (next == '[') { + // CSI sequence: ESC [ ... + out.append(next); + i++; + while (i < s.length()) { + char cc = s.charAt(i); + out.append(cc); + i++; + if (Character.isLetter(cc)) { + break; + } + } + } else { + // Two-char escape (DECSC, DECRC, etc.) + out.append(next); + i++; + } + } + } else { + if (visible >= maxVisible) { + out.append(RESET); + break; + } + out.append(c); + visible++; + i++; + } + } + return out.toString(); + } + + private static String extractArtifactName(String resourceName) { + if (resourceName == null) { + return "unknown"; + } + int lastSlash = resourceName.lastIndexOf('/'); + return lastSlash >= 0 ? resourceName.substring(lastSlash + 1) : resourceName; + } + + private static String formatCompactDuration(Duration duration) { + long totalSeconds = duration.getSeconds(); + if (totalSeconds < 60) { + return totalSeconds + "s"; + } else if (totalSeconds < 3600) { + return (totalSeconds / 60) + "m " + (totalSeconds % 60) + "s"; + } else { + return (totalSeconds / 3600) + "h " + ((totalSeconds % 3600) / 60) + "m"; + } + } + + private static String formatBytes(long bytes) { + if (bytes < 1024) { + return bytes + " B"; + } else if (bytes < 1024 * 1024) { + return String.format("%.0f KB", bytes / 1024.0); + } else { + return String.format("%.1f MB", bytes / (1024.0 * 1024.0)); + } + } + + // ---- Inner state classes ---- + + private static class ProjectState { + final String projectId; + final Instant startTime; + volatile String currentMojo; + volatile boolean failed; + + ProjectState(String projectId, Instant startTime) { + this.projectId = projectId; + this.startTime = startTime; + } + } + + private static class TransferInfo { + final String artifactName; + final long totalBytes; + volatile long transferred; + volatile long lastUpdateBytes; + + TransferInfo(String artifactName, long transferred, long totalBytes) { + this.artifactName = artifactName; + this.transferred = transferred; + this.totalBytes = totalBytes; + } + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/event/RichExecutionEventLogger.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/RichExecutionEventLogger.java new file mode 100644 index 000000000000..9ced16074235 --- /dev/null +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/event/RichExecutionEventLogger.java @@ -0,0 +1,377 @@ +/* + * 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.cling.event; + +import java.time.Duration; +import java.util.List; +import java.util.Objects; + +import org.apache.maven.api.MonotonicClock; +import org.apache.maven.api.services.MessageBuilder; +import org.apache.maven.api.services.MessageBuilderFactory; +import org.apache.maven.cling.utils.CLIReportingUtils; +import org.apache.maven.execution.AbstractExecutionListener; +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.project.MavenProject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.maven.cling.utils.CLIReportingUtils.formatDuration; + +/** + * Execution event logger for the rich terminal mode ({@code --console=rich}). + *

+ * In rich mode, the {@link RichBuildEventListener} manages a JLine status bar at + * the bottom of the terminal showing live reactor progress. This logger is deliberately + * minimal — it suppresses the verbose per-mojo and per-project banners that + * {@link ExecutionEventLogger} produces, since the status bar replaces them. + *

+ * What this logger DOES print (above the status bar): + *

    + *
  • One line per completed module (like {@link PlainExecutionEventLogger})
  • + *
  • Build result summary ({@code BUILD SUCCESS/FAILURE})
  • + *
  • Compact module statistics and timing
  • + *
  • Pointer to the structured build report
  • + *
+ *

+ * What the status bar shows (managed by {@link RichBuildEventListener}): + *

    + *
  • Currently building modules with active mojo name
  • + *
  • Reactor progress ({@code [n/total]}) and elapsed time
  • + *
  • Active downloads with progress
  • + *
+ * + * @since 4.1.0 + * @see RichBuildEventListener + * @see PlainExecutionEventLogger + */ +public class RichExecutionEventLogger extends AbstractExecutionListener { + + private static final int MAX_LOG_PREFIX_SIZE = 8; // "[ERROR] " + private static final int PROJECT_STATUS_SUFFIX_SIZE = 20; // "SUCCESS [ 0.000 s]" + private static final int MIN_TERMINAL_WIDTH = 60; + private static final int DEFAULT_TERMINAL_WIDTH = 80; + private static final int MAX_TERMINAL_WIDTH = 130; + private static final int MAX_PADDED_BUILD_TIME_DURATION_LENGTH = 9; + + private final MessageBuilderFactory messageBuilderFactory; + private final Logger logger; + private final RichBuildEventListener buildEventListener; + private int terminalWidth; + private int lineLength; + private int maxProjectNameLength; + private int totalProjects; + private volatile int currentVisitedProjectCount; + + public RichExecutionEventLogger( + MessageBuilderFactory messageBuilderFactory, RichBuildEventListener buildEventListener) { + this(messageBuilderFactory, buildEventListener, LoggerFactory.getLogger(RichExecutionEventLogger.class)); + } + + public RichExecutionEventLogger( + MessageBuilderFactory messageBuilderFactory, RichBuildEventListener buildEventListener, Logger logger) { + this(messageBuilderFactory, buildEventListener, logger, -1); + } + + public RichExecutionEventLogger( + MessageBuilderFactory messageBuilderFactory, + RichBuildEventListener buildEventListener, + Logger logger, + int terminalWidth) { + this.logger = Objects.requireNonNull(logger, "logger cannot be null"); + this.messageBuilderFactory = messageBuilderFactory; + this.buildEventListener = Objects.requireNonNull(buildEventListener, "buildEventListener cannot be null"); + this.terminalWidth = terminalWidth; + } + + private void init() { + if (maxProjectNameLength == 0) { + if (terminalWidth < 0) { + terminalWidth = messageBuilderFactory.getTerminalWidth(); + } + terminalWidth = Math.min( + MAX_TERMINAL_WIDTH, + Math.max(terminalWidth <= 0 ? DEFAULT_TERMINAL_WIDTH : terminalWidth, MIN_TERMINAL_WIDTH)); + lineLength = terminalWidth - MAX_LOG_PREFIX_SIZE; + maxProjectNameLength = lineLength - PROJECT_STATUS_SUFFIX_SIZE; + } + } + + private MessageBuilder builder() { + return messageBuilderFactory.builder(); + } + + private static String chars(char c, int count) { + return String.valueOf(c).repeat(Math.max(0, count)); + } + + // ---- Session lifecycle ---- + + @Override + public void projectDiscoveryStarted(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + logger.info("Scanning for projects..."); + } + } + + @Override + public void sessionStarted(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + MavenSession session = event.getSession(); + List projects = session.getProjects(); + List allProjects = session.getAllProjects(); + + currentVisitedProjectCount = allProjects.size() - projects.size(); + totalProjects = allProjects.size(); + + // Initialize the status bar + buildEventListener.initReactor(session); + } + } + + @Override + public void sessionEnded(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + + // Tear down the status bar before printing summary + buildEventListener.tearDown(); + + // Write summary directly through the terminal writer (via buildEventListener.log) + // rather than logger.info() — all SLF4J output is routed through + // ProjectBuildLogAppender → projectLogMessage which filters INFO in rich mode. + buildEventListener.log(""); + logResult(event.getSession()); + logStats(event.getSession()); + } + } + + // ---- Module lifecycle ---- + // In rich mode, per-module success lines are suppressed — the status bar already + // shows ✓/●/○ indicators and the [n/total] counter for every module. + // Only FAILURE and SKIPPED scroll above the status bar since they're actionable. + + @Override + public void projectStarted(ExecutionEvent event) { + // Suppressed — the status bar shows active modules + } + + @Override + public void projectSucceeded(ExecutionEvent event) { + // Suppressed — the status bar checkmarks already indicate completion + } + + @Override + public void projectFailed(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + logProjectLine(event, "FAILURE"); + } + } + + @Override + public void projectSkipped(ExecutionEvent event) { + if (logger.isInfoEnabled()) { + init(); + logProjectLine(event, "SKIPPED"); + } + } + + // ---- Mojo lifecycle: suppressed (status bar shows active mojo) ---- + + @Override + public void mojoStarted(ExecutionEvent event) { + // Suppressed — the status bar shows the active mojo + } + + @Override + public void mojoSkipped(ExecutionEvent event) { + if (logger.isWarnEnabled()) { + logger.warn( + "Goal '{}' requires online mode for execution but Maven is currently offline, skipping", + event.getMojoExecution().getGoal()); + } + } + + @Override + public void forkStarted(ExecutionEvent event) { + // Suppressed in rich mode + } + + @Override + public void forkSucceeded(ExecutionEvent event) { + // Suppressed in rich mode + } + + // ---- Formatting helpers (reuses PlainExecutionEventLogger patterns) ---- + + private void logProjectLine(ExecutionEvent event, String status) { + MavenProject project = event.getProject(); + MavenSession session = event.getSession(); + MavenExecutionResult result = session.getResult(); + BuildSummary buildSummary = result.getBuildSummary(project); + + StringBuilder buffer = new StringBuilder(128); + + // Status icon + switch (status) { + case "SUCCESS": + buffer.append(" ✓ "); + break; + case "FAILURE": + buffer.append(" ✗ "); + break; + default: + buffer.append(" ○ "); + break; + } + + buffer.append(project.getName()); + buffer.append(' '); + + if (totalProjects > 1) { + int number; + synchronized (this) { + number = ++currentVisitedProjectCount; + } + String progress = "[" + number + "/" + totalProjects + "]"; + buffer.append(progress); + buffer.append(' '); + } + + // Pad with dots to align status + int effectiveMax = maxProjectNameLength - 3; // account for status icon + if (buffer.length() <= effectiveMax) { + while (buffer.length() < effectiveMax) { + buffer.append('.'); + } + buffer.append(' '); + } + + // Status with color + MessageBuilder mb = builder(); + mb.a(buffer); + switch (status) { + case "SUCCESS": + mb.success(status); + break; + case "FAILURE": + mb.failure(status); + break; + default: + mb.warning(status); + break; + } + + // Duration + if (buildSummary != null) { + mb.a(" ["); + String duration = formatDuration(buildSummary.getExecTime()); + int padSize = MAX_PADDED_BUILD_TIME_DURATION_LENGTH - duration.length(); + if (padSize > 0) { + mb.a(chars(' ', padSize)); + } + mb.a(duration); + mb.a(']'); + } + + buildEventListener.log(mb.toString()); + } + + private void logResult(MavenSession session) { + MessageBuilder buffer = builder(); + if (session.getResult().hasExceptions()) { + buffer.failure("BUILD FAILURE"); + } else { + buffer.success("BUILD SUCCESS"); + } + + int passed = 0; + int failed = 0; + int skipped = 0; + for (MavenProject project : session.getProjects()) { + BuildSummary summary = session.getResult().getBuildSummary(project); + if (summary instanceof BuildSuccess) { + passed++; + } else if (summary instanceof BuildFailure) { + failed++; + } else { + skipped++; + } + } + + buildEventListener.log(buffer.toString()); + + // Compact stats line + if (totalProjects > 1) { + StringBuilder stats = new StringBuilder(); + stats.append(totalProjects).append(" modules"); + stats.append(" | ").append(passed).append(" passed"); + if (failed > 0) { + stats.append(" | ").append(failed).append(" failed"); + } + if (skipped > 0) { + stats.append(" | ").append(skipped).append(" skipped"); + } + buildEventListener.log(stats.toString()); + } + + // Warning/error summary — warnings are suppressed inline in rich mode, + // so the count and a command hint help the user find them. + int warnings = buildEventListener.getWarningCount(); + int errors = buildEventListener.getErrorCount(); + if (warnings > 0 || errors > 0) { + MessageBuilder diag = builder(); + diag.a("Diagnostics: "); + if (warnings > 0) { + diag.warning(warnings + " warning" + (warnings > 1 ? "s" : "")); + } + if (warnings > 0 && errors > 0) { + diag.a(", "); + } + if (errors > 0) { + diag.failure(errors + " error" + (errors > 1 ? "s" : "")); + } + diag.a(" — run ").strong("mvnlog").a(" to see details"); + buildEventListener.log(diag.toString()); + } + } + + private void logStats(MavenSession session) { + Duration time = Duration.between(session.getRequest().getStartInstant(), MonotonicClock.now()); + String wallClock = session.getRequest().getDegreeOfConcurrency() > 1 ? " (Wall Clock)" : ""; + buildEventListener.log("Total time: " + formatDuration(time) + wallClock); + + // On failure, show Maven and Java version to help with bug reports (MNG-7372) + if (session.getResult().hasExceptions()) { + buildEventListener.log("Maven: " + CLIReportingUtils.showVersionMinimal()); + buildEventListener.log("Java: " + System.getProperty("java.version", "") + " (" + + System.getProperty("java.vendor", "") + ")"); + } + + buildEventListener.log("Full report: target/build-reports/build-report-latest.json"); + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java index 7f5768715ae4..a75e79d9ba54 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/CommonsCliOptions.java @@ -212,6 +212,26 @@ public Optional color() { return Optional.empty(); } + @Override + public Optional console() { + if (commandLine.hasOption(CLIManager.CONSOLE)) { + if (commandLine.getOptionValue(CLIManager.CONSOLE) != null) { + return Optional.of(commandLine.getOptionValue(CLIManager.CONSOLE)); + } else { + return Optional.of("auto"); + } + } + return Optional.empty(); + } + + @Override + public Optional warningMode() { + if (commandLine.hasOption(CLIManager.WARNING_MODE)) { + return Optional.of(commandLine.getOptionValue(CLIManager.WARNING_MODE)); + } + return Optional.empty(); + } + @Override public Optional offline() { if (commandLine.hasOption(CLIManager.OFFLINE)) { @@ -315,6 +335,8 @@ protected static class CLIManager { public static final String LOG_FILE = "l"; public static final String RAW_STREAMS = "raw-streams"; public static final String COLOR = "color"; + public static final String CONSOLE = "console"; + public static final String WARNING_MODE = "warning-mode"; public static final String OFFLINE = "o"; public static final String HELP = "h"; @@ -344,13 +366,13 @@ protected CLIManager() { prepareOptions(options); } + @SuppressWarnings("checkstyle:MethodLength") protected void prepareOptions(org.apache.commons.cli.Options options) { options.addOption(Option.builder(HELP) .longOpt("help") .desc("Display help information") .get()); options.addOption(Option.builder(USER_PROPERTY) - .longOpt("define") .numberOfArgs(2) .valueSeparator('=') .desc("Define a user property") @@ -373,7 +395,7 @@ protected void prepareOptions(org.apache.commons.cli.Options options) { .get()); options.addOption(Option.builder(BATCH_MODE) .longOpt("batch-mode") - .desc("Run in non-interactive mode. Alias for --non-interactive (kept for backwards compatibility)") + .desc("Run in non-interactive mode. Alias for --non-interactive (kept for backwards compatability)") .get()); options.addOption(Option.builder() .longOpt(NON_INTERACTIVE) @@ -433,6 +455,23 @@ protected void prepareOptions(org.apache.commons.cli.Options options) { .optionalArg(true) .desc("Defines the color mode of the output. Supported are 'auto', 'always', 'never'.") .get()); + options.addOption(Option.builder() + .longOpt(CONSOLE) + .hasArg() + .optionalArg(true) + .desc("Defines the console output mode. Supported are 'auto' (default)," + + " 'plain', 'rich', 'verbose', 'machine'." + + " In 'auto' mode, CI environments use 'plain'," + + " interactive TTYs use 'rich' (status bar)." + + " 'machine' outputs one JSON line per lifecycle event.") + .get()); + options.addOption(Option.builder() + .longOpt(WARNING_MODE) + .hasArg() + .desc("Controls how build warnings are displayed." + + " Supported modes: 'summary' (default, deduplicated summary at end)," + + " 'all' (inline + summary), 'none' (suppress), 'fail' (treat warnings as errors).") + .get()); options.addOption(Option.builder(OFFLINE) .longOpt("offline") .desc("Work offline") @@ -539,4 +578,4 @@ protected String commandLineSyntax(String command) { return command + " [options] [ ...]"; } } -} +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LayeredOptions.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LayeredOptions.java index 2f9c367c4786..0fd64704aaf2 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LayeredOptions.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LayeredOptions.java @@ -132,6 +132,16 @@ public Optional color() { return returnFirstPresentOrEmpty(Options::color); } + @Override + public Optional console() { + return returnFirstPresentOrEmpty(Options::console); + } + + @Override + public Optional warningMode() { + return returnFirstPresentOrEmpty(Options::warningMode); + } + @Override public Optional offline() { return returnFirstPresentOrEmpty(Options::offline); @@ -188,4 +198,4 @@ protected Optional> collectMapIfPresentOrEmpty( } return had == 0 ? Optional.empty() : Optional.of(Map.copyOf(items)); } -} +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java index bf156b69498d..1372083f9f7a 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java @@ -78,6 +78,17 @@ public LookupContext(InvokerRequest invokerRequest, boolean containerCapsuleMana public Logger logger; + /** + * Early log entries accumulated before {@link #createTerminal()} wires up + * the {@link org.apache.maven.slf4j.MavenSimpleLogger} log-sink and any + * {@code -l} log-file writer. Set by {@code activateLogging()} (which + * runs before {@code createTerminal()}) and drained in + * {@code createTerminal()} after the sink is installed, so that early + * messages such as "Enabled to break the build on log level WARN." reach + * the log file rather than going to stdout via {@code super.write()}. + */ + public List pendingEarlyLogs; + // this one "evolves" as process progresses (instance is immutable but instances are replaced) public ProtoSession protoSession; // here we track which user properties we pushed to Java System Properties (internal only) diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java index 3e027ef4bbf2..4e9c2c6bded1 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java @@ -78,12 +78,12 @@ import org.apache.maven.impl.SettingsUtilsV4; import org.apache.maven.jline.FastTerminal; import org.apache.maven.jline.MessageUtils; -import org.apache.maven.logging.AsyncDrainWriter; import org.apache.maven.logging.BuildEventListener; import org.apache.maven.logging.LoggingOutputStream; import org.apache.maven.logging.ProjectBuildLogAppender; import org.apache.maven.logging.SimpleBuildEventListener; import org.apache.maven.logging.api.LogLevelRecorder; +import org.apache.maven.slf4j.MavenJulHandler; import org.apache.maven.slf4j.MavenSimpleLogger; import org.codehaus.plexus.PlexusContainer; import org.jline.terminal.Terminal; @@ -92,7 +92,6 @@ import org.jline.terminal.spi.TerminalExt; import org.jline.utils.OSUtils; import org.slf4j.LoggerFactory; -import org.slf4j.bridge.SLF4JBridgeHandler; import org.slf4j.spi.LocationAwareLogger; import static java.util.Objects.requireNonNull; @@ -156,6 +155,7 @@ protected int doInvoke(C context) throws Exception { pushUserProperties(context); setupGuiceClassLoading(context); configureLogging(context); + preliminaryInteractiveDetection(context); createTerminal(context); activateLogging(context); helpOrVersionAndMayExit(context); @@ -302,6 +302,30 @@ protected void configureLogging(C context) throws Exception { } } + /** + * Sets {@code context.interactive} based on CLI flags and CI detection before + * {@link #createTerminal(LookupContext)} runs. This is necessary because + * {@code createTerminal} caches the {@link BuildEventListener} (via + * {@link #determineBuildEventListener}), and the console-mode auto-detection + * in subclasses reads {@code context.interactive} to decide between rich/plain/verbose. + * + *

The full settings-based interactive-mode resolution still runs later in + * {@link #settings}, so this is a best-effort early pass using only CLI flags and + * CI environment detection — which is sufficient for the console-mode decision.

+ */ + protected void preliminaryInteractiveDetection(C context) { + if (context.options().forceInteractive().orElse(false)) { + context.interactive = true; + } else if (context.options().nonInteractive().orElse(false)) { + context.interactive = false; + } else if (context.invokerRequest.ciInfo().isPresent()) { + context.interactive = false; + } else { + // Default: assume interactive (settings may refine later) + context.interactive = true; + } + } + protected BuildEventListener determineBuildEventListener(C context) { if (context.buildEventListener == null) { context.buildEventListener = doDetermineBuildEventListener(context); @@ -420,36 +444,29 @@ protected Consumer determineWriter(C context) { } protected Consumer doDetermineWriter(C context) { - Consumer raw; if (context.options().logFile().isPresent()) { Path logFile = context.cwd.resolve(context.options().logFile().get()); try { PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(logFile), true); context.closeables.add(printWriter); - raw = printWriter::println; + return printWriter::println; } catch (IOException e) { throw new MavenException("Unable to redirect logging to " + logFile, e); } } else { // Given the terminal creation has been offloaded to a different thread, // do not pass directly the terminal writer - raw = msg -> { + return msg -> { PrintWriter pw = context.terminal.writer(); pw.println(msg); pw.flush(); }; } - // Wrap with lock-free async drain to eliminate PrintWriter synchronized contention - // when multiple PhasingExecutor threads log concurrently during parallel model building. - AsyncDrainWriter asyncWriter = new AsyncDrainWriter(raw); - context.closeables.add(asyncWriter); - return asyncWriter; } protected void activateLogging(C context) throws Exception { - if (!SLF4JBridgeHandler.isInstalled()) { - SLF4JBridgeHandler.removeHandlersForRootLogger(); - SLF4JBridgeHandler.install(); + if (!MavenJulHandler.isInstalled()) { + MavenJulHandler.install(); } context.slf4jConfiguration.activate(); @@ -527,7 +544,7 @@ protected String describe(Terminal terminal) { protected void preCommands(C context) throws Exception { boolean verbose = context.invokerRequest.effectiveVerbose(); - boolean version = context.options().showVersion().orElse(false) && !Boolean.getBoolean("maven.version.printed"); + boolean version = context.options().showVersion().orElse(false); if (verbose || version) { showVersion(context); } @@ -964,4 +981,4 @@ protected int calculateDegreeOfConcurrency(String threadConfiguration) { } protected abstract int execute(C context) throws Exception; -} +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java index e6372ccfd818..bc03026990d7 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java @@ -48,6 +48,11 @@ import org.apache.maven.api.services.model.ModelProcessor; import org.apache.maven.api.toolchain.PersistedToolchains; import org.apache.maven.cling.event.ExecutionEventLogger; +import org.apache.maven.cling.event.MachineBuildEventListener; +import org.apache.maven.cling.event.MachineExecutionEventLogger; +import org.apache.maven.cling.event.PlainExecutionEventLogger; +import org.apache.maven.cling.event.RichBuildEventListener; +import org.apache.maven.cling.event.RichExecutionEventLogger; import org.apache.maven.cling.invoker.CliUtils; import org.apache.maven.cling.invoker.LookupContext; import org.apache.maven.cling.invoker.LookupInvoker; @@ -66,6 +71,7 @@ import org.apache.maven.execution.ProjectActivation; import org.apache.maven.jline.MessageUtils; import org.apache.maven.lifecycle.LifecycleExecutionException; +import org.apache.maven.logging.BuildEventListener; import org.apache.maven.logging.LoggingExecutionListener; import org.apache.maven.logging.MavenTransferListener; import org.apache.maven.project.MavenProject; @@ -257,6 +263,11 @@ protected void populateRequest(MavenContext context, Lookup lookup, MavenExecuti } } + // Propagate warning mode and diagnostic suppression to the session so + // BuildReportCollector (an EventSpy) can read them from user properties + String warningMode = context.options().warningMode().orElse("summary"); + request.getUserProperties().put("maven.build.warningMode", warningMode); + request.setTransferListener(determineTransferListener( context, context.options().noTransferProgress().orElse(false))); request.setExecutionListener(determineExecutionListener(context)); @@ -343,21 +354,102 @@ protected String determineGlobalChecksumPolicy(MavenContext context) { } protected ExecutionListener determineExecutionListener(MavenContext context) { - ExecutionListener listener = new ExecutionEventLogger(context.invokerRequest.messageBuilderFactory()); + ExecutionListener listener; + String consoleMode = determineConsoleMode(context); + switch (consoleMode) { + case "machine": + BuildEventListener machineBel = determineBuildEventListener(context); + if (machineBel instanceof MachineBuildEventListener machineListener) { + listener = new MachineExecutionEventLogger(machineListener); + } else { + // Fallback if machine listener couldn't be created + listener = new PlainExecutionEventLogger(context.invokerRequest.messageBuilderFactory()); + } + break; + case "rich": + BuildEventListener richBel = determineBuildEventListener(context); + if (richBel instanceof RichBuildEventListener richListener) { + listener = + new RichExecutionEventLogger(context.invokerRequest.messageBuilderFactory(), richListener); + } else { + // Fallback if status bar couldn't be created + listener = new PlainExecutionEventLogger(context.invokerRequest.messageBuilderFactory()); + } + break; + case "plain": + listener = new PlainExecutionEventLogger(context.invokerRequest.messageBuilderFactory()); + break; + default: + listener = new ExecutionEventLogger(context.invokerRequest.messageBuilderFactory()); + break; + } if (context.eventSpyDispatcher != null) { listener = context.eventSpyDispatcher.chainListener(listener); } return new LoggingExecutionListener(listener, determineBuildEventListener(context)); } + @Override + protected BuildEventListener doDetermineBuildEventListener(MavenContext context) { + String consoleMode = determineConsoleMode(context); + if ("machine".equals(consoleMode)) { + return new MachineBuildEventListener(determineWriter(context)); + } + if ("rich".equals(consoleMode) && context.terminal != null) { + return new RichBuildEventListener(context.terminal, determineWriter(context)); + } + return super.doDetermineBuildEventListener(context); + } + + /** + * Resolves the effective console mode from the {@code --console} flag and CI/TTY detection. + *

+ * Resolution order: + *

    + *
  1. Explicit {@code --console=plain}, {@code --console=verbose}, {@code --console=rich}, + * or {@code --console=machine} — always honored
  2. + *
  3. {@code --console=auto} (or unset) — selects mode based on environment: + *
      + *
    • CI detected → "plain"
    • + *
    • Interactive TTY → "rich"
    • + *
    • Otherwise → "verbose"
    • + *
    + *
  4. + *
+ */ + String determineConsoleMode(MavenContext context) { + String consoleMode = context.options().console().orElse("auto"); + if ("plain".equalsIgnoreCase(consoleMode) + || "verbose".equalsIgnoreCase(consoleMode) + || "rich".equalsIgnoreCase(consoleMode) + || "machine".equalsIgnoreCase(consoleMode)) { + return consoleMode.toLowerCase(); + } + // "auto" mode: CI → plain, interactive TTY → rich, otherwise → verbose + if (context.invokerRequest.ciInfo().isPresent() + && !context.options().forceInteractive().orElse(false)) { + return "plain"; + } + if (context.interactive && context.terminal != null && !context.invokerRequest.embedded()) { + return "rich"; + } + return "verbose"; + } + protected TransferListener determineTransferListener(MavenContext context, boolean noTransferProgress) { boolean quiet = context.options().quiet().orElse(false); boolean logFile = context.options().logFile().isPresent(); boolean quietCI = context.invokerRequest.ciInfo().isPresent() && !context.options().forceInteractive().orElse(false); + String mode = determineConsoleMode(context); + boolean richMode = "rich".equals(mode); + boolean machineMode = "machine".equals(mode); TransferListener delegate; - if (quiet || noTransferProgress || quietCI) { + if (quiet || noTransferProgress || quietCI || richMode || machineMode) { + // In rich mode, transfer progress is shown in the JLine status bar. + // In machine mode, transfer events are emitted as JSON lines. + // In both cases, suppress the console transfer listener. delegate = new QuietMavenTransferListener(); } else if (context.interactive && !logFile) { if (context.simplexTransferListener == null) { @@ -616,4 +708,4 @@ protected void logSummary( logSummary(context, child, references, indent); } } -} +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/resources/META-INF/maven/slf4j-configuration.properties b/impl/maven-cli/src/main/resources/META-INF/maven/slf4j-configuration.properties index 369b0e6a266b..9580d5071ac9 100644 --- a/impl/maven-cli/src/main/resources/META-INF/maven/slf4j-configuration.properties +++ b/impl/maven-cli/src/main/resources/META-INF/maven/slf4j-configuration.properties @@ -19,5 +19,3 @@ # value = corresponding o.a.m.cli.logging.Slf4jConfiguration class org.slf4j.impl.SimpleLoggerFactory=org.apache.maven.cling.logging.impl.MavenSimpleConfiguration org.apache.maven.slf4j.MavenLoggerFactory=org.apache.maven.cling.logging.impl.MavenSimpleConfiguration -org.apache.logging.slf4j.Log4jLoggerFactory=org.apache.maven.cling.logging.impl.Log4j2Configuration -ch.qos.logback.classic.LoggerContext=org.apache.maven.cling.logging.impl.LogbackConfiguration diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/event/MachineBuildEventListenerTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/MachineBuildEventListenerTest.java new file mode 100644 index 000000000000..74e5e9cca486 --- /dev/null +++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/MachineBuildEventListenerTest.java @@ -0,0 +1,239 @@ +/* + * 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.cling.event; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.api.MonotonicClock; +import org.apache.maven.api.build.report.LogLevel; +import org.apache.maven.internal.build.DefaultLogEvent; +import org.eclipse.aether.transfer.TransferEvent; +import org.eclipse.aether.transfer.TransferResource; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.mockito.MockitoSession; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link MachineBuildEventListener}. + */ +class MachineBuildEventListenerTest { + + private MockitoSession mockitoSession; + private List capturedOutput; + private MachineBuildEventListener listener; + + @BeforeEach + void beforeEach() { + mockitoSession = Mockito.mockitoSession().startMocking(); + capturedOutput = new ArrayList<>(); + listener = new MachineBuildEventListener(capturedOutput::add); + } + + @org.junit.jupiter.api.AfterEach + void afterEach() { + mockitoSession.finishMocking(); + } + + @Test + void testLogEmitsJsonLine() { + listener.log("Hello world"); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.startsWith("{"), "Should be JSON object"); + assertTrue(json.endsWith("}"), "Should be JSON object"); + assertTrue(json.contains("\"event\":\"log\""), "Should have event type"); + assertTrue(json.contains("\"message\":\"Hello world\""), "Should have message"); + assertTrue(json.contains("\"timestamp\":\""), "Should have timestamp"); + } + + @Test + void testProjectLogMessageIncludesModule() { + listener.projectLogMessage( + "my-core", + new DefaultLogEvent( + MonotonicClock.now(), + LogLevel.INFO, + "Compiling 42 source files", + "compiler", + null, + "[INFO] Compiling 42 source files")); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"log\"")); + assertTrue(json.contains("\"level\":\"INFO\"")); + assertTrue(json.contains("\"module\":\"my-core\"")); + assertTrue(json.contains("\"message\":\"Compiling 42 source files\"")); + } + + @Test + void testExecutionFailureEmitsJsonLine() { + listener.executionFailure("my-core", true, "Compilation failed"); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"execution.failure\"")); + assertTrue(json.contains("\"module\":\"my-core\"")); + assertTrue(json.contains("\"halted\":true")); + assertTrue(json.contains("\"error\":\"Compilation failed\"")); + } + + @Test + void testTransferStartedEmitsJsonLine() { + TransferResource resource = mock(TransferResource.class); + when(resource.getResourceName()).thenReturn("org/apache/maven/core/4.1.0/core-4.1.0.jar"); + when(resource.getContentLength()).thenReturn(524288L); + + TransferEvent event = mock(TransferEvent.class); + when(event.getType()).thenReturn(TransferEvent.EventType.STARTED); + when(event.getResource()).thenReturn(resource); + + listener.transfer("my-core", event); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"transfer.started\"")); + assertTrue(json.contains("\"artifact\":\"core-4.1.0.jar\"")); + assertTrue(json.contains("\"size\":524288")); + assertTrue(json.contains("\"module\":\"my-core\"")); + } + + @Test + void testTransferProgressedEmitsJsonLine() { + TransferResource resource = mock(TransferResource.class); + when(resource.getResourceName()).thenReturn("org/apache/maven/core/4.1.0/core-4.1.0.jar"); + when(resource.getContentLength()).thenReturn(524288L); + + TransferEvent event = mock(TransferEvent.class); + when(event.getType()).thenReturn(TransferEvent.EventType.PROGRESSED); + when(event.getResource()).thenReturn(resource); + when(event.getTransferredBytes()).thenReturn(262144L); + + listener.transfer("my-core", event); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"transfer.progressed\"")); + assertTrue(json.contains("\"transferred\":262144")); + assertTrue(json.contains("\"total\":524288")); + } + + @Test + void testTransferCompletedEmitsJsonLine() { + TransferResource resource = mock(TransferResource.class); + when(resource.getResourceName()).thenReturn("org/apache/maven/core/4.1.0/core-4.1.0.jar"); + when(resource.getContentLength()).thenReturn(524288L); + + TransferEvent event = mock(TransferEvent.class); + when(event.getType()).thenReturn(TransferEvent.EventType.SUCCEEDED); + when(event.getResource()).thenReturn(resource); + when(event.getTransferredBytes()).thenReturn(524288L); + + listener.transfer("my-core", event); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"transfer.completed\"")); + assertTrue(json.contains("\"transferred\":524288")); + } + + @Test + void testTransferFailedEmitsJsonLine() { + TransferResource resource = mock(TransferResource.class); + when(resource.getResourceName()).thenReturn("org/apache/maven/core/4.1.0/core-4.1.0.jar"); + when(resource.getContentLength()).thenReturn(524288L); + + TransferEvent event = mock(TransferEvent.class); + when(event.getType()).thenReturn(TransferEvent.EventType.FAILED); + when(event.getResource()).thenReturn(resource); + when(event.getException()).thenReturn(new RuntimeException("Connection timed out")); + + listener.transfer("my-core", event); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"transfer.failed\"")); + assertTrue(json.contains("\"error\":\"Connection timed out\"")); + } + + @Test + void testJsonEscapingInMessages() { + listener.log("Message with \"quotes\" and \\backslash and\nnewline"); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + // Verify the JSON is properly escaped + assertTrue(json.contains("\\\"quotes\\\""), "Quotes should be escaped"); + assertTrue(json.contains("\\\\backslash"), "Backslash should be escaped"); + assertTrue(json.contains("\\n"), "Newline should be escaped"); + // Verify it's parseable as a single line (no raw newlines) + assertFalse(json.contains("\n"), "JSON line should not contain raw newlines"); + } + + @Test + void testEmitEventIsSynchronized() throws Exception { + // Verify that concurrent calls to emitEvent don't interleave + List output = new ArrayList<>(); + MachineBuildEventListener concurrentListener = new MachineBuildEventListener(output::add); + + Thread t1 = new Thread(() -> { + for (int i = 0; i < 100; i++) { + concurrentListener.log("Thread1-" + i); + } + }); + Thread t2 = new Thread(() -> { + for (int i = 0; i < 100; i++) { + concurrentListener.log("Thread2-" + i); + } + }); + + t1.start(); + t2.start(); + t1.join(); + t2.join(); + + assertEquals(200, output.size(), "All events should be emitted"); + // Each line should be a complete JSON object + for (String line : output) { + assertTrue(line.startsWith("{") && line.endsWith("}"), "Each line should be a complete JSON object"); + } + } + + @Test + void testNoOpMethods() throws Exception { + // These should not produce any output (handled by MachineExecutionEventLogger) + listener.sessionStarted(null); + listener.projectStarted("my-core"); + listener.projectFinished("my-core"); + listener.mojoStarted(null); + listener.finish(0); + listener.fail(new RuntimeException("error")); + + assertEquals(0, capturedOutput.size(), "No-op methods should not produce output"); + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/event/MachineExecutionEventLoggerTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/MachineExecutionEventLoggerTest.java new file mode 100644 index 000000000000..be9c88edcd82 --- /dev/null +++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/MachineExecutionEventLoggerTest.java @@ -0,0 +1,445 @@ +/* + * 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.cling.event; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.maven.execution.BuildFailure; +import org.apache.maven.execution.BuildSuccess; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.ExecutionEvent; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionResult; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.project.MavenProject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.mockito.MockitoSession; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link MachineExecutionEventLogger}. + */ +class MachineExecutionEventLoggerTest { + + private MockitoSession mockitoSession; + private List capturedOutput; + private MachineBuildEventListener machineBel; + private MachineExecutionEventLogger logger; + + @BeforeEach + void beforeEach() { + mockitoSession = Mockito.mockitoSession().startMocking(); + capturedOutput = new ArrayList<>(); + machineBel = new MachineBuildEventListener(capturedOutput::add); + logger = new MachineExecutionEventLogger(machineBel); + } + + @AfterEach + void afterEach() { + mockitoSession.finishMocking(); + } + + @Test + void testSessionStartedEmitsBuildStarted() { + MavenProject project1 = createProject("API", "api"); + MavenProject project2 = createProject("Core", "core"); + + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setGoals(List.of("clean", "install")); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project1, project2)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2)); + when(session.getRequest()).thenReturn(request); + + ExecutionEvent event = mock(ExecutionEvent.class); + when(event.getSession()).thenReturn(session); + + logger.sessionStarted(event); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"build.started\"")); + assertTrue(json.contains("\"projectCount\":2")); + assertTrue(json.contains("\"goals\":\"clean install\"")); + } + + @Test + void testSessionEndedEmitsBuildFinished() { + MavenProject project1 = createProject("API", "api"); + MavenProject project2 = createProject("Core", "core"); + + MavenExecutionResult result = new DefaultMavenExecutionResult(); + result.addBuildSummary(new BuildSuccess(project1, 1000)); + result.addBuildSummary(new BuildSuccess(project2, 2000)); + + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setGoals(List.of("install")); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project1, project2)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2)); + when(session.getResult()).thenReturn(result); + when(session.getRequest()).thenReturn(request); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + logger.sessionStarted(sessionEvent); + capturedOutput.clear(); + + logger.sessionEnded(sessionEvent); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"build.finished\"")); + assertTrue(json.contains("\"status\":\"SUCCESS\"")); + assertTrue(json.contains("\"total\":2")); + assertTrue(json.contains("\"passed\":2")); + assertTrue(json.contains("\"failed\":0")); + assertTrue(json.contains("\"skipped\":0")); + assertTrue(json.contains("\"duration\":")); + } + + @Test + void testSessionEndedWithFailures() { + MavenProject project1 = createProject("API", "api"); + MavenProject project2 = createProject("Core", "core"); + MavenProject project3 = createProject("CLI", "cli"); + + MavenExecutionResult result = new DefaultMavenExecutionResult(); + result.addBuildSummary(new BuildSuccess(project1, 1000)); + result.addBuildSummary(new BuildFailure(project2, 2000, new Exception("Compile error"))); + result.addException(new Exception("Compile error")); + + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setGoals(List.of("install")); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getResult()).thenReturn(result); + when(session.getRequest()).thenReturn(request); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + logger.sessionStarted(sessionEvent); + capturedOutput.clear(); + + logger.sessionEnded(sessionEvent); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"status\":\"FAILURE\"")); + assertTrue(json.contains("\"passed\":1")); + assertTrue(json.contains("\"failed\":1")); + assertTrue(json.contains("\"skipped\":1")); + } + + @Test + void testProjectStartedEmitsModuleStarted() { + MavenProject project = createProject("Maven Core", "maven-core"); + setupSessionForProjectEvents(project); + + ExecutionEvent event = mock(ExecutionEvent.class); + when(event.getProject()).thenReturn(project); + + logger.projectStarted(event); + + // build.started + module.started + assertEquals(2, capturedOutput.size()); + String json = capturedOutput.get(1); + assertTrue(json.contains("\"event\":\"module.started\"")); + assertTrue(json.contains("\"module\":\"Maven Core\"")); + assertTrue(json.contains("\"groupId\":\"org.apache.maven\"")); + assertTrue(json.contains("\"artifactId\":\"maven-core\"")); + assertTrue(json.contains("\"version\":\"4.1.0-SNAPSHOT\"")); + assertTrue(json.contains("\"index\":1")); + assertTrue(json.contains("\"total\":1")); + } + + @Test + void testProjectSucceededEmitsModuleSucceeded() { + MavenProject project = createProject("Core", "core"); + MavenSession session = setupSessionForProjectEvents(project); + + MavenExecutionResult result = new DefaultMavenExecutionResult(); + result.addBuildSummary(new BuildSuccess(project, 2100)); + when(session.getResult()).thenReturn(result); + + ExecutionEvent event = mock(ExecutionEvent.class); + when(event.getProject()).thenReturn(project); + when(event.getSession()).thenReturn(session); + + logger.projectSucceeded(event); + + // build.started + module.succeeded + assertEquals(2, capturedOutput.size()); + String json = capturedOutput.get(1); + assertTrue(json.contains("\"event\":\"module.succeeded\"")); + assertTrue(json.contains("\"module\":\"Core\"")); + assertTrue(json.contains("\"duration\":2.1")); + } + + @Test + void testProjectFailedEmitsModuleFailed() { + MavenProject project = createProject("Core", "core"); + MavenSession session = setupSessionForProjectEvents(project); + + MavenExecutionResult result = new DefaultMavenExecutionResult(); + result.addBuildSummary(new BuildFailure(project, 5000, new Exception("Compile error"))); + when(session.getResult()).thenReturn(result); + + ExecutionEvent event = mock(ExecutionEvent.class); + when(event.getProject()).thenReturn(project); + when(event.getSession()).thenReturn(session); + when(event.getException()).thenReturn(new Exception("Compile error")); + + logger.projectFailed(event); + + assertEquals(2, capturedOutput.size()); + String json = capturedOutput.get(1); + assertTrue(json.contains("\"event\":\"module.failed\"")); + assertTrue(json.contains("\"duration\":5.0")); + assertTrue(json.contains("\"error\":\"Compile error\"")); + } + + @Test + void testMojoStartedEmitsJsonLine() { + MavenProject project = createProject("Core", "core"); + MojoExecution mojo = createMojoExecution("maven-compiler-plugin", "compile", "compile", "default-compile"); + + ExecutionEvent event = mock(ExecutionEvent.class); + when(event.getProject()).thenReturn(project); + when(event.getMojoExecution()).thenReturn(mojo); + + logger.mojoStarted(event); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"mojo.started\"")); + assertTrue(json.contains("\"module\":\"Core\"")); + assertTrue(json.contains("\"plugin\":\"maven-compiler-plugin\"")); + assertTrue(json.contains("\"goal\":\"compile\"")); + assertTrue(json.contains("\"phase\":\"compile\"")); + assertTrue(json.contains("\"executionId\":\"default-compile\"")); + } + + @Test + void testMojoSucceededIncludesDuration() { + MavenProject project = createProject("Core", "core"); + MojoExecution mojo = createMojoExecution("maven-compiler-plugin", "compile", "compile", "default-compile"); + + ExecutionEvent startEvent = mock(ExecutionEvent.class); + when(startEvent.getProject()).thenReturn(project); + when(startEvent.getMojoExecution()).thenReturn(mojo); + + ExecutionEvent endEvent = mock(ExecutionEvent.class); + when(endEvent.getProject()).thenReturn(project); + when(endEvent.getMojoExecution()).thenReturn(mojo); + + logger.mojoStarted(startEvent); + capturedOutput.clear(); + + logger.mojoSucceeded(endEvent); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"mojo.succeeded\"")); + assertTrue(json.contains("\"duration\":")); + } + + @Test + void testMojoFailedIncludesError() { + MavenProject project = createProject("Core", "core"); + MojoExecution mojo = createMojoExecution("maven-compiler-plugin", "compile", "compile", "default-compile"); + + ExecutionEvent startEvent = mock(ExecutionEvent.class); + when(startEvent.getProject()).thenReturn(project); + when(startEvent.getMojoExecution()).thenReturn(mojo); + + ExecutionEvent endEvent = mock(ExecutionEvent.class); + when(endEvent.getProject()).thenReturn(project); + when(endEvent.getMojoExecution()).thenReturn(mojo); + when(endEvent.getException()).thenReturn(new Exception("Cannot find symbol: class Foo")); + + logger.mojoStarted(startEvent); + capturedOutput.clear(); + + logger.mojoFailed(endEvent); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"mojo.failed\"")); + assertTrue(json.contains("\"error\":\"Cannot find symbol: class Foo\"")); + } + + @Test + void testMojoSkippedEmitsJsonLine() { + MavenProject project = createProject("Core", "core"); + MojoExecution mojo = createMojoExecution("maven-deploy-plugin", "deploy", "deploy", "default-deploy"); + + ExecutionEvent event = mock(ExecutionEvent.class); + when(event.getProject()).thenReturn(project); + when(event.getMojoExecution()).thenReturn(mojo); + + logger.mojoSkipped(event); + + assertEquals(1, capturedOutput.size()); + String json = capturedOutput.get(0); + assertTrue(json.contains("\"event\":\"mojo.skipped\"")); + assertTrue(json.contains("\"plugin\":\"maven-deploy-plugin\"")); + assertTrue(json.contains("\"goal\":\"deploy\"")); + } + + @Test + void testMultiModuleLifecycle() { + MavenProject project1 = createProject("API", "api"); + MavenProject project2 = createProject("Core", "core"); + MavenProject project3 = createProject("CLI", "cli"); + + MavenExecutionResult result = new DefaultMavenExecutionResult(); + result.addBuildSummary(new BuildSuccess(project1, 1000)); + result.addBuildSummary(new BuildSuccess(project2, 3000)); + result.addBuildSummary(new BuildSuccess(project3, 2000)); + + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setGoals(List.of("install")); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getResult()).thenReturn(result); + when(session.getRequest()).thenReturn(request); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + // Full lifecycle + logger.sessionStarted(sessionEvent); + + for (MavenProject p : List.of(project1, project2, project3)) { + ExecutionEvent pe = mock(ExecutionEvent.class); + when(pe.getProject()).thenReturn(p); + when(pe.getSession()).thenReturn(session); + logger.projectStarted(pe); + logger.projectSucceeded(pe); + } + + logger.sessionEnded(sessionEvent); + + // build.started + 3*(module.started + module.succeeded) + build.finished = 8 + assertEquals(8, capturedOutput.size()); + + // First event is build.started + assertTrue(capturedOutput.get(0).contains("\"event\":\"build.started\"")); + // Check module indices + assertTrue(capturedOutput.get(1).contains("\"index\":1")); + assertTrue(capturedOutput.get(3).contains("\"index\":2")); + assertTrue(capturedOutput.get(5).contains("\"index\":3")); + // Last event is build.finished + assertTrue(capturedOutput.get(7).contains("\"event\":\"build.finished\"")); + } + + @Test + void testAllOutputIsValidJsonLines() { + MavenProject project = createProject("Core", "core"); + MavenSession session = setupSessionForProjectEvents(project); + + MavenExecutionResult result = new DefaultMavenExecutionResult(); + result.addBuildSummary(new BuildSuccess(project, 1000)); + when(session.getResult()).thenReturn(result); + + MojoExecution mojo = createMojoExecution("maven-compiler-plugin", "compile", "compile", "default-compile"); + + ExecutionEvent projectEvent = mock(ExecutionEvent.class); + when(projectEvent.getProject()).thenReturn(project); + when(projectEvent.getSession()).thenReturn(session); + when(projectEvent.getMojoExecution()).thenReturn(mojo); + + // Generate various events + logger.projectStarted(projectEvent); + logger.mojoStarted(projectEvent); + machineBel.log("Some log message"); + logger.mojoSucceeded(projectEvent); + logger.projectSucceeded(projectEvent); + + // All lines should be valid JSON (start with { and end with }) + for (String line : capturedOutput) { + assertTrue(line.startsWith("{"), "Should start with {: " + line); + assertTrue(line.endsWith("}"), "Should end with }: " + line); + // Should not contain raw newlines + assertFalse(line.contains("\n"), "Should not contain raw newlines: " + line); + assertFalse(line.contains("\r"), "Should not contain raw carriage returns: " + line); + } + } + + // ---- Helpers ---- + + private static MavenProject createProject(String name, String artifactId) { + MavenProject project = mock(MavenProject.class); + lenient().when(project.getName()).thenReturn(name); + lenient().when(project.getArtifactId()).thenReturn(artifactId); + lenient().when(project.getGroupId()).thenReturn("org.apache.maven"); + lenient().when(project.getVersion()).thenReturn("4.1.0-SNAPSHOT"); + lenient().when(project.getPackaging()).thenReturn("jar"); + return project; + } + + private static MojoExecution createMojoExecution(String artifactId, String goal, String phase, String executionId) { + MojoExecution mojo = mock(MojoExecution.class); + lenient().when(mojo.getArtifactId()).thenReturn(artifactId); + lenient().when(mojo.getGoal()).thenReturn(goal); + lenient().when(mojo.getLifecyclePhase()).thenReturn(phase); + lenient().when(mojo.getExecutionId()).thenReturn(executionId); + return mojo; + } + + private MavenSession setupSessionForProjectEvents(MavenProject project) { + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setGoals(List.of("install")); + + MavenExecutionResult result = new DefaultMavenExecutionResult(); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project)); + when(session.getAllProjects()).thenReturn(List.of(project)); + lenient().when(session.getResult()).thenReturn(result); + when(session.getRequest()).thenReturn(request); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + logger.sessionStarted(sessionEvent); + + return session; + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/event/PlainExecutionEventLoggerTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/PlainExecutionEventLoggerTest.java new file mode 100644 index 000000000000..7b183e1c055c --- /dev/null +++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/PlainExecutionEventLoggerTest.java @@ -0,0 +1,317 @@ +/* + * 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.cling.event; + +import java.util.List; + +import org.apache.maven.execution.BuildFailure; +import org.apache.maven.execution.BuildSuccess; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.ExecutionEvent; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionResult; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.jline.JLineMessageBuilderFactory; +import org.apache.maven.jline.MessageUtils; +import org.apache.maven.project.MavenProject; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InOrder; +import org.mockito.Mockito; +import org.mockito.MockitoSession; +import org.slf4j.Logger; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.matches; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link PlainExecutionEventLogger} — the compact one-line-per-module renderer. + */ +class PlainExecutionEventLoggerTest { + + private MockitoSession mockitoSession; + + private Logger logger; + private PlainExecutionEventLogger plainLogger; + private final JLineMessageBuilderFactory messageBuilderFactory = new JLineMessageBuilderFactory(); + + @BeforeAll + static void setUp() { + MessageUtils.setColorEnabled(false); + } + + @AfterAll + static void tearDown() { + MessageUtils.setColorEnabled(true); + } + + @BeforeEach + void beforeEach() { + mockitoSession = Mockito.mockitoSession().startMocking(); + logger = mock(Logger.class); + lenient().when(logger.isInfoEnabled()).thenReturn(true); + lenient().when(logger.isWarnEnabled()).thenReturn(true); + plainLogger = new PlainExecutionEventLogger(messageBuilderFactory, logger); + } + + @AfterEach + void afterEach() { + mockitoSession.finishMocking(); + } + + @Test + void testProjectStartedSuppressed() { + // In plain mode, projectStarted should produce NO output + ExecutionEvent event = mock(ExecutionEvent.class); + + plainLogger.projectStarted(event); + + // Verify no logger calls were made + verify(logger, never()).info(anyString()); + } + + @Test + void testMojoStartedSuppressed() { + // In plain mode, mojoStarted should produce NO output + ExecutionEvent event = mock(ExecutionEvent.class); + + plainLogger.mojoStarted(event); + + verify(logger, never()).info(anyString()); + } + + @Test + void testSingleProjectSucceeded() { + MavenProject project = generateMavenProject("Maven Core"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project, 2100)); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project)); + when(session.getAllProjects()).thenReturn(List.of(project)); + when(session.getResult()).thenReturn(executionResult); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + ExecutionEvent projectEvent = mock(ExecutionEvent.class); + when(projectEvent.getProject()).thenReturn(project); + when(projectEvent.getSession()).thenReturn(session); + + plainLogger.sessionStarted(sessionEvent); + plainLogger.projectSucceeded(projectEvent); + + // Single project: no progress counter + verify(logger).info(matches("Maven Core.*SUCCESS.*2\\.1")); + } + + @Test + void testMultiModuleProjectOneLinePerModule() { + MavenProject project1 = generateMavenProject("API"); + MavenProject project2 = generateMavenProject("Core"); + MavenProject project3 = generateMavenProject("CLI"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project1, 1000)); + executionResult.addBuildSummary(new BuildSuccess(project2, 3000)); + executionResult.addBuildSummary(new BuildSuccess(project3, 2000)); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getResult()).thenReturn(executionResult); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + plainLogger.sessionStarted(sessionEvent); + + // Simulate lifecycle + ExecutionEvent event1 = mockProjectEvent(project1, session); + ExecutionEvent event2 = mockProjectEvent(project2, session); + ExecutionEvent event3 = mockProjectEvent(project3, session); + + plainLogger.projectSucceeded(event1); + plainLogger.projectSucceeded(event2); + plainLogger.projectSucceeded(event3); + + // Multi-module: each line has progress counter [n/total] + InOrder inOrder = inOrder(logger); + inOrder.verify(logger).info(matches("API.*\\[1/3\\].*SUCCESS.*1\\.0")); + inOrder.verify(logger).info(matches("Core.*\\[2/3\\].*SUCCESS.*3\\.0")); + inOrder.verify(logger).info(matches("CLI.*\\[3/3\\].*SUCCESS.*2\\.0")); + } + + @Test + void testProjectFailedShowsFailure() { + MavenProject project = generateMavenProject("Core"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildFailure(project, 5000, new Exception("Compile error"))); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project)); + when(session.getAllProjects()).thenReturn(List.of(project)); + when(session.getResult()).thenReturn(executionResult); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + plainLogger.sessionStarted(sessionEvent); + + ExecutionEvent event = mockProjectEvent(project, session); + plainLogger.projectFailed(event); + + verify(logger).info(matches("Core.*FAILURE.*5\\.0")); + } + + @Test + void testSessionEndedShowsSummary() { + MavenProject project1 = generateMavenProject("API"); + MavenProject project2 = generateMavenProject("Core"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project1, 1000)); + executionResult.addBuildSummary(new BuildSuccess(project2, 2000)); + + MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); + + MavenSession session = mock(MavenSession.class); + when(session.getResult()).thenReturn(executionResult); + when(session.getRequest()).thenReturn(executionRequest); + when(session.getProjects()).thenReturn(List.of(project1, project2)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2)); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + plainLogger.sessionStarted(sessionEvent); + plainLogger.sessionEnded(sessionEvent); + + // Verify BUILD SUCCESS and stats + InOrder inOrder = inOrder(logger); + inOrder.verify(logger).info(""); + inOrder.verify(logger).info("BUILD SUCCESS"); + inOrder.verify(logger).info(matches("2 modules.*2 passed")); + inOrder.verify(logger).info(eq("Total time: {}{}"), anyString(), anyString()); + inOrder.verify(logger).info("Full report: target/build-reports/build-report-latest.json"); + } + + @Test + void testSessionEndedWithFailures() { + MavenProject project1 = generateMavenProject("API"); + MavenProject project2 = generateMavenProject("Core"); + MavenProject project3 = generateMavenProject("CLI"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project1, 1000)); + executionResult.addBuildSummary(new BuildFailure(project2, 2000, new Exception("Error"))); + executionResult.addException(new Exception("Error")); + + MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); + + MavenSession session = mock(MavenSession.class); + when(session.getResult()).thenReturn(executionResult); + when(session.getRequest()).thenReturn(executionRequest); + when(session.getProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2, project3)); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + plainLogger.sessionStarted(sessionEvent); + plainLogger.sessionEnded(sessionEvent); + + InOrder inOrder = inOrder(logger); + inOrder.verify(logger).info(""); + inOrder.verify(logger).info("BUILD FAILURE"); + inOrder.verify(logger).info(matches("3 modules.*1 passed.*1 failed.*1 skipped")); + } + + @Test + void testMojoSkippedStillWarns() { + ExecutionEvent event = mock(ExecutionEvent.class); + var mojoExec = mock(org.apache.maven.plugin.MojoExecution.class); + when(mojoExec.getGoal()).thenReturn("deploy"); + when(event.getMojoExecution()).thenReturn(mojoExec); + + plainLogger.mojoSkipped(event); + + verify(logger).warn(anyString(), eq("deploy")); + } + + @Test + void testResumeFromProgress() { + // When resuming, allProjects > projects (some already built) + MavenProject project1 = generateMavenProject("API"); + MavenProject project2 = generateMavenProject("Core"); + MavenProject project3 = generateMavenProject("CLI"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project2, 1000)); + executionResult.addBuildSummary(new BuildSuccess(project3, 2000)); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project2, project3)); // resumed from project2 + when(session.getAllProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getResult()).thenReturn(executionResult); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + plainLogger.sessionStarted(sessionEvent); + + ExecutionEvent event2 = mockProjectEvent(project2, session); + ExecutionEvent event3 = mockProjectEvent(project3, session); + + plainLogger.projectSucceeded(event2); + plainLogger.projectSucceeded(event3); + + // Progress should start from 2/3, not 1/3 + InOrder inOrder = inOrder(logger); + inOrder.verify(logger).info(matches("Core.*\\[2/3\\].*SUCCESS")); + inOrder.verify(logger).info(matches("CLI.*\\[3/3\\].*SUCCESS")); + } + + // ---- Helpers ---- + + private static MavenProject generateMavenProject(String projectName) { + MavenProject project = mock(MavenProject.class); + lenient().when(project.getPackaging()).thenReturn("jar"); + lenient().when(project.getVersion()).thenReturn("4.1.0-SNAPSHOT"); + lenient().when(project.getName()).thenReturn(projectName); + return project; + } + + private static ExecutionEvent mockProjectEvent(MavenProject project, MavenSession session) { + ExecutionEvent event = mock(ExecutionEvent.class); + when(event.getProject()).thenReturn(project); + when(event.getSession()).thenReturn(session); + return event; + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/event/RichBuildEventListenerTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/RichBuildEventListenerTest.java new file mode 100644 index 000000000000..8fc9688cac23 --- /dev/null +++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/RichBuildEventListenerTest.java @@ -0,0 +1,332 @@ +/* + * 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.cling.event; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.util.List; + +import org.apache.maven.api.MonotonicClock; +import org.apache.maven.api.build.report.LogEvent; +import org.apache.maven.api.build.report.LogLevel; +import org.apache.maven.execution.ExecutionEvent; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.internal.build.DefaultLogEvent; +import org.apache.maven.project.MavenProject; +import org.eclipse.aether.transfer.TransferEvent; +import org.eclipse.aether.transfer.TransferResource; +import org.jline.terminal.Size; +import org.jline.terminal.Terminal; +import org.jline.terminal.impl.DumbTerminal; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.mockito.MockitoSession; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link RichBuildEventListener}. + */ +class RichBuildEventListenerTest { + + private MockitoSession mockitoSession; + private Terminal terminal; + private ByteArrayOutputStream terminalOutput; + private RichBuildEventListener listener; + + @BeforeEach + void beforeEach() throws Exception { + mockitoSession = Mockito.mockitoSession().startMocking(); + terminalOutput = new ByteArrayOutputStream(); + // DumbTerminal: supported=false (fallback mode), output goes to terminalOutput + terminal = new DumbTerminal(new ByteArrayInputStream(new byte[0]), terminalOutput); + terminal.setSize(new Size(120, 40)); + listener = new RichBuildEventListener(terminal, msg -> {}); + } + + @AfterEach + void afterEach() throws Exception { + terminal.close(); + mockitoSession.finishMocking(); + } + + @Test + void testProjectStartedAndFinished() { + MavenSession session = + createSession(List.of(createProject("api"), createProject("core"), createProject("cli"))); + + listener.initReactor(session); + + listener.projectStarted("api"); + listener.projectFinished("api"); + } + + @Test + void testLogMessagePassthrough() { + listener.log("Test log message"); + String output = terminalOutput.toString(); + assertTrue(output.contains("Test log message"), "Expected log message in output: " + output); + } + + @Test + void testProjectLogMessageFiltersInfo() { + LogEvent infoEvent = new DefaultLogEvent( + MonotonicClock.now(), + LogLevel.INFO, + "Compiling 42 source files", + "compiler", + null, + "[INFO] Compiling 42 source files"); + listener.projectLogMessage("api", infoEvent); + String output = terminalOutput.toString(); + assertFalse( + output.contains("Compiling 42 source files"), + "INFO messages should be suppressed in rich mode: " + output); + } + + @Test + void testProjectLogMessageSuppressesWarningInline() { + // In rich mode, warnings are suppressed inline and only counted for the + // end-of-build summary — they don't scroll above the status bar. + LogEvent warnEvent = new DefaultLogEvent( + MonotonicClock.now(), + LogLevel.WARN, + "Deprecated API usage", + "compiler", + null, + "[WARNING] Deprecated API usage"); + listener.projectLogMessage("api", warnEvent); + String output = terminalOutput.toString(); + assertFalse( + output.contains("[WARNING] Deprecated API usage"), + "WARN messages should be suppressed inline in rich mode: " + output); + assertEquals(1, listener.getWarningCount(), "Warning count should be tracked"); + } + + @Test + void testProjectLogMessageShowsError() { + LogEvent errorEvent = new DefaultLogEvent( + MonotonicClock.now(), + LogLevel.ERROR, + "Compilation failure", + "compiler", + null, + "[ERROR] Compilation failure"); + listener.projectLogMessage("api", errorEvent); + String output = terminalOutput.toString(); + assertTrue(output.contains("[ERROR] Compilation failure"), "ERROR messages should pass through: " + output); + } + + @Test + void testMojoStartedUpdatesState() { + MavenSession session = createSession(List.of(createProject("api"), createProject("core"))); + listener.initReactor(session); + + listener.projectStarted("api"); + + ExecutionEvent mojoEvent = mock(ExecutionEvent.class); + MavenProject project = createProject("api"); + when(mojoEvent.getProject()).thenReturn(project); + var mojoExec = mock(org.apache.maven.plugin.MojoExecution.class); + when(mojoExec.getArtifactId()).thenReturn("maven-compiler-plugin"); + when(mojoExec.getGoal()).thenReturn("compile"); + when(mojoEvent.getMojoExecution()).thenReturn(mojoExec); + + listener.mojoStarted(mojoEvent); + + listener.projectFinished("api"); + } + + @Test + void testTransferEvents() { + MavenSession session = createSession(List.of(createProject("api"))); + listener.initReactor(session); + + TransferResource resource = mock(TransferResource.class); + when(resource.getResourceName()).thenReturn("org/apache/maven/core/4.1.0/core-4.1.0.jar"); + when(resource.getContentLength()).thenReturn(524288L); + + TransferEvent startEvent = mock(TransferEvent.class); + when(startEvent.getType()).thenReturn(TransferEvent.EventType.STARTED); + when(startEvent.getResource()).thenReturn(resource); + + listener.transfer("api", startEvent); + + TransferEvent progressEvent = mock(TransferEvent.class); + when(progressEvent.getType()).thenReturn(TransferEvent.EventType.PROGRESSED); + when(progressEvent.getResource()).thenReturn(resource); + when(progressEvent.getTransferredBytes()).thenReturn(262144L); + + listener.transfer("api", progressEvent); + + TransferEvent doneEvent = mock(TransferEvent.class); + when(doneEvent.getType()).thenReturn(TransferEvent.EventType.SUCCEEDED); + when(doneEvent.getResource()).thenReturn(resource); + + listener.transfer("api", doneEvent); + } + + @Test + void testParallelProjects() { + MavenSession session = + createSession(List.of(createProject("api"), createProject("core"), createProject("cli"))); + listener.initReactor(session); + + listener.projectStarted("api"); + listener.projectStarted("core"); + + listener.projectFinished("api"); + + listener.projectStarted("cli"); + + listener.projectFinished("core"); + listener.projectFinished("cli"); + } + + @Test + void testExecutionFailureUpdatesState() { + MavenSession session = createSession(List.of(createProject("api"))); + listener.initReactor(session); + + listener.projectStarted("api"); + listener.executionFailure("api", true, "Compilation error"); + listener.projectFinished("api"); + } + + @Test + void testTearDown() throws Exception { + MavenSession session = createSession(List.of(createProject("api"))); + listener.initReactor(session); + listener.projectStarted("api"); + + listener.tearDown(); + } + + @Test + void testFinishCallsTearDown() throws Exception { + MavenSession session = createSession(List.of(createProject("api"))); + listener.initReactor(session); + + listener.finish(0); + } + + @Test + void testFailCallsTearDown() throws Exception { + MavenSession session = createSession(List.of(createProject("api"))); + listener.initReactor(session); + + listener.fail(new RuntimeException("build error")); + } + + @Test + void testLargeReactorProgressBar() { + // With 50 modules on a 120-char terminal, maxIndicators = (120-40)/3 = 26. + // 50 > 26, so the summary line should use a progress bar instead of + // per-module ✓/●/○ indicators. + List projects = new java.util.ArrayList<>(); + for (int i = 0; i < 50; i++) { + projects.add(createProject("module-" + i)); + } + MavenSession session = createSession(projects); + listener.initReactor(session); + + // Start and finish some modules to exercise the progress bar rendering + for (int i = 0; i < 10; i++) { + listener.projectStarted("module-" + i); + } + for (int i = 0; i < 5; i++) { + listener.projectFinished("module-" + i); + } + // 5 completed, 5 active, 40 pending — should render without error + for (int i = 5; i < 10; i++) { + listener.projectFinished("module-" + i); + } + } + + @Test + void testSmallReactorUsesIndicators() { + // With 3 modules on a 120-char terminal, maxIndicators = 26 > 3, + // so the summary line should use per-module ✓/●/○ indicators. + MavenSession session = + createSession(List.of(createProject("api"), createProject("core"), createProject("cli"))); + listener.initReactor(session); + + listener.projectStarted("api"); + listener.projectFinished("api"); + listener.projectStarted("core"); + listener.projectFinished("core"); + listener.projectStarted("cli"); + listener.projectFinished("cli"); + } + + @Test + void testTruncateAnsiPlainText() { + String truncated = RichBuildEventListener.truncateAnsi("hello world", 5); + // When truncated, a RESET escape is appended to close any open styling + assertTrue(truncated.startsWith("hello"), "Should start with 'hello': " + truncated); + // Should not contain characters beyond "hello" (except ANSI reset) + String stripped = truncated.replaceAll("\033\\[[^a-zA-Z]*[a-zA-Z]", ""); + assertEquals("hello", stripped); + } + + @Test + void testTruncateAnsiPreservesEscapeSequences() { + // ANSI color codes should not count toward visible length + String colored = "\033[1mhello\033[0m world"; + String truncated = RichBuildEventListener.truncateAnsi(colored, 5); + // Should keep "hello" (5 visible chars) with the bold prefix + assertTrue(truncated.contains("hello"), "Truncated should contain 'hello': " + truncated); + assertTrue(truncated.contains("\033[1m"), "Truncated should preserve ANSI prefix"); + } + + @Test + void testTruncateAnsiNoTruncationNeeded() { + String s = "short"; + assertEquals(s, RichBuildEventListener.truncateAnsi(s, 100)); + } + + // ---- Helpers ---- + + private static MavenProject createProject(String artifactId) { + MavenProject project = mock(MavenProject.class); + lenient().when(project.getArtifactId()).thenReturn(artifactId); + lenient().when(project.getName()).thenReturn(artifactId); + lenient().when(project.getPackaging()).thenReturn("jar"); + lenient().when(project.getVersion()).thenReturn("4.1.0-SNAPSHOT"); + return project; + } + + private static MavenSession createSession(List projects) { + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(projects); + when(session.getAllProjects()).thenReturn(projects); + MavenExecutionRequest request = mock(MavenExecutionRequest.class); + lenient().when(request.getDegreeOfConcurrency()).thenReturn(1); + lenient().when(session.getRequest()).thenReturn(request); + return session; + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/event/RichExecutionEventLoggerTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/RichExecutionEventLoggerTest.java new file mode 100644 index 000000000000..fe1572903ae8 --- /dev/null +++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/event/RichExecutionEventLoggerTest.java @@ -0,0 +1,424 @@ +/* + * 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.cling.event; + +import java.io.ByteArrayOutputStream; +import java.time.Instant; +import java.util.List; + +import org.apache.maven.api.build.report.LogLevel; +import org.apache.maven.execution.BuildFailure; +import org.apache.maven.execution.BuildSuccess; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.ExecutionEvent; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionResult; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.internal.build.DefaultLogEvent; +import org.apache.maven.jline.JLineMessageBuilderFactory; +import org.apache.maven.jline.MessageUtils; +import org.apache.maven.project.MavenProject; +import org.jline.terminal.Size; +import org.jline.terminal.Terminal; +import org.jline.terminal.impl.DumbTerminal; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.mockito.MockitoSession; +import org.slf4j.Logger; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link RichExecutionEventLogger}. + *

+ * In rich mode, all output goes directly to the terminal writer via + * {@link RichBuildEventListener#log(String)}, bypassing SLF4J entirely. + * Tests capture the terminal output to verify content. + */ +class RichExecutionEventLoggerTest { + + private MockitoSession mockitoSession; + private Logger logger; + private Terminal terminal; + private ByteArrayOutputStream terminalOutput; + private RichBuildEventListener buildEventListener; + private RichExecutionEventLogger richLogger; + private final JLineMessageBuilderFactory messageBuilderFactory = new JLineMessageBuilderFactory(); + + @BeforeAll + static void setUp() { + MessageUtils.setColorEnabled(false); + } + + @AfterAll + static void tearDown() { + MessageUtils.setColorEnabled(true); + } + + @BeforeEach + void beforeEach() throws Exception { + mockitoSession = Mockito.mockitoSession().startMocking(); + logger = mock(Logger.class); + lenient().when(logger.isInfoEnabled()).thenReturn(true); + lenient().when(logger.isWarnEnabled()).thenReturn(true); + terminalOutput = new ByteArrayOutputStream(); + terminal = new DumbTerminal(System.in, terminalOutput); + terminal.setSize(new Size(120, 40)); + buildEventListener = new RichBuildEventListener(terminal, msg -> {}); + richLogger = new RichExecutionEventLogger(messageBuilderFactory, buildEventListener, logger); + } + + @AfterEach + void afterEach() throws Exception { + terminal.close(); + mockitoSession.finishMocking(); + } + + @Test + void testProjectStartedSuppressed() { + // In rich mode, projectStarted should produce NO output (status bar handles it) + ExecutionEvent event = mock(ExecutionEvent.class); + + richLogger.projectStarted(event); + + verify(logger, never()).info(anyString()); + assertTrue(terminalOutput.toString().isEmpty(), "No terminal output expected"); + } + + @Test + void testMojoStartedSuppressed() { + // In rich mode, mojoStarted should produce NO output (status bar handles it) + ExecutionEvent event = mock(ExecutionEvent.class); + + richLogger.mojoStarted(event); + + verify(logger, never()).info(anyString()); + assertTrue(terminalOutput.toString().isEmpty(), "No terminal output expected"); + } + + @Test + void testProjectSucceededSuppressed() { + // In rich mode, projectSucceeded produces NO scrolling output — + // the status bar checkmarks already indicate completion. + MavenProject project = generateMavenProject("Maven Core"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project, 2100)); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project)); + when(session.getAllProjects()).thenReturn(List.of(project)); + lenient().when(session.getResult()).thenReturn(executionResult); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + richLogger.sessionStarted(sessionEvent); + + // Clear terminal output accumulated from sessionStarted (status bar init) + terminalOutput.reset(); + + ExecutionEvent projectEvent = mock(ExecutionEvent.class); + lenient().when(projectEvent.getProject()).thenReturn(project); + lenient().when(projectEvent.getSession()).thenReturn(session); + + richLogger.projectSucceeded(projectEvent); + + // No output — SUCCESS lines are suppressed in rich mode + String output = terminalOutput.toString(); + assertFalse(output.contains("SUCCESS"), "SUCCESS line should be suppressed in rich mode"); + } + + @Test + void testProjectFailedShowsCross() { + MavenProject project = generateMavenProject("Core"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildFailure(project, 5000, new Exception("Compile error"))); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project)); + when(session.getAllProjects()).thenReturn(List.of(project)); + when(session.getResult()).thenReturn(executionResult); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + richLogger.sessionStarted(sessionEvent); + + ExecutionEvent event = mockProjectEvent(project, session); + richLogger.projectFailed(event); + + String output = terminalOutput.toString(); + assertTrue(output.contains("Core"), "Should contain project name"); + assertTrue(output.contains("FAILURE"), "Should contain FAILURE status"); + } + + @Test + void testMultiModuleSuccessSuppressed() { + // In rich mode, per-module SUCCESS lines are suppressed — the status bar + // already shows ✓/●/○ indicators and the [n/total] counter. + MavenProject project1 = generateMavenProject("API"); + MavenProject project2 = generateMavenProject("Core"); + MavenProject project3 = generateMavenProject("CLI"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project1, 1000)); + executionResult.addBuildSummary(new BuildSuccess(project2, 3000)); + executionResult.addBuildSummary(new BuildSuccess(project3, 2000)); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2, project3)); + lenient().when(session.getResult()).thenReturn(executionResult); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + richLogger.sessionStarted(sessionEvent); + + // Clear terminal output from sessionStarted + terminalOutput.reset(); + + // mockProjectEvent stubs are unused since projectSucceeded is a no-op; + // call with a plain mock to avoid UnnecessaryStubbing errors. + richLogger.projectSucceeded(mock(ExecutionEvent.class)); + richLogger.projectSucceeded(mock(ExecutionEvent.class)); + richLogger.projectSucceeded(mock(ExecutionEvent.class)); + + String output = terminalOutput.toString(); + // No per-module SUCCESS lines should appear + assertFalse(output.contains("SUCCESS"), "SUCCESS lines should be suppressed in rich mode"); + assertFalse(output.contains("[1/3]"), "Progress counters should not appear"); + } + + @Test + void testSessionEndedShowsSummary() { + MavenProject project1 = generateMavenProject("API"); + MavenProject project2 = generateMavenProject("Core"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project1, 1000)); + executionResult.addBuildSummary(new BuildSuccess(project2, 2000)); + + MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); + + MavenSession session = mock(MavenSession.class); + when(session.getResult()).thenReturn(executionResult); + when(session.getRequest()).thenReturn(executionRequest); + when(session.getProjects()).thenReturn(List.of(project1, project2)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2)); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + richLogger.sessionStarted(sessionEvent); + richLogger.sessionEnded(sessionEvent); + + // Summary goes to terminal writer, not logger + String output = terminalOutput.toString(); + assertTrue(output.contains("BUILD SUCCESS"), "Should contain BUILD SUCCESS"); + assertTrue(output.contains("2 modules"), "Should contain module count"); + assertTrue(output.contains("2 passed"), "Should contain passed count"); + assertTrue(output.contains("Total time:"), "Should contain total time"); + assertTrue( + output.contains("Full report: target/build-reports/build-report-latest.json"), + "Should contain report path"); + // MNG-7372: version info only on failure, not success + assertFalse(output.contains("Maven:"), "Should NOT contain Maven version on success"); + } + + @Test + void testSessionEndedWithFailures() { + MavenProject project1 = generateMavenProject("API"); + MavenProject project2 = generateMavenProject("Core"); + MavenProject project3 = generateMavenProject("CLI"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project1, 1000)); + executionResult.addBuildSummary(new BuildFailure(project2, 2000, new Exception("Error"))); + executionResult.addException(new Exception("Error")); + + MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); + + MavenSession session = mock(MavenSession.class); + when(session.getResult()).thenReturn(executionResult); + when(session.getRequest()).thenReturn(executionRequest); + when(session.getProjects()).thenReturn(List.of(project1, project2, project3)); + when(session.getAllProjects()).thenReturn(List.of(project1, project2, project3)); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + richLogger.sessionStarted(sessionEvent); + richLogger.sessionEnded(sessionEvent); + + String output = terminalOutput.toString(); + assertTrue(output.contains("BUILD FAILURE"), "Should contain BUILD FAILURE"); + assertTrue(output.contains("3 modules"), "Should contain module count"); + assertTrue(output.contains("1 passed"), "Should contain passed count"); + assertTrue(output.contains("1 failed"), "Should contain failed count"); + assertTrue(output.contains("1 skipped"), "Should contain skipped count"); + // MNG-7372: version info shown on failure + assertTrue(output.contains("Maven:"), "Should contain Maven version on failure"); + assertTrue(output.contains("Java:"), "Should contain Java version on failure"); + } + + @Test + void testMojoSkippedStillWarns() { + // mojoSkipped still uses logger.warn (goes through SLF4J for WARN level) + ExecutionEvent event = mock(ExecutionEvent.class); + var mojoExec = mock(org.apache.maven.plugin.MojoExecution.class); + when(mojoExec.getGoal()).thenReturn("deploy"); + when(event.getMojoExecution()).thenReturn(mojoExec); + + richLogger.mojoSkipped(event); + + verify(logger).warn(anyString(), eq("deploy")); + } + + @Test + void testOutputBypassesSLF4J() { + // Verify that summary output does NOT go through logger.info + MavenProject project = generateMavenProject("Core"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project, 1000)); + + MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project)); + when(session.getAllProjects()).thenReturn(List.of(project)); + when(session.getResult()).thenReturn(executionResult); + when(session.getRequest()).thenReturn(executionRequest); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + richLogger.sessionStarted(sessionEvent); + richLogger.sessionEnded(sessionEvent); + + // No logger.info calls — all output goes through terminal writer + verify(logger, never()).info(anyString()); + + // But the output IS in the terminal + String output = terminalOutput.toString(); + assertFalse(output.isEmpty(), "Terminal should have output"); + assertTrue(output.contains("BUILD SUCCESS"), "Terminal should contain BUILD SUCCESS"); + } + + @Test + void testWarningSummaryShown() { + MavenProject project = generateMavenProject("Core"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project, 1000)); + + MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project)); + when(session.getAllProjects()).thenReturn(List.of(project)); + when(session.getResult()).thenReturn(executionResult); + when(session.getRequest()).thenReturn(executionRequest); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + richLogger.sessionStarted(sessionEvent); + + // Simulate 3 warnings arriving during the build + Instant now = Instant.now(); + buildEventListener.projectLogMessage( + "core", + new DefaultLogEvent(now, LogLevel.WARN, "unchecked cast", "javac", null, "[WARNING] unchecked cast")); + buildEventListener.projectLogMessage( + "core", + new DefaultLogEvent(now, LogLevel.WARN, "deprecated API", "javac", null, "[WARNING] deprecated API")); + buildEventListener.projectLogMessage( + "core", + new DefaultLogEvent(now, LogLevel.WARN, "unused import", "javac", null, "[WARNING] unused import")); + + richLogger.sessionEnded(sessionEvent); + + String output = terminalOutput.toString(); + assertTrue(output.contains("BUILD SUCCESS"), "Should contain BUILD SUCCESS"); + assertTrue(output.contains("3 warning"), "Should contain warning count"); + assertTrue(output.contains("Diagnostics:"), "Should contain Diagnostics label"); + assertTrue(output.contains("mvnlog"), "Should hint how to see warning details"); + } + + @Test + void testNoWarningSummaryWhenClean() { + MavenProject project = generateMavenProject("Core"); + + MavenExecutionResult executionResult = new DefaultMavenExecutionResult(); + executionResult.addBuildSummary(new BuildSuccess(project, 1000)); + + MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); + + MavenSession session = mock(MavenSession.class); + when(session.getProjects()).thenReturn(List.of(project)); + when(session.getAllProjects()).thenReturn(List.of(project)); + when(session.getResult()).thenReturn(executionResult); + when(session.getRequest()).thenReturn(executionRequest); + + ExecutionEvent sessionEvent = mock(ExecutionEvent.class); + when(sessionEvent.getSession()).thenReturn(session); + + richLogger.sessionStarted(sessionEvent); + richLogger.sessionEnded(sessionEvent); + + String output = terminalOutput.toString(); + assertTrue(output.contains("BUILD SUCCESS"), "Should contain BUILD SUCCESS"); + assertFalse(output.contains("Diagnostics:"), "Should NOT contain Diagnostics when no warnings"); + } + + // ---- Helpers ---- + + private static MavenProject generateMavenProject(String projectName) { + MavenProject project = mock(MavenProject.class); + lenient() + .when(project.getArtifactId()) + .thenReturn(projectName.toLowerCase().replace(" ", "-")); + lenient().when(project.getPackaging()).thenReturn("jar"); + lenient().when(project.getVersion()).thenReturn("4.1.0-SNAPSHOT"); + lenient().when(project.getName()).thenReturn(projectName); + return project; + } + + private static ExecutionEvent mockProjectEvent(MavenProject project, MavenSession session) { + ExecutionEvent event = mock(ExecutionEvent.class); + when(event.getProject()).thenReturn(project); + when(event.getSession()).thenReturn(session); + return event; + } +} \ No newline at end of file diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java new file mode 100644 index 000000000000..908c9b7be4c8 --- /dev/null +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportCollector.java @@ -0,0 +1,741 @@ +/* + * 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.Inject; +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.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +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.LogLevel; +import org.apache.maven.api.build.report.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.services.BuilderProblem; +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. + *

+ * Registered as an {@link org.apache.maven.eventspy.EventSpy} via {@code @Named}/{@code @Singleton}, + * following the same pattern as {@code DefaultPluginValidationManager}. + *

+ * Thread-safe: concurrent module builds (with {@code -T}) each write to their + * own entry in a {@link ConcurrentHashMap}. + *

+ * 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; + + private final DefaultDiagnosticCollector diagnosticCollector; + + @Inject + public BuildReportCollector(DefaultDiagnosticCollector diagnosticCollector) { + this.diagnosticCollector = diagnosticCollector; + } + + /** + * No-arg constructor for tests that don't need diagnostic collection. + */ + BuildReportCollector() { + this(new DefaultDiagnosticCollector()); + } + + /** + * Returns the diagnostic collector used by this build report collector. + * Plugins can inject {@link DefaultDiagnosticCollector} directly, but this + * accessor is provided for internal use and testing. + */ + DefaultDiagnosticCollector getDiagnosticCollector() { + return diagnosticCollector; + } + + /** + * 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; + + // ---- Mutable state, populated during the build ---- + + /** Per-project mojo tracking: project key → list of in-flight/completed mojos. */ + private final Map> mojoTimings = new ConcurrentHashMap<>(); + + /** Per-project start instants for duration computation. */ + private final Map projectStartTimes = new ConcurrentHashMap<>(); + + /** Per-mojo start instants for duration computation. */ + private final Map mojoStartTimes = new ConcurrentHashMap<>(); + + /** Session-level state — set once on SessionStarted. */ + private volatile MavenSession session; + + // ---- 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 currentMojoByThread = new ConcurrentHashMap<>(); + + /** Per-mojo log buffers: mojo key → captured log events. */ + private final Map> 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 currentProjectByThread = new ConcurrentHashMap<>(); + + /** Per-module log buffers: project key → events captured outside any mojo. */ + private final Map> moduleLogBuffers = new ConcurrentHashMap<>(); + + /** Build-level log buffer: events captured outside any module lifecycle. */ + private final List 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; + default: + break; + } + } + } + + // ---- Event handlers ---- + + private void onSessionStarted(ExecutionEvent event) { + this.session = event.getSession(); + configureDiagnosticSuppression(); + installLogCapture(); + } + + /** + * Reads the {@code maven.diagnostic.suppress} user property and configures + * the diagnostic collector to suppress matching keys. The property accepts + * a comma-separated list of keys or patterns: + *

    + *
  • Exact keys: {@code -Dmaven.diagnostic.suppress=deprecated-source-target}
  • + *
  • Prefix wildcards: {@code -Dmaven.diagnostic.suppress=auto:*} (suppresses all + * auto-collected warnings from Maven 3 plugins)
  • + *
  • Multiple: {@code -Dmaven.diagnostic.suppress=key1,key2,auto:*}
  • + *
+ */ + private void configureDiagnosticSuppression() { + if (session == null) { + return; + } + String suppressProp = session.getUserProperties().getProperty("maven.diagnostic.suppress"); + if (suppressProp == null || suppressProp.isBlank()) { + return; + } + Set keys = new LinkedHashSet<>(); + for (String token : suppressProp.split(",")) { + String trimmed = token.trim(); + if (!trimmed.isEmpty()) { + keys.add(trimmed); + } + } + if (!keys.isEmpty()) { + diagnosticCollector.setSuppressedKeys(keys); + LOGGER.debug("Diagnostic suppression configured: {}", keys); + } + } + + private void onSessionEnded(ExecutionEvent event) { + removeLogCapture(); + + MavenSession endSession = event.getSession(); + if (endSession == null) { + return; + } + + // Read warning mode from user properties (set by MavenInvoker from --warning-mode) + String warningMode = endSession.getUserProperties().getProperty("maven.build.warningMode", "summary"); + + BuildReport report = buildReport(endSession); + writeReport(report, endSession); + + if (!"none".equalsIgnoreCase(warningMode)) { + printDiagnosticSummary(); + } + + // --warning-mode=fail: fail the build if any warnings were collected + if ("fail".equalsIgnoreCase(warningMode) && diagnosticCollector.hasWarnings()) { + int warningCount = 0; + for (DefaultDiagnosticSummary entry : diagnosticCollector.getSummary()) { + if (entry.problem().getSeverity() == BuilderProblem.Severity.WARNING) { + warningCount += entry.count(); + } + } + endSession + .getResult() + .addException(new RuntimeException( + "Build has " + warningCount + " warning(s) and --warning-mode=fail is set")); + } + } + + private void onProjectStarted(ExecutionEvent event) { + String key = projectKey(event.getProject()); + projectStartTimes.put(key, MonotonicClock.now()); + 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()); + + // 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); + 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 logBuffer = mojoLogBuffers.remove(mKey); + List 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, + output); + + 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(); + + // Auto-collect WARN-level log events as build problems, giving Maven 3 plugins + // automatic deduplication and summary at end of build without code changes. + // Skip our own logger to avoid feedback loops from problem summary printing. + if (event.level() == LogLevel.WARN + && event.message() != null + && !event.loggerName().equals(BuildReportCollector.class.getName())) { + String syntheticKey = syntheticDiagnosticKey(event.loggerName(), event.message()); + diagnosticCollector.report(BuilderProblem.builder() + .source(event.loggerName()) + .message(event.message()) + .severity(BuilderProblem.Severity.WARNING) + .key(syntheticKey) + .build()); + } + + // 1. Mojo-level: event belongs to the currently-executing mojo on this thread + String mKey = currentMojoByThread.get(threadId); + if (mKey != null) { + List 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 buffer = moduleLogBuffers.get(pKey); + if (buffer != null && buffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { + buffer.add(event); + } + return; + } + + // 3. Build-level: no project active (startup, reactor summary, post-build) + if (buildLogBuffer.size() < MAX_LOG_EVENTS_PER_SCOPE) { + buildLogBuffer.add(event); + } + } + + // ---- Report assembly ---- + + BuildReport buildReport(MavenSession endSession) { + Instant now = MonotonicClock.now(); + Instant startInstant = endSession.getRequest().getStartInstant(); + Duration totalDuration = Duration.between(startInstant, now); + + MavenExecutionResult result = endSession.getResult(); + boolean hasFailures = result != null && result.hasExceptions(); + BuildStatus overallStatus = hasFailures ? BuildStatus.FAILURE : BuildStatus.SUCCESS; + + // Collect module reports + List moduleReports = new ArrayList<>(); + for (MavenProject project : endSession.getProjects()) { + moduleReports.add(buildModuleReport(project, endSession)); + } + + // Collect failures + List failureReports = new ArrayList<>(); + if (result != null) { + for (MavenProject project : endSession.getProjects()) { + BuildSummary summary = result.getBuildSummary(project); + if (summary instanceof BuildFailure buildFailure) { + failureReports.add(buildFailureReport(project, buildFailure)); + } + } + } + + // Metadata + String mavenVersion = endSession.getSystemProperties().getProperty("maven.version", "unknown"); + String javaVersion = System.getProperty("java.version", "unknown"); + List goals = endSession.getGoals(); + MavenProject topProject = endSession.getTopLevelProject(); + String projectId = topProject != null + ? topProject.getGroupId() + ":" + topProject.getArtifactId() + ":" + topProject.getVersion() + : "unknown"; + boolean multiModule = endSession.getProjects().size() > 1; + int threads = endSession.getRequest().getDegreeOfConcurrency(); + + // Problems (deduplicated) + List problems = diagnosticCollector.getProblems(); + + // Build-level log events (outside any module lifecycle) + List buildOutput = List.copyOf(buildLogBuffer); + + return new DefaultBuildReport( + overallStatus, + totalDuration, + startInstant, + mavenVersion, + javaVersion, + goals, + projectId, + multiModule, + threads, + moduleReports, + failureReports, + problems, + buildOutput); + } + + private ModuleReport buildModuleReport(MavenProject project, MavenSession endSession) { + String key = projectKey(project); + + // Duration from BuildSummary (preferred) or fallback to our own tracking + MavenExecutionResult result = endSession.getResult(); + Duration duration = Duration.ZERO; + BuildStatus status = BuildStatus.SKIPPED; + Instant moduleStartTime = + projectStartTimes.getOrDefault(key, endSession.getRequest().getStartInstant()); + + if (result != null) { + BuildSummary summary = result.getBuildSummary(project); + if (summary instanceof BuildSuccess) { + status = BuildStatus.SUCCESS; + duration = summary.getExecTime(); + } else if (summary instanceof BuildFailure) { + status = BuildStatus.FAILURE; + duration = summary.getExecTime(); + } else if (summary != null) { + // Unknown summary type — use its timing + duration = summary.getExecTime(); + } else { + // No summary means skipped + Instant start = projectStartTimes.get(key); + if (start != null) { + duration = Duration.between(start, MonotonicClock.now()); + } + } + } + + // Mojo reports + List timings = mojoTimings.getOrDefault(key, Collections.emptyList()); + List mojoReports; + synchronized (timings) { + mojoReports = timings.stream() + .map(t -> (MojoReport) new DefaultMojoReport( + t.groupId, + t.artifactId, + t.version, + t.goal, + t.executionId, + t.phase, + t.status, + t.startTime, + t.duration, + t.output)) + .toList(); + } + + // Module-level log events (between mojos) + List moduleLogBuffer = moduleLogBuffers.getOrDefault(key, Collections.emptyList()); + List moduleOutput; + synchronized (moduleLogBuffer) { + moduleOutput = List.copyOf(moduleLogBuffer); + } + + return new DefaultModuleReport( + project.getGroupId(), + project.getArtifactId(), + project.getVersion(), + status, + moduleStartTime, + duration, + mojoReports, + moduleOutput); + } + + private FailureReport buildFailureReport(MavenProject project, BuildFailure buildFailure) { + String module = project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion(); + + // Try to find which mojo failed + String mojoId = null; + List timings = mojoTimings.getOrDefault(projectKey(project), Collections.emptyList()); + synchronized (timings) { + for (MojoTiming t : timings) { + if (t.status == BuildStatus.FAILURE) { + mojoId = t.artifactId + ":" + t.version + ":" + t.goal; + break; + } + } + } + + Throwable cause = buildFailure.getCause(); + String message = cause != null ? cause.getMessage() : "Unknown error"; + String stackTrace = cause != null ? truncateStackTrace(cause) : null; + + Instant failureTimestamp = MonotonicClock.now(); + String exceptionType = cause != null ? cause.getClass().getSimpleName() : null; + + return new DefaultFailureReport( + module, + mojoId, + failureTimestamp, + exceptionType, + message != null ? message : "Unknown error", + stackTrace); + } + + // ---- JSON persistence ---- + + void writeReport(BuildReport report, MavenSession endSession) { + Path topDirectory = endSession.getTopDirectory(); + if (topDirectory == null) { + LOGGER.debug("No top directory available, skipping build report"); + return; + } + + Path reportsDir = topDirectory.resolve("target").resolve(REPORT_DIR); + + try { + Files.createDirectories(reportsDir); + String json = BuildReportJsonWriter.toJson(report); + + // Timestamped file: build-report-20250729T143000Z.json + String timestamp = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'") + .withZone(ZoneOffset.UTC) + .format(report.startTime()); + Path timestampedFile = reportsDir.resolve("build-report-" + timestamp + ".json"); + + // Write to a temp file, then atomic-move into place so a crash + // never leaves a half-written report on disk. + Path tmpFile = Files.createTempFile(reportsDir, ".build-report-", ".tmp"); + try { + Files.writeString(tmpFile, json); + atomicMove(tmpFile, timestampedFile); + } catch (IOException e) { + Files.deleteIfExists(tmpFile); + throw e; + } + + // Latest symlink (or copy on filesystems that don't support symlinks) + Path latestFile = reportsDir.resolve(REPORT_LATEST); + try { + // Atomic symlink swap: create new link, then rename over the old one + Path tmpLink = Files.createTempFile(reportsDir, ".latest-", ".tmp"); + Files.delete(tmpLink); // createTempFile creates a regular file + Files.createSymbolicLink(tmpLink, timestampedFile.getFileName()); + atomicMove(tmpLink, latestFile); + } catch (UnsupportedOperationException | IOException symEx) { + // Windows or restricted filesystem — fall back to a plain copy + Files.writeString(latestFile, json); + } + + LOGGER.debug("Build report written to {}", timestampedFile); + } catch (IOException e) { + LOGGER.warn("Failed to write build report to {}: {}", reportsDir, e.getMessage()); + } + } + + /** + * Attempts an atomic move; falls back to a plain move if the filesystem + * does not support {@code ATOMIC_MOVE}. + */ + private static void atomicMove(Path source, Path target) throws IOException { + try { + Files.move(source, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); + } catch (AtomicMoveNotSupportedException e) { + Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); + } + } + + // ---- Diagnostic summary ---- + + /** + * Prints a deduplicated summary of diagnostics (warnings and errors) at the + * end of the build. This ensures important messages are not lost in scrollback. + */ + void printDiagnosticSummary() { + List summary = diagnosticCollector.getSummary(); + if (summary.isEmpty()) { + return; + } + + // Count unique warnings and total occurrences + int uniqueWarnings = 0; + int totalOccurrences = 0; + int uniqueErrors = 0; + for (DefaultDiagnosticSummary entry : summary) { + BuilderProblem.Severity sev = entry.problem().getSeverity(); + if (sev == BuilderProblem.Severity.WARNING) { + uniqueWarnings++; + totalOccurrences += entry.count(); + } else if (sev == BuilderProblem.Severity.ERROR) { + uniqueErrors++; + totalOccurrences += entry.count(); + } + } + + if (uniqueWarnings == 0 && uniqueErrors == 0) { + return; + } + + // Print header + StringBuilder header = new StringBuilder(); + if (uniqueWarnings > 0) { + header.append(uniqueWarnings).append(" warning"); + if (uniqueWarnings > 1) { + header.append('s'); + } + } + if (uniqueErrors > 0) { + if (header.length() > 0) { + header.append(", "); + } + header.append(uniqueErrors).append(" error"); + if (uniqueErrors > 1) { + header.append('s'); + } + } + if (totalOccurrences > (uniqueWarnings + uniqueErrors)) { + header.append(" (").append(totalOccurrences).append(" total occurrences)"); + } + + // Print the summary at INFO level. We intentionally do NOT re-print individual + // warning messages here — they were already logged inline at WARN level. Re-printing + // the raw message text would double warning counts in log parsers, trigger + // --fail-on-severity WARN again, and confuse tools that search for specific text. + // Full details are available in target/build-reports/. + LOGGER.info(""); + LOGGER.info("Diagnostics: {} — see target/{}/{} for details", header, REPORT_DIR, REPORT_LATEST); + } + + // ---- Utility methods ---- + + private static String projectKey(MavenProject project) { + return project.getGroupId() + ":" + project.getArtifactId(); + } + + private static String mojoKey(MavenProject project, MojoExecution mojo) { + return projectKey(project) + "#" + mojo.getGoal() + "@" + mojo.getExecutionId(); + } + + /** + * Generates a stable deduplication key for a warning intercepted from a + * Maven 3 plugin's {@code Log.warn()} call. The key is derived from the + * logger name and a normalized hash of the message text, so that the same + * warning from different modules or files deduplicates correctly. + *

+ * File-specific coordinates (paths, line numbers) are stripped before hashing + * so that "Foo.java:42: unchecked cast" and "Bar.java:99: unchecked cast" + * map to the same key. + */ + static String syntheticDiagnosticKey(String loggerName, String message) { + // Strip file coordinates for dedup: remove paths and line/column numbers + String normalized = message.replaceAll("\\S+\\.java:\\d+(:\\d+)?:?\\s*", "") + .replaceAll("\\S+[\\\\/][\\w.]+:\\d+", "") + .trim(); + // Use a short logger suffix to namespace the key + String loggerSuffix = loggerName; + int lastDot = loggerName.lastIndexOf('.'); + if (lastDot >= 0 && lastDot < loggerName.length() - 1) { + loggerSuffix = loggerName.substring(lastDot + 1); + } + return "auto:" + loggerSuffix + ":" + Integer.toHexString(normalized.hashCode()); + } + + static String truncateStackTrace(Throwable t) { + StringWriter sw = new StringWriter(); + t.printStackTrace(new PrintWriter(sw)); + String full = sw.toString(); + String[] lines = full.split("\n"); + if (lines.length <= MAX_STACKTRACE_LINES) { + return full; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < MAX_STACKTRACE_LINES; i++) { + sb.append(lines[i]).append('\n'); + } + sb.append("... ").append(lines.length - MAX_STACKTRACE_LINES).append(" more lines truncated\n"); + return sb.toString(); + } + + // ---- Internal records ---- + + record MojoTiming( + String groupId, + String artifactId, + String version, + String goal, + String executionId, + String phase, + BuildStatus status, + Instant startTime, + Duration duration, + List output) {} +} \ No newline at end of file diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportJsonWriter.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportJsonWriter.java new file mode 100644 index 000000000000..ad9e0dfe5a9e --- /dev/null +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/BuildReportJsonWriter.java @@ -0,0 +1,396 @@ +/* + * 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. + *

+ * 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()); + if (problem.getKey() != null) { + writeField(sb, indent + 1, "key", problem.getKey()); + } + 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()); + } + if (problem.getSuggestion() != null) { + writeField(sb, indent + 1, "suggestion", problem.getSuggestion()); + } + if (problem.getDocumentationUrl() != null) { + writeField(sb, indent + 1, "documentationUrl", problem.getDocumentationUrl()); + } + // 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) { + sb.append("{\n"); + writeField(sb, indent + 1, "groupId", module.groupId()); + writeField(sb, indent + 1, "artifactId", module.artifactId()); + writeField(sb, indent + 1, "version", module.version()); + writeField(sb, indent + 1, "status", module.status().name()); + writeField(sb, indent + 1, "startTime", module.startTime().toString()); + writeField(sb, indent + 1, "duration", module.duration().toString()); + + // mojos array + writeIndent(sb, indent + 1); + sb.append("\"mojos\": "); + if (module.mojos().isEmpty()) { + sb.append("[]"); + } else { + sb.append("[\n"); + for (int i = 0; i < module.mojos().size(); i++) { + writeIndent(sb, indent + 2); + writeMojo(sb, module.mojos().get(i), indent + 2); + if (i < module.mojos().size() - 1) { + sb.append(','); + } + sb.append('\n'); + } + writeIndent(sb, indent + 1); + sb.append(']'); + } + sb.append(",\n"); + + // output array — module-level log lines (between mojos) + writeOutputArray(sb, indent + 1, module.output()); + sb.append('\n'); + + writeIndent(sb, indent); + sb.append('}'); + } + + private static void writeMojo(StringBuilder sb, MojoReport mojo, int indent) { + sb.append("{\n"); + writeField(sb, indent + 1, "groupId", mojo.groupId()); + writeField(sb, indent + 1, "artifactId", mojo.artifactId()); + writeField(sb, indent + 1, "version", mojo.version()); + writeField(sb, indent + 1, "goal", mojo.goal()); + writeNullableField(sb, indent + 1, "executionId", mojo.executionId(), true); + writeNullableField(sb, indent + 1, "phase", mojo.phase(), true); + writeField(sb, indent + 1, "status", mojo.status().name()); + writeField(sb, indent + 1, "startTime", mojo.startTime().toString()); + writeField(sb, indent + 1, "duration", mojo.duration().toString()); + + // output array — captured log lines + writeOutputArray(sb, indent + 1, mojo.output()); + sb.append('\n'); + + writeIndent(sb, indent); + sb.append('}'); + } + + private static void writeFailure(StringBuilder sb, FailureReport failure, int indent) { + sb.append("{\n"); + writeField(sb, indent + 1, "module", failure.module()); + writeNullableField(sb, indent + 1, "mojo", failure.mojo(), true); + writeField(sb, indent + 1, "timestamp", failure.timestamp().toString()); + writeNullableField(sb, indent + 1, "exceptionType", failure.exceptionType(), true); + if (failure.stackTrace() != null) { + writeField(sb, indent + 1, "message", failure.message()); + writeLastField(sb, indent + 1, "stackTrace", failure.stackTrace()); + } else { + writeLastField(sb, indent + 1, "message", failure.message()); + } + writeIndent(sb, indent); + sb.append('}'); + } + + /** + * Writes an {@code "output": [...]} array of structured log events + * (used by report, module, and mojo). + * This is always the last field in its object, so no trailing comma. + */ + private static void writeOutputArray(StringBuilder sb, int indent, java.util.List events) { + writeIndent(sb, indent); + sb.append("\"output\": "); + if (events.isEmpty()) { + sb.append("[]"); + } else { + sb.append("[\n"); + for (int i = 0; i < events.size(); i++) { + writeIndent(sb, indent + 1); + writeLogEvent(sb, events.get(i), indent + 1); + if (i < events.size() - 1) { + sb.append(','); + } + sb.append('\n'); + } + writeIndent(sb, indent); + sb.append(']'); + } + } + + private static void writeLogEvent(StringBuilder sb, LogEvent event, int indent) { + sb.append("{\n"); + writeField(sb, indent + 1, "timestamp", event.timestamp().toString()); + writeField(sb, indent + 1, "level", event.level().name()); + if (event.loggerName() != null) { + writeField(sb, indent + 1, "loggerName", event.loggerName()); + } + writeField(sb, indent + 1, "message", event.message()); + if (event.stackTrace() != null) { + writeField(sb, indent + 1, "stackTrace", event.stackTrace()); + } + // Source metadata — present for Log API and JUL events + if (event.sourceClassName() != null) { + writeField(sb, indent + 1, "sourceClassName", event.sourceClassName()); + } + if (event.sourceMethodName() != null) { + writeField(sb, indent + 1, "sourceMethodName", event.sourceMethodName()); + } + if (event.threadId() >= 0) { + writeField(sb, indent + 1, "threadId", event.threadId()); + } + if (event.sequenceNumber() >= 0) { + writeField(sb, indent + 1, "sequenceNumber", event.sequenceNumber()); + } + removeTrailingComma(sb); + writeIndent(sb, indent); + sb.append('}'); + } + + // ---- Low-level JSON writing helpers ---- + + private static void writeField(StringBuilder sb, int indent, String key, String value) { + writeIndent(sb, indent); + sb.append('"').append(key).append("\": "); + writeJsonString(sb, value); + sb.append(",\n"); + } + + private static void writeField(StringBuilder sb, int indent, String key, int value) { + writeIndent(sb, indent); + sb.append('"').append(key).append("\": ").append(value).append(",\n"); + } + + private static void writeField(StringBuilder sb, int indent, String key, long value) { + writeIndent(sb, indent); + sb.append('"').append(key).append("\": ").append(value).append(",\n"); + } + + private static void writeField(StringBuilder sb, int indent, String key, boolean value) { + writeIndent(sb, indent); + sb.append('"').append(key).append("\": ").append(value).append(",\n"); + } + + /** + * Removes the trailing comma from the last field in a JSON object. + * Turns {@code "field": value,\n} into {@code "field": value\n}. + */ + private static void removeTrailingComma(StringBuilder sb) { + int len = sb.length(); + if (len >= 2 && sb.charAt(len - 2) == ',' && sb.charAt(len - 1) == '\n') { + sb.deleteCharAt(len - 2); + } + } + + private static void writeLastField(StringBuilder sb, int indent, String key, String value) { + writeIndent(sb, indent); + sb.append('"').append(key).append("\": "); + writeJsonString(sb, value); + sb.append('\n'); + } + + private static void writeNullableField( + StringBuilder sb, int indent, String key, String value, @SuppressWarnings("unused") boolean hasMore) { + writeIndent(sb, indent); + sb.append('"').append(key).append("\": "); + if (value != null) { + writeJsonString(sb, value); + } else { + sb.append("null"); + } + sb.append(",\n"); + } + + private static void writeStringArray(StringBuilder sb, int indent, String key, java.util.List values) { + writeIndent(sb, indent); + sb.append('"').append(key).append("\": ["); + for (int i = 0; i < values.size(); i++) { + writeJsonString(sb, values.get(i)); + if (i < values.size() - 1) { + sb.append(", "); + } + } + sb.append("],\n"); + } + + private static void writeJsonString(StringBuilder sb, String value) { + sb.append('"'); + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + switch (c) { + case '"': + sb.append("\\\""); + break; + case '\\': + sb.append("\\\\"); + break; + case '\n': + sb.append("\\n"); + break; + case '\r': + sb.append("\\r"); + break; + case '\t': + sb.append("\\t"); + break; + case '\b': + sb.append("\\b"); + break; + case '\f': + sb.append("\\f"); + break; + default: + if (c < 0x20) { + sb.append("\\u"); + sb.append(String.format("%04x", (int) c)); + } else { + sb.append(c); + } + } + } + sb.append('"'); + } + + private static void writeIndent(StringBuilder sb, int level) { + sb.append(" ".repeat(level)); + } +} \ No newline at end of file diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultBuildReport.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultBuildReport.java new file mode 100644 index 000000000000..c0522681a5b2 --- /dev/null +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultBuildReport.java @@ -0,0 +1,82 @@ +/* + * 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 java.time.Duration; +import java.time.Instant; +import java.util.List; + +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.services.BuilderProblem; + +/** + * Internal immutable implementation of {@link BuildReport}. + */ +record DefaultBuildReport( + BuildStatus status, + Duration duration, + Instant startTime, + String mavenVersion, + String javaVersion, + List goals, + String project, + boolean multiModule, + int threads, + List modules, + List failures, + List problems, + List output) + implements BuildReport { + + private static final int FORMAT_VERSION = 1; + + @Override + public int formatVersion() { + return FORMAT_VERSION; + } + + @Override + public List modules() { + return List.copyOf(modules); + } + + @Override + public List failures() { + return List.copyOf(failures); + } + + @Override + public List problems() { + return List.copyOf(problems); + } + + @Override + public List goals() { + return List.copyOf(goals); + } + + @Override + public List output() { + return List.copyOf(output); + } +} diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultDiagnosticCollector.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultDiagnosticCollector.java new file mode 100644 index 000000000000..3576487f61b5 --- /dev/null +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultDiagnosticCollector.java @@ -0,0 +1,221 @@ +/* + * 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.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.LongAdder; + +import org.apache.maven.api.services.BuilderProblem; + +import static java.util.Objects.requireNonNull; + +/** + * Thread-safe collector for {@link BuilderProblem}s with deduplication support. + *

+ * Problems with a non-null {@link BuilderProblem#getKey()} are deduplicated: + * the first occurrence is stored, subsequent duplicates only increment the + * counter. Problems without a key are always stored (up to the cap). + *

+ * This implementation is safe for use from parallel module builds + * ({@code -T}) and from any thread within a plugin execution. + * + * @since 4.1.0 + */ +@Named +@Singleton +public final class DefaultDiagnosticCollector { + + /** + * Maximum number of unique problems to store. + * Protects against runaway plugins that produce unbounded problems. + */ + static final int MAX_DIAGNOSTICS = 1000; + + /** + * Preserves insertion order: key → first problem. + * Using ConcurrentHashMap for thread safety; insertion order is tracked + * separately in {@link #orderedKeys}. + */ + private final Map uniqueProblems = new ConcurrentHashMap<>(); + + /** Counts per key (including the first occurrence). */ + private final Map counts = new ConcurrentHashMap<>(); + + /** + * Insertion-order tracking. Synchronized on itself for ordered access. + * The key list mirrors {@link #uniqueProblems} keys in insertion order. + */ + private final List orderedKeys = Collections.synchronizedList(new ArrayList<>()); + + /** Counter for problems without a key, used to generate synthetic keys. */ + private final LongAdder noKeyCounter = new LongAdder(); + + /** + * Keys to suppress. Problems with a key in this set are silently dropped. + * Configured via {@link #setSuppressedKeys(Set)}, typically from the + * {@code maven.diagnostic.suppress} user property. + */ + private volatile Set suppressedKeys = Set.of(); + + /** + * Sets the keys to suppress. Problems with a matching key will be + * silently dropped from {@link #report(BuilderProblem)}. + *

+ * Supports both exact keys ({@code "deprecated-source-target"}) and + * prefix matching with wildcard ({@code "auto:*"} to suppress all + * auto-collected warnings from Maven 3 plugins). + * + * @param keys the set of keys to suppress; must not be null + */ + public void setSuppressedKeys(Set keys) { + this.suppressedKeys = Set.copyOf(requireNonNull(keys, "keys")); + } + + /** + * Reports a problem. + *

+ * If the problem has a non-null {@link BuilderProblem#getKey()} and a + * problem with the same key has already been reported, the duplicate is + * counted but not stored again. + * + * @param problem the problem to report + */ + public void report(BuilderProblem problem) { + requireNonNull(problem, "problem"); + String key = problem.getKey(); + + // Problems without a key get a synthetic key for storage + if (key == null) { + noKeyCounter.increment(); + key = "__no_key__" + noKeyCounter.longValue(); + } + + // Check suppression: exact match or prefix wildcard (e.g. "auto:*") + if (isSuppressed(key)) { + return; + } + + // Always increment the count + counts.computeIfAbsent(key, k -> new LongAdder()).increment(); + + // Store the first occurrence only (if within cap) + if (uniqueProblems.putIfAbsent(key, problem) == null) { + if (uniqueProblems.size() <= MAX_DIAGNOSTICS) { + orderedKeys.add(key); + } else { + // Over cap — remove the entry we just added + uniqueProblems.remove(key); + } + } + } + + private boolean isSuppressed(String key) { + Set suppressed = this.suppressedKeys; + if (suppressed.isEmpty()) { + return false; + } + if (suppressed.contains(key)) { + return true; + } + // Check prefix wildcards: "auto:*" matches "auto:Compiler:1a2b3c" + for (String pattern : suppressed) { + if (pattern.endsWith("*") && key.startsWith(pattern.substring(0, pattern.length() - 1))) { + return true; + } + } + return false; + } + + /** + * Returns all unique problems reported so far, in the order they + * were first reported. + * + * @return an unmodifiable list of unique problems, never {@code null} + */ + public List getProblems() { + List result; + synchronized (orderedKeys) { + result = new ArrayList<>(orderedKeys.size()); + for (String key : orderedKeys) { + BuilderProblem p = uniqueProblems.get(key); + if (p != null) { + result.add(p); + } + } + } + return Collections.unmodifiableList(result); + } + + /** + * Returns a deduplicated summary of all reported problems. + * Each entry contains the problem and the number of times it was + * reported (across all modules). + * + * @return an unmodifiable list of summaries, never {@code null} + */ + public List getSummary() { + List result; + synchronized (orderedKeys) { + result = new ArrayList<>(orderedKeys.size()); + for (String key : orderedKeys) { + BuilderProblem p = uniqueProblems.get(key); + LongAdder counter = counts.get(key); + if (p != null && counter != null) { + result.add(new DefaultDiagnosticSummary(p, counter.intValue())); + } + } + } + return Collections.unmodifiableList(result); + } + + /** + * Returns {@code true} if at least one problem with severity + * {@link BuilderProblem.Severity#WARNING WARNING} or higher has been reported. + */ + public boolean hasWarnings() { + return hasAtSeverity(BuilderProblem.Severity.WARNING); + } + + /** + * Returns {@code true} if at least one problem with severity + * {@link BuilderProblem.Severity#ERROR ERROR} has been reported. + */ + public boolean hasErrors() { + return hasAtSeverity(BuilderProblem.Severity.ERROR); + } + + private boolean hasAtSeverity(BuilderProblem.Severity targetSeverity) { + for (BuilderProblem p : uniqueProblems.values()) { + // Severity enum is ordered most severe first: FATAL, ERROR, WARNING, INFO + // A problem "has" the target severity if its ordinal is <= target ordinal + if (p.getSeverity().ordinal() <= targetSeverity.ordinal()) { + return true; + } + } + return false; + } +} \ No newline at end of file diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultDiagnosticReporter.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultDiagnosticReporter.java new file mode 100644 index 000000000000..6e49700e644d --- /dev/null +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultDiagnosticReporter.java @@ -0,0 +1,55 @@ +/* + * 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.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import org.apache.maven.api.services.BuilderProblem; +import org.apache.maven.api.services.DiagnosticReporter; + +import static java.util.Objects.requireNonNull; + +/** + * Default implementation of {@link DiagnosticReporter} that delegates + * to {@link DefaultDiagnosticCollector}. + *

+ * Registered as a {@link org.apache.maven.api.Service} so it is + * automatically available to Maven 4 plugins via {@code @Inject} + * or {@code Session.getService(DiagnosticReporter.class)}. + * + * @since 4.1.0 + */ +@Named +@Singleton +public class DefaultDiagnosticReporter implements DiagnosticReporter { + + private final DefaultDiagnosticCollector collector; + + @Inject + public DefaultDiagnosticReporter(DefaultDiagnosticCollector collector) { + this.collector = requireNonNull(collector, "collector"); + } + + @Override + public void report(BuilderProblem problem) { + collector.report(problem); + } +} \ No newline at end of file diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultDiagnosticSummary.java similarity index 57% rename from impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java rename to impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultDiagnosticSummary.java index bbd487fa5f87..4a75a50b3a10 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/Log4j2Configuration.java +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultDiagnosticSummary.java @@ -16,29 +16,22 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.maven.cling.logging.impl; +package org.apache.maven.internal.build; -import org.apache.maven.cling.logging.BaseSlf4jConfiguration; +import org.apache.maven.api.services.BuilderProblem; + +import static java.util.Objects.requireNonNull; /** - * Configuration for slf4j-log4j2. - * - * @since 3.1.0 + * A deduplicated summary entry pairing a unique {@link BuilderProblem} with the + * number of times it was reported across the build. */ -public class Log4j2Configuration extends BaseSlf4jConfiguration { - @Override - public void setRootLoggerLevel(Level level) { - String value = - switch (level) { - case DEBUG -> "debug"; - case INFO -> "info"; - default -> "error"; - }; - System.setProperty("maven.logging.root.level", value); - } +record DefaultDiagnosticSummary(BuilderProblem problem, int count) { - @Override - public void activate() { - // no op + DefaultDiagnosticSummary { + requireNonNull(problem, "problem"); + if (count < 1) { + throw new IllegalArgumentException("count must be >= 1"); + } } -} +} \ No newline at end of file diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultFailureReport.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultFailureReport.java new file mode 100644 index 000000000000..b0bc075d34f3 --- /dev/null +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultFailureReport.java @@ -0,0 +1,30 @@ +/* + * 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 java.time.Instant; + +import org.apache.maven.api.build.report.FailureReport; + +/** + * Internal immutable implementation of {@link FailureReport}. + */ +record DefaultFailureReport( + String module, String mojo, Instant timestamp, String exceptionType, String message, String stackTrace) + implements FailureReport {} diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultLogEvent.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultLogEvent.java new file mode 100644 index 000000000000..95951d8e7ac6 --- /dev/null +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultLogEvent.java @@ -0,0 +1,77 @@ +/* + * 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 java.time.Instant; + +import org.apache.maven.api.build.report.LogEvent; +import org.apache.maven.api.build.report.LogLevel; + +/** + * Immutable implementation of {@link LogEvent}. + *

+ * Public to allow construction from other packages within the Maven + * implementation (e.g. {@code ProjectBuildLogAppender}). + * + * @param timestamp when the event was produced + * @param level the severity level + * @param message the clean log message (without level prefix or ANSI) + * @param loggerName the name of the logger, or {@code null} + * @param stackTrace the stack trace string, or {@code null} + * @param formattedMessage the fully formatted console line, or {@code null} + * @param sourceClassName the source class name (Log API mojo FQCN or JUL source), or {@code null} + * @param sourceMethodName the source method name (via StackWalker or JUL), or {@code null} + * @param threadId the originating thread ID, or {@code -1} if unavailable + * @param sequenceNumber the JUL sequence number for ordering, or {@code -1} if unavailable + */ +public record DefaultLogEvent( + Instant timestamp, + LogLevel level, + String message, + String loggerName, + String stackTrace, + String formattedMessage, + String sourceClassName, + String sourceMethodName, + long threadId, + long sequenceNumber) + implements LogEvent { + + /** + * Convenience constructor for events without source metadata + * (i.e. direct SLF4J events). + */ + public DefaultLogEvent( + Instant timestamp, + LogLevel level, + String message, + String loggerName, + String stackTrace, + String formattedMessage) { + this(timestamp, level, message, loggerName, stackTrace, formattedMessage, null, null, -1, -1); + } + + /** + * Convenience constructor for events created without a formatted message + * (e.g. in tests or programmatic construction). + */ + DefaultLogEvent(Instant timestamp, LogLevel level, String message, String loggerName, String stackTrace) { + this(timestamp, level, message, loggerName, stackTrace, null, null, null, -1, -1); + } +} diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultModuleReport.java similarity index 51% rename from impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java rename to impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultModuleReport.java index 67ee429d82ab..64c79636ebd1 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/logging/impl/LogbackConfiguration.java +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultModuleReport.java @@ -16,31 +16,38 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.maven.cling.logging.impl; +package org.apache.maven.internal.build; -import org.apache.maven.cling.logging.BaseSlf4jConfiguration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.time.Duration; +import java.time.Instant; +import java.util.List; + +import org.apache.maven.api.build.report.BuildStatus; +import org.apache.maven.api.build.report.LogEvent; +import org.apache.maven.api.build.report.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; /** - * Configuration for slf4j-logback. - * - * @since 3.1.0 + * Internal immutable implementation of {@link ModuleReport}. */ -public class LogbackConfiguration extends BaseSlf4jConfiguration { +record DefaultModuleReport( + String groupId, + String artifactId, + String version, + BuildStatus status, + Instant startTime, + Duration duration, + List mojos, + List output) + implements ModuleReport { + @Override - public void setRootLoggerLevel(Level level) { - ch.qos.logback.classic.Level value = - switch (level) { - case DEBUG -> ch.qos.logback.classic.Level.DEBUG; - case INFO -> ch.qos.logback.classic.Level.INFO; - default -> ch.qos.logback.classic.Level.ERROR; - }; - ((ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(value); + public List mojos() { + return List.copyOf(mojos); } @Override - public void activate() { - // no op + public List output() { + return List.copyOf(output); } } diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultMojoReport.java b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultMojoReport.java new file mode 100644 index 000000000000..5270d9c11fe3 --- /dev/null +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/build/DefaultMojoReport.java @@ -0,0 +1,49 @@ +/* + * 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 java.time.Duration; +import java.time.Instant; +import java.util.List; + +import org.apache.maven.api.build.report.BuildStatus; +import org.apache.maven.api.build.report.LogEvent; +import org.apache.maven.api.build.report.MojoReport; + +/** + * Internal immutable implementation of {@link MojoReport}. + */ +record DefaultMojoReport( + String groupId, + String artifactId, + String version, + String goal, + String executionId, + String phase, + BuildStatus status, + Instant startTime, + Duration duration, + List output) + implements MojoReport { + + @Override + public List output() { + return List.copyOf(output); + } +} diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLog.java b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLog.java index b1cf40cc4059..581227c76d62 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLog.java +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLog.java @@ -18,6 +18,7 @@ */ package org.apache.maven.internal.impl; +import java.lang.StackWalker.StackFrame; import java.util.function.Supplier; import org.apache.maven.api.plugin.Log; @@ -27,12 +28,64 @@ import static java.util.Objects.requireNonNull; public class DefaultLog implements Log { + + /** + * Metadata captured from Log API calls, mirroring the JUL metadata + * pattern in {@code MavenJulHandler}. + * + * @param sourceClassName the fully qualified class name of the caller + * @param sourceMethodName the method that issued the log call + * @param threadId the originating thread ID + */ + public record LogApiMetadata(String sourceClassName, String sourceMethodName, long threadId) {} + + private static final ThreadLocal LOG_API_METADATA = new ThreadLocal<>(); + private static final StackWalker WALKER = StackWalker.getInstance(); + private static final String THIS_CLASS = DefaultLog.class.getName(); + + /** + * Returns the Log API metadata for the current log event being processed, + * or {@code null} if the current event did not originate from the Log API. + *

+ * Called from {@code ProjectBuildLogAppender.accept()} to populate + * {@code LogEvent.sourceClassName()} and {@code LogEvent.sourceMethodName()}. + * + * @return the current Log API metadata, or {@code null} + */ + public static LogApiMetadata getLogApiMetadata() { + return LOG_API_METADATA.get(); + } + private final Logger logger; public DefaultLog(Logger logger) { this.logger = requireNonNull(logger); } + /** + * Wraps a logging call with Log API metadata: captures the caller's + * method name via {@link StackWalker}, sets the ThreadLocal, executes + * the actual SLF4J call, and clears the ThreadLocal. + *

+ * The source class name is taken from the SLF4J logger name (which + * is the mojo implementation FQCN, set at injection time). The + * source method name is resolved by walking the stack past this class + * to find the first external caller frame. + */ + private void withMetadata(Runnable logAction) { + String callerMethodName = WALKER.walk(frames -> frames.dropWhile(f -> THIS_CLASS.equals(f.getClassName())) + .findFirst() + .map(StackFrame::getMethodName) + .orElse(null)); + LOG_API_METADATA.set(new LogApiMetadata( + logger.getName(), callerMethodName, Thread.currentThread().getId())); + try { + logAction.run(); + } finally { + LOG_API_METADATA.remove(); + } + } + @Override public boolean isTraceEnabled() { return logger.isTraceEnabled(); @@ -41,175 +94,175 @@ public boolean isTraceEnabled() { @Override public void trace(CharSequence content) { if (isTraceEnabled()) { - logger.trace(toString(content)); + withMetadata(() -> logger.trace(toString(content))); } } @Override public void trace(CharSequence content, Throwable error) { if (isTraceEnabled()) { - logger.trace(toString(content), error); + withMetadata(() -> logger.trace(toString(content), error)); } } @Override public void trace(Throwable error) { if (isTraceEnabled()) { - logger.trace("", error); + withMetadata(() -> logger.trace("", error)); } } @Override public void trace(Supplier content) { if (isTraceEnabled()) { - logger.trace(content.get()); + withMetadata(() -> logger.trace(content.get())); } } @Override public void trace(Supplier content, Throwable error) { if (isTraceEnabled()) { - logger.trace(content.get(), error); + withMetadata(() -> logger.trace(content.get(), error)); } } @Override public void debug(CharSequence content) { if (isDebugEnabled()) { - logger.debug(toString(content)); + withMetadata(() -> logger.debug(toString(content))); } } @Override public void debug(CharSequence content, Throwable error) { if (isDebugEnabled()) { - logger.debug(toString(content), error); + withMetadata(() -> logger.debug(toString(content), error)); } } @Override public void debug(Throwable error) { if (isDebugEnabled()) { - logger.debug("", error); + withMetadata(() -> logger.debug("", error)); } } @Override public void debug(Supplier content) { if (isDebugEnabled()) { - logger.debug(content.get()); + withMetadata(() -> logger.debug(content.get())); } } @Override public void debug(Supplier content, Throwable error) { if (isDebugEnabled()) { - logger.debug(content.get(), error); + withMetadata(() -> logger.debug(content.get(), error)); } } @Override public void info(CharSequence content) { if (isInfoEnabled()) { - logger.info(toString(content)); + withMetadata(() -> logger.info(toString(content))); } } @Override public void info(CharSequence content, Throwable error) { if (isInfoEnabled()) { - logger.info(toString(content), error); + withMetadata(() -> logger.info(toString(content), error)); } } @Override public void info(Throwable error) { if (isInfoEnabled()) { - logger.info("", error); + withMetadata(() -> logger.info("", error)); } } @Override public void info(Supplier content) { if (isInfoEnabled()) { - logger.info(content.get()); + withMetadata(() -> logger.info(content.get())); } } @Override public void info(Supplier content, Throwable error) { if (isInfoEnabled()) { - logger.info(content.get(), error); + withMetadata(() -> logger.info(content.get(), error)); } } @Override public void warn(CharSequence content) { if (isWarnEnabled()) { - logger.warn(toString(content)); + withMetadata(() -> logger.warn(toString(content))); } } @Override public void warn(CharSequence content, Throwable error) { if (isWarnEnabled()) { - logger.warn(toString(content), error); + withMetadata(() -> logger.warn(toString(content), error)); } } @Override public void warn(Throwable error) { if (isWarnEnabled()) { - logger.warn("", error); + withMetadata(() -> logger.warn("", error)); } } @Override public void warn(Supplier content) { if (isWarnEnabled()) { - logger.warn(content.get()); + withMetadata(() -> logger.warn(content.get())); } } @Override public void warn(Supplier content, Throwable error) { if (isWarnEnabled()) { - logger.warn(content.get(), error); + withMetadata(() -> logger.warn(content.get(), error)); } } @Override public void error(CharSequence content) { if (isErrorEnabled()) { - logger.error(toString(content)); + withMetadata(() -> logger.error(toString(content))); } } @Override public void error(CharSequence content, Throwable error) { if (isErrorEnabled()) { - logger.error(toString(content), error); + withMetadata(() -> logger.error(toString(content), error)); } } @Override public void error(Throwable error) { if (isErrorEnabled()) { - logger.error("", error); + withMetadata(() -> logger.error("", error)); } } @Override public void error(Supplier content) { if (isErrorEnabled()) { - logger.error(content.get()); + withMetadata(() -> logger.error(content.get())); } } @Override public void error(Supplier content, Throwable error) { if (isErrorEnabled()) { - logger.error(content.get(), error); + withMetadata(() -> logger.error(content.get(), error)); } } diff --git a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java index bc7e55c1bb70..683a440efd73 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java +++ b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java @@ -293,9 +293,7 @@ public Thread getOwner() { } private static void warn(String msg) { - for (String s : MultilineMessageHelper.format(msg)) { - LOGGER.warn(s); - } + LOGGER.warn(String.join("\n", MultilineMessageHelper.format(msg))); } private void doExecute(MavenSession session, MojoExecution mojoExecution, DependencyContext dependencyContext) @@ -438,27 +436,9 @@ public List executeForkedExecutions(MojoExecution mojoExecution, M .getData() .computeIfAbsent(PROJECT_INDEX, () -> new ProjectIndex(session.getProjects())); - Integer idx = projectIndex.getIndices().get(projectId); - if (idx == null) { - throw new LifecycleExecutionException( - "Forked execution references project '" + projectId - + "' which is not in the reactor. This can happen with parallel builds" - + " (-T) or when extensions modify the session projects.", - mojoExecution, - project); - } - int index = idx; + int index = projectIndex.getIndices().get(projectId); MavenProject forkedProject = projectIndex.getProjects().get(projectId); - if (forkedProject == null) { - throw new LifecycleExecutionException( - "Forked execution references project '" + projectId - + "' which is not in the reactor (no project instance). This can happen" - + " with parallel builds (-T) or when extensions modify the session" - + " projects.", - mojoExecution, - project); - } forkedProjects.add(forkedProject); @@ -503,4 +483,4 @@ public List executeForkedExecutions(MojoExecution mojoExecution, M return forkedProjects; } -} +} \ No newline at end of file diff --git a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/builder/BuilderCommon.java b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/builder/BuilderCommon.java index f94271ed1f22..507b0f208598 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/builder/BuilderCommon.java +++ b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/builder/BuilderCommon.java @@ -114,30 +114,36 @@ public MavenExecutionPlan resolveBuildPlan( && session.getProjects().size() > 1) { final Set unsafePlugins = executionPlan.getNonThreadSafePlugins(); if (!unsafePlugins.isEmpty()) { - for (String s : MultilineMessageHelper.format( - "Your build is requesting parallel execution, but this project contains the following " - + "plugin(s) that have goals not marked as thread-safe to support parallel execution.", - "While this /may/ work fine, please look for plugin updates and/or " - + "request plugins be made thread-safe.", - "If reporting an issue, report it against the plugin in question, not against Apache Maven.")) { - logger.warn(s); - } + StringBuilder warning = new StringBuilder(); + warning.append( + String.join( + "\n", + MultilineMessageHelper.format( + "Your build is requesting parallel execution, but this project contains the following " + + "plugin(s) that have goals not marked as thread-safe to support parallel execution.", + "While this /may/ work fine, please look for plugin updates and/or " + + "request plugins be made thread-safe.", + "If reporting an issue, report it against the plugin in question, not against Apache Maven."))); if (logger.isDebugEnabled()) { final Set unsafeGoals = executionPlan.getNonThreadSafeMojos(); - logger.warn("The following goals are not marked as thread-safe in " + project.getName() + ":"); + warning.append("\nThe following goals are not marked as thread-safe in ") + .append(project.getName()) + .append(":"); for (MojoDescriptor unsafeGoal : unsafeGoals) { - logger.warn(" " + unsafeGoal.getId()); + warning.append("\n ").append(unsafeGoal.getId()); } } else { - logger.warn("The following plugins are not marked as thread-safe in " + project.getName() + ":"); + warning.append("\nThe following plugins are not marked as thread-safe in ") + .append(project.getName()) + .append(":"); for (Plugin unsafePlugin : unsafePlugins) { - logger.warn(" " + unsafePlugin.getId()); + warning.append("\n ").append(unsafePlugin.getId()); } - logger.warn(""); - logger.warn("Enable verbose output (-X) to see precisely which goals are not marked as" + warning.append("\n\nEnable verbose output (-X) to see precisely which goals are not marked as" + " thread-safe."); } - logger.warn(MultilineMessageHelper.separatorLine()); + warning.append('\n').append(MultilineMessageHelper.separatorLine()); + logger.warn(warning.toString()); } } @@ -211,4 +217,4 @@ public static void attachToThread(MavenProject currentProject) { public static String getKey(MavenProject project) { return project.getGroupId() + ':' + project.getArtifactId() + ':' + project.getVersion(); } -} +} \ No newline at end of file diff --git a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java index f2c5d68254bf..97dce99de543 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java +++ b/impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanExecutor.java @@ -314,35 +314,36 @@ private void checkThreadSafety(BuildPlan buildPlan) { .filter(execution -> !execution.getMojoDescriptor().isV4Api()) .collect(Collectors.toSet()); if (!unsafeExecutions.isEmpty()) { - for (String s : MultilineMessageHelper.format(""" - Your build is requesting concurrent execution, but this project contains the \ - following plugin(s) that have goals not built with Maven 4 to support concurrent \ - execution. While this /may/ work fine, please look for plugin updates and/or \ - request plugins be made thread-safe. If reporting an issue, report it against the \ - plugin in question, not against Apache Maven.""")) { - logger.warn(s); - } + StringBuilder warning = new StringBuilder(); + warning.append(String.join( + "\n", + MultilineMessageHelper.format( + "Your build is requesting concurrent execution, but this project contains the " + + "following plugin(s) that have goals not built with Maven 4 to support concurrent " + + "execution. While this /may/ work fine, please look for plugin updates and/or " + + "request plugins be made thread-safe. If reporting an issue, report it against the " + + "plugin in question, not against Apache Maven."))); if (logger.isDebugEnabled()) { Set unsafeGoals = unsafeExecutions.stream() .map(MojoExecution::getMojoDescriptor) .collect(Collectors.toSet()); - logger.warn("The following goals are not Maven 4 goals:"); + warning.append("\nThe following goals are not Maven 4 goals:"); for (MojoDescriptor unsafeGoal : unsafeGoals) { - logger.warn(" " + unsafeGoal.getId()); + warning.append("\n ").append(unsafeGoal.getId()); } } else { Set unsafePlugins = unsafeExecutions.stream() .map(MojoExecution::getPlugin) .collect(Collectors.toSet()); - logger.warn("The following plugins are not Maven 4 plugins:"); + warning.append("\nThe following plugins are not Maven 4 plugins:"); for (Plugin unsafePlugin : unsafePlugins) { - logger.warn(" " + unsafePlugin.getId()); + warning.append("\n ").append(unsafePlugin.getId()); } - logger.warn(""); - logger.warn("Enable verbose output (-X) to see precisely which goals are not marked as" + warning.append("\n\nEnable verbose output (-X) to see precisely which goals are not marked as" + " thread-safe."); } - logger.warn(MultilineMessageHelper.separatorLine()); + warning.append('\n').append(MultilineMessageHelper.separatorLine()); + logger.warn(warning.toString()); } } } @@ -401,9 +402,9 @@ private void processStep(BuildStep step) { try { executeStep(step); executePlan(); - } catch (Throwable e) { + } catch (Exception e) { step.status.compareAndSet(SKIPPED, FAILED); - // Store the failure in the step for handling in the TEARDOWN phase + // Store the exception in the step for handling in the TEARDOWN phase step.exception = e; logger.debug("Stored exception for step {} to be handled in TEARDOWN phase", step, e); // Let the scheduler handle after:* phases and TEARDOWN in the next cycle @@ -438,10 +439,10 @@ private void processStep(BuildStep step) { } } executePlan(); - } catch (Throwable e) { + } catch (Exception e) { step.status.compareAndSet(SCHEDULED, FAILED); - // Store the failure in the step for handling in the TEARDOWN phase + // Store the exception in the step for handling in the TEARDOWN phase step.exception = e; logger.debug("Stored exception for step {} to be handled in TEARDOWN phase", step, e); @@ -525,7 +526,7 @@ private void executeStep(BuildStep step) throws IOException, LifecycleExecutionE List failures = null; boolean allWorkExecuted = true; for (BuildStep projectStep : plan.steps(step.project).toList()) { - Throwable exception = projectStep.exception; + Exception exception = projectStep.exception; if (exception != null) { if (failures == null) { failures = new ArrayList<>(); @@ -560,7 +561,7 @@ private void executeStep(BuildStep step) throws IOException, LifecycleExecutionE failure = new LifecycleExecutionException("Error building project"); failures.forEach(failure::addSuppressed); } - handleBuildError(reactorContext, session, step.project, failure, isFatal(failures)); + handleBuildError(reactorContext, session, step.project, failure); } else if (projectStarted && allWorkExecuted) { // If there were no failures, report success projectExecutionListener.afterProjectExecutionSuccess( @@ -593,20 +594,6 @@ private void executeStep(BuildStep step) throws IOException, LifecycleExecutionE step.status.compareAndSet(SCHEDULED, EXECUTED); } - /** - * Tells whether any of the failures collected for a project must halt the build. Several failures are - * reported through a wrapper, and a wrapper is always a checked exception, so an {@link Error} among - * them can only be seen by looking at the failures themselves. - * - * @param failures The failures collected for a single project - * @return {@code true} if the build must be halted; checked exceptions (ordinary plugin failures) are - * soft and allow the reactor to continue with other projects, while {@link RuntimeException}s - * and {@link Error}s indicate an unexpected JVM or framework state and halt the build - */ - private static boolean isFatal(List failures) { - return failures.stream().anyMatch(t -> t instanceof RuntimeException || !(t instanceof Exception)); - } - private void attachToThread(BuildStep step) { BuildPlanExecutor.attachToThread(step.project); session.setCurrentProject(step.project); @@ -731,23 +718,18 @@ private void applyAfterLinks(MojoDescriptor mojoDescriptor, MavenProject project * all upstream projects are returned. Otherwise, only projects that the given * project depends on with a matching scope are included. *

- * The scope parameter is a lifecycle dependency scope (as declared in - * {@link org.apache.maven.api.DependencyScope}). It is expanded to match all - * artifact scopes that contribute to that dependency scope for build ordering: - *

    - *
  • {@code "compile"} matches compile, provided, system, and null-scoped - * (Maven default) dependencies
  • - *
  • {@code "runtime"} matches compile, runtime, and null-scoped dependencies
  • - *
  • {@code "test"} matches all scopes
  • - *
  • {@code "test-only"} matches only test-scoped dependencies
  • - *
- * This ensures that reactor dependencies contributing to a given classpath are - * properly ordered in the build plan (e.g. a provided-scope reactor dependency - * is built before the consumer's compile phase). + * Matching is exact on the dependency's declared scope string (e.g. "compile", + * "provided", "test"). Maven's default dependency scope is "compile" (when no + * scope is declared), so a null scope in the model is treated as "compile" for + * matching purposes. Note that this does not perform path-scope + * resolution — for example, filtering by "compile" will not include + * "provided"-scoped dependencies even though they contribute to + * {@code PathScope.MAIN_COMPILE}. This keeps the filter simple and predictable; + * broader scope-aware filtering can be added in a follow-up if needed. * * @param project the project whose dependencies to check * @param upstreamProjects the list of upstream reactor projects - * @param scope the lifecycle dependency scope to filter by, or null/empty for all + * @param scope the dependency scope to filter by, or null/empty for all * @return the filtered list of upstream projects */ static List filterByScope( @@ -755,29 +737,14 @@ static List filterByScope( if (scope == null || scope.isEmpty()) { return upstreamProjects; } - Set matchingScopes = expandScope(scope); return upstreamProjects.stream() .filter(dep -> project.getDependencies().stream() .anyMatch(d -> dep.getGroupId().equals(d.getGroupId()) && dep.getArtifactId().equals(d.getArtifactId()) - && matchingScopes.contains(d.getScope() != null ? d.getScope() : "compile"))) + && scope.equals(d.getScope() != null ? d.getScope() : "compile"))) .collect(Collectors.toList()); } - /** - * Expands a lifecycle dependency scope to the set of artifact scopes that - * contribute to it for build ordering purposes. - */ - private static Set expandScope(String scope) { - return switch (scope) { - case "compile" -> Set.of("compile", "provided", "system"); - case "runtime" -> Set.of("compile", "runtime"); - case "test" -> Set.of("compile", "provided", "system", "runtime", "test"); - case "test-only" -> Set.of("test"); - default -> Set.of(scope); - }; - } - protected BuildPlan computeForkPlan(BuildStep step, MojoExecution execution, BuildPlan buildPlan) { MojoDescriptor mojoDescriptor = execution.getMojoDescriptor(); PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor(); @@ -897,16 +864,13 @@ private String getResolvedPhase(String phase) { * @param buildContext The reactor context * @param session The Maven session * @param mavenProject The project that failed - * @param t The exception that caused the failure, possibly a wrapper around several failures - * @param fatal Whether the failure must halt the build. This cannot be read off {@code t}, because a - * wrapper around several failures hides what is inside it. See {@link #isFatal(List)}. + * @param t The exception that caused the failure */ protected void handleBuildError( final ReactorContext buildContext, final MavenSession session, final MavenProject mavenProject, - Throwable t, - boolean fatal) { + Throwable t) { // record the error and mark the project as failed Clock clock = getClock(mavenProject); buildContext.getResult().addException(t); @@ -915,12 +879,12 @@ protected void handleBuildError( .addBuildSummary(new BuildFailure(mavenProject, clock.execTime(), clock.wallTime(), t)); // notify listeners about "soft" project build failures only - if (!fatal && t instanceof Exception exception) { + if (t instanceof Exception exception && !(t instanceof RuntimeException)) { eventCatapult.fire(ExecutionEvent.Type.ProjectFailed, session, null, exception); } // reactor failure modes - if (fatal) { + if (t instanceof RuntimeException || !(t instanceof Exception)) { // fail fast on RuntimeExceptions, Errors and "other" Throwables // assume these are system errors and further build is meaningless buildContext.getReactorBuildStatus().halt(); @@ -1273,4 +1237,4 @@ protected Duration execTime() { return exec; } } -} +} \ No newline at end of file diff --git a/impl/maven-core/src/main/java/org/apache/maven/logging/BuildEventListener.java b/impl/maven-core/src/main/java/org/apache/maven/logging/BuildEventListener.java index 39573d061cd0..c1d771b5c11b 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/logging/BuildEventListener.java +++ b/impl/maven-core/src/main/java/org/apache/maven/logging/BuildEventListener.java @@ -18,6 +18,7 @@ */ package org.apache.maven.logging; +import org.apache.maven.api.build.report.LogEvent; import org.apache.maven.execution.ExecutionEvent; import org.eclipse.aether.transfer.TransferEvent; @@ -30,7 +31,7 @@ public interface BuildEventListener { void projectStarted(String projectId); - void projectLogMessage(String projectId, String event); + void projectLogMessage(String projectId, LogEvent event); void projectFinished(String projectId); diff --git a/impl/maven-core/src/main/java/org/apache/maven/logging/LoggingExecutionListener.java b/impl/maven-core/src/main/java/org/apache/maven/logging/LoggingExecutionListener.java index 700834eb80df..6685351b362a 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/logging/LoggingExecutionListener.java +++ b/impl/maven-core/src/main/java/org/apache/maven/logging/LoggingExecutionListener.java @@ -129,22 +129,21 @@ public void mojoStarted(ExecutionEvent event) { @Override public void mojoSucceeded(ExecutionEvent event) { setMdc(event); - delegate.mojoSucceeded(event); ProjectBuildLogAppender.setMojoId(null); + delegate.mojoSucceeded(event); } @Override public void mojoFailed(ExecutionEvent event) { setMdc(event); - delegate.mojoFailed(event); ProjectBuildLogAppender.setMojoId(null); + delegate.mojoFailed(event); } @Override public void mojoSkipped(ExecutionEvent event) { setMdc(event); delegate.mojoSkipped(event); - ProjectBuildLogAppender.setMojoId(null); } @Override @@ -152,22 +151,18 @@ public void forkStarted(ExecutionEvent event) { setMdc(event); delegate.forkStarted(event); ProjectBuildLogAppender.setForkingProjectId(event.getProject().getArtifactId()); - // Save the forking mojo's ID so it can be restored when the fork completes - ProjectBuildLogAppender.setForkingMojoId(ProjectBuildLogAppender.getMojoId()); } @Override public void forkSucceeded(ExecutionEvent event) { delegate.forkSucceeded(event); ProjectBuildLogAppender.setForkingProjectId(null); - ProjectBuildLogAppender.setForkingMojoId(null); } @Override public void forkFailed(ExecutionEvent event) { delegate.forkFailed(event); ProjectBuildLogAppender.setForkingProjectId(null); - ProjectBuildLogAppender.setForkingMojoId(null); } @Override diff --git a/impl/maven-core/src/main/java/org/apache/maven/logging/ProjectBuildLogAppender.java b/impl/maven-core/src/main/java/org/apache/maven/logging/ProjectBuildLogAppender.java index dc82a2f89848..03485b8759a0 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/logging/ProjectBuildLogAppender.java +++ b/impl/maven-core/src/main/java/org/apache/maven/logging/ProjectBuildLogAppender.java @@ -18,11 +18,29 @@ */ package org.apache.maven.logging; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.time.Instant; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Consumer; + +import org.apache.maven.api.MonotonicClock; +import org.apache.maven.api.build.report.LogEvent; +import org.apache.maven.api.build.report.LogLevel; +import org.apache.maven.internal.build.DefaultLogEvent; +import org.apache.maven.internal.impl.DefaultLog; +import org.apache.maven.slf4j.MavenJulHandler; import org.apache.maven.slf4j.MavenSimpleLogger; import org.slf4j.MDC; +import org.slf4j.spi.LocationAwareLogger; /** - * Forwards log messages to the client. + * Forwards log messages to the client as structured {@link LogEvent} objects. + *

+ * Installs itself as a {@link MavenSimpleLogger.LogSink} to intercept all + * SLF4J log output, enrich it with structured metadata (level, logger name, + * clean message, formatted output), and forward to the active + * {@link BuildEventListener}. */ public class ProjectBuildLogAppender implements AutoCloseable { @@ -31,7 +49,6 @@ public class ProjectBuildLogAppender implements AutoCloseable { private static final ThreadLocal PROJECT_ID = new InheritableThreadLocal<>(); private static final ThreadLocal MOJO_ID = new InheritableThreadLocal<>(); private static final ThreadLocal FORKING_PROJECT_ID = new InheritableThreadLocal<>(); - private static final ThreadLocal FORKING_MOJO_ID = new InheritableThreadLocal<>(); public static String getProjectId() { return PROJECT_ID.get(); @@ -61,8 +78,9 @@ public static String getMojoId() { /** * Sets or clears the mojo execution identifier in both the thread-local - * and the SLF4J MDC. When clearing ({@code null}), if a forking mojo ID - * was saved, it is restored — mirroring the fork-aware project ID pattern. + * and the SLF4J MDC. The value is available to any SLF4J appender via + * the MDC key {@code maven.mojo.id} and to JUL-bridged messages through + * the same MDC path. *

* Format: {@code "prefix:goal@executionId"} * (e.g. {@code "compiler:compile@default-compile"}). @@ -74,15 +92,8 @@ public static void setMojoId(String mojoId) { MOJO_ID.set(mojoId); MDC.put(KEY_MOJO_ID, mojoId); } else { - // Restore the forking mojo's ID if one was saved - String forkingMojoId = FORKING_MOJO_ID.get(); - if (forkingMojoId != null) { - MOJO_ID.set(forkingMojoId); - MDC.put(KEY_MOJO_ID, forkingMojoId); - } else { - MOJO_ID.remove(); - MDC.remove(KEY_MOJO_ID); - } + MOJO_ID.remove(); + MDC.remove(KEY_MOJO_ID); } } @@ -94,21 +105,6 @@ public static void setForkingProjectId(String forkingProjectId) { } } - /** - * Saves or clears the mojo ID of the forking mojo, so it can be - * restored when the fork completes. Mirrors the {@link #setForkingProjectId} - * pattern for project IDs. - * - * @param forkingMojoId the forking mojo identifier, or {@code null} to clear - */ - public static void setForkingMojoId(String forkingMojoId) { - if (forkingMojoId != null) { - FORKING_MOJO_ID.set(forkingMojoId); - } else { - FORKING_MOJO_ID.remove(); - } - } - public static void updateMdc() { String id = getProjectId(); if (id != null) { @@ -118,6 +114,32 @@ public static void updateMdc() { } } + /** + * Global sequence counter for total ordering of log events across + * all sources (Log API, JUL, direct SLF4J). Incremented atomically + * in {@link #accept} which is called synchronously on the logging thread. + */ + private static final AtomicLong SEQUENCE = new AtomicLong(); + + /** + * Callback for build report log capture. Receives the fully-formed + * {@link LogEvent} produced by {@link #accept}, eliminating the need + * for a second capture pipeline in {@code MavenSimpleLogger}. + *

+ * Set by {@code BuildReportCollector} at session start, cleared at + * session end. The callback runs synchronously on the logging thread. + */ + private static volatile Consumer reportCapture; + + /** + * Sets the report capture callback. + * + * @param capture the callback, or {@code null} to remove + */ + public static void setReportCapture(Consumer capture) { + ProjectBuildLogAppender.reportCapture = capture; + } + private final BuildEventListener buildEventListener; public ProjectBuildLogAppender(BuildEventListener buildEventListener) { @@ -125,13 +147,73 @@ public ProjectBuildLogAppender(BuildEventListener buildEventListener) { MavenSimpleLogger.setLogSink(this::accept); } - protected void accept(String message) { + protected void accept( + int level, String loggerName, String cleanMessage, String formattedMessage, Throwable throwable) { String projectId = MDC.get(KEY_PROJECT_ID); - buildEventListener.projectLogMessage(projectId, message); + Instant timestamp = MonotonicClock.now(); + LogLevel logLevel = toLogLevel(level); + String stackTrace = throwable != null ? formatStackTrace(throwable) : null; + + long seq = SEQUENCE.getAndIncrement(); + + // Read source metadata: JUL events carry it via MavenJulHandler, + // Log API events carry it via DefaultLog's ThreadLocal. + MavenJulHandler.JulMetadata julMeta = MavenJulHandler.getJulMetadata(); + DefaultLog.LogApiMetadata logApiMeta = DefaultLog.getLogApiMetadata(); + String sourceClassName; + String sourceMethodName; + long threadId; + if (julMeta != null) { + sourceClassName = julMeta.sourceClassName(); + sourceMethodName = julMeta.sourceMethodName(); + threadId = julMeta.threadId(); + } else if (logApiMeta != null) { + sourceClassName = logApiMeta.sourceClassName(); + sourceMethodName = logApiMeta.sourceMethodName(); + threadId = logApiMeta.threadId(); + } else { + sourceClassName = null; + sourceMethodName = null; + threadId = -1; + } + LogEvent event = new DefaultLogEvent( + timestamp, + logLevel, + cleanMessage, + loggerName, + stackTrace, + formattedMessage, + sourceClassName, + sourceMethodName, + threadId, + seq); + buildEventListener.projectLogMessage(projectId, event); + + // Forward to build report collector (if active) + Consumer capture = reportCapture; + if (capture != null) { + capture.accept(event); + } } @Override public void close() throws Exception { MavenSimpleLogger.setLogSink(null); } + + private static LogLevel toLogLevel(int level) { + return switch (level) { + case LocationAwareLogger.TRACE_INT -> LogLevel.TRACE; + case LocationAwareLogger.DEBUG_INT -> LogLevel.DEBUG; + case LocationAwareLogger.INFO_INT -> LogLevel.INFO; + case LocationAwareLogger.WARN_INT -> LogLevel.WARN; + default -> LogLevel.ERROR; + }; + } + + private static String formatStackTrace(Throwable t) { + StringWriter sw = new StringWriter(); + t.printStackTrace(new PrintWriter(sw)); + return sw.toString(); + } } diff --git a/impl/maven-core/src/main/java/org/apache/maven/logging/SimpleBuildEventListener.java b/impl/maven-core/src/main/java/org/apache/maven/logging/SimpleBuildEventListener.java index 87f7baa1fd59..37c49a92c1c4 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/logging/SimpleBuildEventListener.java +++ b/impl/maven-core/src/main/java/org/apache/maven/logging/SimpleBuildEventListener.java @@ -20,6 +20,7 @@ import java.util.function.Consumer; +import org.apache.maven.api.build.report.LogEvent; import org.apache.maven.execution.ExecutionEvent; import org.eclipse.aether.transfer.TransferEvent; @@ -38,8 +39,9 @@ public void sessionStarted(ExecutionEvent event) {} public void projectStarted(String projectId) {} @Override - public void projectLogMessage(String projectId, String event) { - log(event); + public void projectLogMessage(String projectId, LogEvent event) { + String formatted = event.formattedMessage(); + log(formatted != null ? formatted : event.message()); } @Override diff --git a/impl/maven-core/src/test/java/org/apache/maven/internal/build/BuildReportCollectorTest.java b/impl/maven-core/src/test/java/org/apache/maven/internal/build/BuildReportCollectorTest.java new file mode 100644 index 000000000000..8cffb11c30bf --- /dev/null +++ b/impl/maven-core/src/test/java/org/apache/maven/internal/build/BuildReportCollectorTest.java @@ -0,0 +1,414 @@ +/* + * 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 java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Properties; + +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.services.BuilderProblem; +import org.apache.maven.execution.BuildSuccess; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.ExecutionEvent; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionResult; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.impl.DefaultBuilderProblem; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.project.MavenProject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class BuildReportCollectorTest { + + @TempDir + Path tempDir; + + private BuildReportCollector collector; + + @BeforeEach + void setUp() { + collector = new BuildReportCollector(); + } + + @Test + void testBuildReportAssembly() { + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + MavenExecutionResult result = session.getResult(); + + // Simulate: session started → project started → mojo started → mojo succeeded → project succeeded → session + // ended + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, project, null)); + + MojoExecution mojo = createMojoExecution( + "org.apache.maven.plugins", "maven-compiler-plugin", "3.15.0", "compile", "default-compile", "compile"); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoStarted, session, project, mojo)); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoSucceeded, session, project, mojo)); + + // Record build success in the result + result.addBuildSummary(new BuildSuccess(project, 5000)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, project, null)); + + // Build the report + BuildReport report = collector.buildReport(session); + + assertNotNull(report); + assertEquals(BuildStatus.SUCCESS, report.status()); + assertEquals(1, report.formatVersion()); + assertEquals("1.0.0", report.mavenVersion()); + assertFalse(report.multiModule()); + assertEquals(1, report.threads()); + assertEquals(1, report.modules().size()); + assertEquals("org.example", report.modules().get(0).groupId()); + assertEquals("my-app", report.modules().get(0).artifactId()); + assertEquals(BuildStatus.SUCCESS, report.modules().get(0).status()); + assertEquals(1, report.modules().get(0).mojos().size()); + assertEquals("compile", report.modules().get(0).mojos().get(0).goal()); + assertEquals(BuildStatus.SUCCESS, report.modules().get(0).mojos().get(0).status()); + assertTrue(report.failures().isEmpty()); + } + + @Test + void testBuildReportWithFailure() { + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + MavenExecutionResult result = session.getResult(); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, project, null)); + + MojoExecution mojo = createMojoExecution( + "org.apache.maven.plugins", "maven-compiler-plugin", "3.15.0", "compile", "default-compile", "compile"); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoStarted, session, project, mojo)); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoFailed, session, project, mojo)); + + RuntimeException failure = new RuntimeException("Compilation failure: 3 errors"); + result.addBuildSummary(new org.apache.maven.execution.BuildFailure(project, 3000, failure)); + result.addException(failure); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectFailed, session, project, null)); + + BuildReport report = collector.buildReport(session); + + assertEquals(BuildStatus.FAILURE, report.status()); + assertEquals(1, report.failures().size()); + assertEquals("org.example:my-app:1.0.0", report.failures().get(0).module()); + assertTrue(report.failures().get(0).message().contains("Compilation failure")); + assertNotNull(report.failures().get(0).timestamp(), "failure should have a timestamp"); + assertEquals("RuntimeException", report.failures().get(0).exceptionType(), "exceptionType from cause"); + + // Navigate from failure → module → mojo using lookup methods + var failureReport = report.failures().get(0); + var moduleOpt = report.findModule(failureReport); + assertTrue(moduleOpt.isPresent(), "findModule(FailureReport) should find the module"); + assertEquals("my-app", moduleOpt.get().artifactId()); + assertEquals("org.example:my-app:1.0.0", moduleOpt.get().id()); + + assertNotNull(failureReport.mojo(), "failure should reference a mojo"); + var mojoOpt = moduleOpt.get().findMojo(failureReport.mojo()); + assertTrue(mojoOpt.isPresent(), "findMojo should find the failed mojo"); + assertEquals("compile", mojoOpt.get().goal()); + assertEquals(BuildStatus.FAILURE, mojoOpt.get().status()); + assertEquals("maven-compiler-plugin:3.15.0:compile", mojoOpt.get().id()); + } + + @Test + void testWriteReportToFile() throws IOException { + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + session.getResult().addBuildSummary(new BuildSuccess(project, 1000)); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, project, null)); + + BuildReport report = collector.buildReport(session); + collector.writeReport(report, session); + + Path reportsDir = tempDir.resolve("target").resolve(BuildReportCollector.REPORT_DIR); + Path latestFile = reportsDir.resolve(BuildReportCollector.REPORT_LATEST); + assertTrue(Files.exists(latestFile), "build-report-latest.json should exist"); + + String content = Files.readString(latestFile); + assertTrue(content.contains("\"formatVersion\": 1")); + assertTrue(content.contains("\"status\": \"SUCCESS\"")); + assertTrue(content.contains("\"artifactId\": \"my-app\"")); + } + + @Test + void testMultiModuleBuild() { + MavenProject parent = createProject("org.example", "parent", "1.0.0"); + MavenProject child1 = createProject("org.example", "child-api", "1.0.0"); + MavenProject child2 = createProject("org.example", "child-impl", "1.0.0"); + + MavenSession session = createSession(parent, child1, child2); + MavenExecutionResult result = session.getResult(); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, parent, null)); + + // Build each module + for (MavenProject p : List.of(parent, child1, child2)) { + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, p, null)); + result.addBuildSummary(new BuildSuccess(p, 1000)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, p, null)); + } + + BuildReport report = collector.buildReport(session); + + assertEquals(BuildStatus.SUCCESS, report.status()); + assertTrue(report.multiModule()); + assertEquals(3, report.modules().size()); + assertEquals("parent", report.modules().get(0).artifactId()); + assertEquals("child-api", report.modules().get(1).artifactId()); + assertEquals("child-impl", report.modules().get(2).artifactId()); + } + + // ---- Warning mode tests ---- + + @Test + void testWarningModeNoneSuppressesSummary() { + // Register a diagnostic so there's something to print + collector.getDiagnosticCollector().report(warning("test-key", "test warning", "test-source")); + + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + session.getUserProperties().setProperty("maven.build.warningMode", "none"); + session.getResult().addBuildSummary(new BuildSuccess(project, 1000)); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.SessionEnded, session, project, null)); + + // No exceptions means summary was suppressed (in "none" mode) + assertFalse(session.getResult().hasExceptions()); + } + + @Test + void testWarningModeFailAddsException() { + collector.getDiagnosticCollector().report(warning("test-key", "test warning", "test-source")); + + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + session.getUserProperties().setProperty("maven.build.warningMode", "fail"); + session.getResult().addBuildSummary(new BuildSuccess(project, 1000)); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.SessionEnded, session, project, null)); + + assertTrue(session.getResult().hasExceptions(), "Build should fail with --warning-mode=fail and warnings"); + assertTrue( + session.getResult().getExceptions().get(0).getMessage().contains("warning"), + "Exception message should mention warnings"); + } + + @Test + void testWarningModeFailNoWarningsNoException() { + // No warnings registered — fail mode should not add exception + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + session.getUserProperties().setProperty("maven.build.warningMode", "fail"); + session.getResult().addBuildSummary(new BuildSuccess(project, 1000)); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.SessionEnded, session, project, null)); + + assertFalse(session.getResult().hasExceptions(), "No warnings, no exception"); + } + + @Test + void testWarningModeSummaryIsDefault() { + collector.getDiagnosticCollector().report(warning("test-key", "test warning", "test-source")); + + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + // No warningMode property set — defaults to "summary" + session.getResult().addBuildSummary(new BuildSuccess(project, 1000)); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.SessionEnded, session, project, null)); + + // Summary mode should NOT fail the build + assertFalse(session.getResult().hasExceptions()); + } + + // ---- Synthetic diagnostic key tests ---- + + @Test + void testSyntheticDiagnosticKeyStripsPaths() { + String key1 = BuildReportCollector.syntheticDiagnosticKey("compiler", "Foo.java:42: unchecked cast"); + String key2 = BuildReportCollector.syntheticDiagnosticKey("compiler", "Bar.java:99: unchecked cast"); + assertEquals(key1, key2, "Same warning from different files should deduplicate"); + } + + @Test + void testSyntheticDiagnosticKeyDifferentMessages() { + String key1 = BuildReportCollector.syntheticDiagnosticKey("compiler", "unchecked cast"); + String key2 = BuildReportCollector.syntheticDiagnosticKey("compiler", "deprecated API"); + assertFalse(key1.equals(key2), "Different messages should produce different keys"); + } + + @Test + void testSyntheticDiagnosticKeyIncludesLoggerSuffix() { + String key = BuildReportCollector.syntheticDiagnosticKey( + "org.apache.maven.plugins.compiler.CompilerMojo", "unchecked cast"); + assertTrue(key.startsWith("auto:CompilerMojo:"), "Key should include short logger suffix"); + } + + // ---- Diagnostic suppression wiring tests ---- + + @Test + void testDiagnosticSuppressionFromUserProperty() { + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + session.getUserProperties().setProperty("maven.diagnostic.suppress", "key-a,key-b"); + session.getResult().addBuildSummary(new BuildSuccess(project, 1000)); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + + // Report diagnostics — key-a and key-b should be suppressed + collector.getDiagnosticCollector().report(warning("key-a", "warning a", null)); + collector.getDiagnosticCollector().report(warning("key-b", "warning b", null)); + collector.getDiagnosticCollector().report(warning("key-c", "warning c", null)); + + assertEquals(1, collector.getDiagnosticCollector().getProblems().size()); + assertEquals( + "key-c", collector.getDiagnosticCollector().getProblems().get(0).getKey()); + } + + @Test + void testDiagnosticSuppressionWildcard() { + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + session.getUserProperties().setProperty("maven.diagnostic.suppress", "auto:*"); + session.getResult().addBuildSummary(new BuildSuccess(project, 1000)); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + + collector.getDiagnosticCollector().report(warning("auto:Compiler:abc", "unchecked", null)); + collector.getDiagnosticCollector().report(warning("explicit-key", "explicit", null)); + + assertEquals(1, collector.getDiagnosticCollector().getProblems().size()); + assertEquals( + "explicit-key", + collector.getDiagnosticCollector().getProblems().get(0).getKey()); + } + + // ---- Test helpers ---- + + private static BuilderProblem warning(String key, String message, String source) { + return new DefaultBuilderProblem( + source, -1, -1, null, message, BuilderProblem.Severity.WARNING, key, null, null); + } + + private MavenProject createProject(String groupId, String artifactId, String version) { + MavenProject project = new MavenProject(); + project.setGroupId(groupId); + project.setArtifactId(artifactId); + project.setVersion(version); + return project; + } + + private MavenSession createSession(MavenProject... projects) { + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setStartInstant(MonotonicClock.now()); + request.setGoals(List.of("clean", "install")); + request.setTopDirectory(tempDir); + + Properties systemProperties = new Properties(); + systemProperties.setProperty("maven.version", "1.0.0"); + request.setSystemProperties(systemProperties); + + MavenExecutionResult result = new DefaultMavenExecutionResult(); + + @SuppressWarnings("deprecation") + MavenSession session = new MavenSession(null, null, request, result); + session.setProjects(List.of(projects)); + return session; + } + + private MojoExecution createMojoExecution( + String groupId, String artifactId, String version, String goal, String executionId, String phase) { + @SuppressWarnings("deprecation") + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + pluginDescriptor.setGroupId(groupId); + pluginDescriptor.setArtifactId(artifactId); + pluginDescriptor.setVersion(version); + + MojoDescriptor mojoDescriptor = new MojoDescriptor(); + mojoDescriptor.setGoal(goal); + mojoDescriptor.setPluginDescriptor(pluginDescriptor); + + MojoExecution execution = new MojoExecution(mojoDescriptor, executionId); + execution.setLifecyclePhase(phase); + + return execution; + } + + private ExecutionEvent createEvent( + ExecutionEvent.Type type, MavenSession session, MavenProject project, MojoExecution mojo) { + return new ExecutionEvent() { + @Override + public Type getType() { + return type; + } + + @Override + public MavenSession getSession() { + return session; + } + + @Override + public MavenProject getProject() { + return project; + } + + @Override + public MojoExecution getMojoExecution() { + return mojo; + } + + @Override + public Exception getException() { + return null; + } + }; + } +} \ No newline at end of file diff --git a/impl/maven-core/src/test/java/org/apache/maven/internal/build/BuildReportIntegrationTest.java b/impl/maven-core/src/test/java/org/apache/maven/internal/build/BuildReportIntegrationTest.java new file mode 100644 index 000000000000..7cf25141a42d --- /dev/null +++ b/impl/maven-core/src/test/java/org/apache/maven/internal/build/BuildReportIntegrationTest.java @@ -0,0 +1,359 @@ +/* + * 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 java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Properties; + +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.services.BuilderProblem; +import org.apache.maven.execution.BuildSuccess; +import org.apache.maven.execution.DefaultMavenExecutionRequest; +import org.apache.maven.execution.DefaultMavenExecutionResult; +import org.apache.maven.execution.ExecutionEvent; +import org.apache.maven.execution.MavenExecutionRequest; +import org.apache.maven.execution.MavenExecutionResult; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.impl.DefaultBuilderProblem; +import org.apache.maven.plugin.MojoExecution; +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.project.MavenProject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Integration test that exercises the full build report pipeline: + * BuildReportCollector → DefaultDiagnosticCollector → BuildReportJsonWriter → file. + *

+ * This test simulates a complete multi-module build lifecycle with mojo + * executions, problems, and failures, then verifies the resulting + * JSON report file contains all expected data. + */ +class BuildReportIntegrationTest { + + @TempDir + Path tempDir; + + private DefaultDiagnosticCollector diagnosticCollector; + private BuildReportCollector collector; + + @BeforeEach + void setUp() { + diagnosticCollector = new DefaultDiagnosticCollector(); + collector = new BuildReportCollector(diagnosticCollector); + } + + private static BuilderProblem warning(String key, String message, String source) { + return new DefaultBuilderProblem( + source, -1, -1, null, message, BuilderProblem.Severity.WARNING, key, null, null); + } + + private static BuilderProblem warning(String key, String message, String source, String suggestion, String docUrl) { + return new DefaultBuilderProblem( + source, -1, -1, null, message, BuilderProblem.Severity.WARNING, key, suggestion, docUrl); + } + + private static BuilderProblem info(String key, String message, String source) { + return new DefaultBuilderProblem(source, -1, -1, null, message, BuilderProblem.Severity.INFO, key, null, null); + } + + private static BuilderProblem error(String key, String message, String source) { + return new DefaultBuilderProblem(source, -1, -1, null, message, BuilderProblem.Severity.ERROR, key, null, null); + } + + /** + * Full lifecycle: multi-module build with mojos, problems, and JSON persistence. + */ + @Test + void testFullMultiModuleBuildWithProblems() throws IOException { + MavenProject parent = createProject("com.example", "parent", "2.0.0"); + MavenProject api = createProject("com.example", "api", "2.0.0"); + MavenProject impl = createProject("com.example", "impl", "2.0.0"); + + MavenSession session = createSession(parent, api, impl); + MavenExecutionResult result = session.getResult(); + + // Session starts + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, parent, null)); + + // --- Module: parent --- + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, parent, null)); + result.addBuildSummary(new BuildSuccess(parent, 500)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, parent, null)); + + // --- Module: api --- + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, api, null)); + + MojoExecution compileApi = createMojoExecution( + "org.apache.maven.plugins", "maven-compiler-plugin", "3.15.0", "compile", "default-compile", "compile"); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoStarted, session, api, compileApi)); + + // Report a deprecation warning during compile + diagnosticCollector.report(warning( + "deprecated-source-target", + "source/target value 8 is deprecated", + "maven-compiler-plugin:3.15.0:compile", + "Update to 11 or higher", + null)); + + collector.onEvent(createEvent(ExecutionEvent.Type.MojoSucceeded, session, api, compileApi)); + + MojoExecution testApi = createMojoExecution( + "org.apache.maven.plugins", "maven-surefire-plugin", "3.5.0", "test", "default-test", "test"); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoStarted, session, api, testApi)); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoSucceeded, session, api, testApi)); + + result.addBuildSummary(new BuildSuccess(api, 3000)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, api, null)); + + // --- Module: impl --- + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, impl, null)); + + MojoExecution compileImpl = createMojoExecution( + "org.apache.maven.plugins", "maven-compiler-plugin", "3.15.0", "compile", "default-compile", "compile"); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoStarted, session, impl, compileImpl)); + + // Same warning fires again in a different module — should be deduplicated + diagnosticCollector.report(warning("deprecated-source-target", "source/target value 8 is deprecated", "impl")); + + // Also report a unique info problem + diagnosticCollector.report(info("build-note", "Using JDK 21 toolchain", "toolchain")); + + collector.onEvent(createEvent(ExecutionEvent.Type.MojoSucceeded, session, impl, compileImpl)); + result.addBuildSummary(new BuildSuccess(impl, 2000)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, impl, null)); + + // --- Build report --- + BuildReport report = collector.buildReport(session); + + // Basic assertions + assertNotNull(report); + assertEquals(BuildStatus.SUCCESS, report.status()); + assertTrue(report.multiModule()); + assertEquals(3, report.modules().size()); + + // Problems in the report + assertEquals(2, report.problems().size()); + assertEquals("deprecated-source-target", report.problems().get(0).getKey()); + assertEquals(BuilderProblem.Severity.WARNING, report.problems().get(0).getSeverity()); + assertEquals("build-note", report.problems().get(1).getKey()); + assertEquals(BuilderProblem.Severity.INFO, report.problems().get(1).getSeverity()); + + // Summary should show count = 2 for the warning + assertEquals(2, diagnosticCollector.getSummary().get(0).count()); + assertEquals(1, diagnosticCollector.getSummary().get(1).count()); + + // Module reports + assertEquals("parent", report.modules().get(0).artifactId()); + assertEquals("api", report.modules().get(1).artifactId()); + assertEquals(2, report.modules().get(1).mojos().size()); + assertEquals("compile", report.modules().get(1).mojos().get(0).goal()); + assertEquals("test", report.modules().get(1).mojos().get(1).goal()); + assertEquals("impl", report.modules().get(2).artifactId()); + + // Write to JSON and verify + collector.writeReport(report, session); + + Path reportsDir = tempDir.resolve("target").resolve(BuildReportCollector.REPORT_DIR); + Path latestFile = reportsDir.resolve(BuildReportCollector.REPORT_LATEST); + assertTrue(Files.exists(latestFile), "build-report-latest.json should exist"); + + String json = Files.readString(latestFile); + + // Verify JSON structure + assertTrue(json.contains("\"formatVersion\": 1")); + assertTrue(json.contains("\"status\": \"SUCCESS\"")); + assertTrue(json.contains("\"multiModule\": true")); + assertTrue(json.contains("\"threads\": 1")); + + // Modules in JSON + assertTrue(json.contains("\"artifactId\": \"parent\"")); + assertTrue(json.contains("\"artifactId\": \"api\"")); + assertTrue(json.contains("\"artifactId\": \"impl\"")); + + // Mojos in JSON + assertTrue(json.contains("\"goal\": \"compile\"")); + assertTrue(json.contains("\"goal\": \"test\"")); + + // Problems in JSON + assertTrue(json.contains("\"problems\": [")); + assertTrue(json.contains("\"key\": \"deprecated-source-target\"")); + assertTrue(json.contains("\"severity\": \"WARNING\"")); + assertTrue(json.contains("\"suggestion\": \"Update to 11 or higher\"")); + assertTrue(json.contains("\"key\": \"build-note\"")); + assertTrue(json.contains("\"severity\": \"INFO\"")); + + // Failures should be empty + assertTrue(json.contains("\"failures\": []")); + + // Output arrays should be present (even if empty in unit test — no SLF4J sink) + assertTrue(json.contains("\"output\": [")); + } + + /** + * Tests that the problem summary printing doesn't throw when there are no problems. + */ + @Test + void testDiagnosticSummaryWithNoProblems() { + // Should be a no-op, not throw + collector.printDiagnosticSummary(); + } + + /** + * Tests that the problem summary printing handles mixed severities. + */ + @Test + void testDiagnosticSummaryWithMixedSeverities() { + diagnosticCollector.report(warning("warn-1", "first warning", null)); + diagnosticCollector.report(warning("warn-1", "first warning (dup)", null)); + diagnosticCollector.report(error("err-1", "first error", null)); + diagnosticCollector.report(info("info-1", "info note", null)); + + // Should not throw + collector.printDiagnosticSummary(); + + assertTrue(diagnosticCollector.hasWarnings()); + assertTrue(diagnosticCollector.hasErrors()); + } + + /** + * Tests navigation methods on the built report. + */ + @Test + void testReportNavigationMethods() { + MavenProject project = createProject("org.example", "my-app", "1.0.0"); + MavenSession session = createSession(project); + MavenExecutionResult result = session.getResult(); + + collector.onEvent(createEvent(ExecutionEvent.Type.SessionStarted, session, project, null)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectStarted, session, project, null)); + + MojoExecution mojo = createMojoExecution( + "org.apache.maven.plugins", "maven-compiler-plugin", "3.15.0", "compile", "default-compile", "compile"); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoStarted, session, project, mojo)); + collector.onEvent(createEvent(ExecutionEvent.Type.MojoSucceeded, session, project, mojo)); + + result.addBuildSummary(new BuildSuccess(project, 5000)); + collector.onEvent(createEvent(ExecutionEvent.Type.ProjectSucceeded, session, project, null)); + + BuildReport report = collector.buildReport(session); + + // findModule by GAV + var moduleOpt = report.findModule("org.example:my-app:1.0.0"); + assertTrue(moduleOpt.isPresent()); + assertEquals("my-app", moduleOpt.get().artifactId()); + assertEquals("org.example:my-app:1.0.0", moduleOpt.get().id()); + + // findMojo by id + var mojoOpt = moduleOpt.get().findMojo("maven-compiler-plugin:3.15.0:compile"); + assertTrue(mojoOpt.isPresent()); + assertEquals("compile", mojoOpt.get().goal()); + + // Not found + assertFalse(report.findModule("nonexistent:module:1.0").isPresent()); + } + + // ---- Test helpers ---- + + private MavenProject createProject(String groupId, String artifactId, String version) { + MavenProject project = new MavenProject(); + project.setGroupId(groupId); + project.setArtifactId(artifactId); + project.setVersion(version); + return project; + } + + private MavenSession createSession(MavenProject... projects) { + MavenExecutionRequest request = new DefaultMavenExecutionRequest(); + request.setStartInstant(MonotonicClock.now()); + request.setGoals(List.of("clean", "install")); + request.setTopDirectory(tempDir); + + Properties systemProperties = new Properties(); + systemProperties.setProperty("maven.version", "4.1.0-SNAPSHOT"); + request.setSystemProperties(systemProperties); + + MavenExecutionResult result = new DefaultMavenExecutionResult(); + + @SuppressWarnings("deprecation") + MavenSession session = new MavenSession(null, null, request, result); + session.setProjects(List.of(projects)); + return session; + } + + private MojoExecution createMojoExecution( + String groupId, String artifactId, String version, String goal, String executionId, String phase) { + @SuppressWarnings("deprecation") + PluginDescriptor pluginDescriptor = new PluginDescriptor(); + pluginDescriptor.setGroupId(groupId); + pluginDescriptor.setArtifactId(artifactId); + pluginDescriptor.setVersion(version); + + MojoDescriptor mojoDescriptor = new MojoDescriptor(); + mojoDescriptor.setGoal(goal); + mojoDescriptor.setPluginDescriptor(pluginDescriptor); + + MojoExecution execution = new MojoExecution(mojoDescriptor, executionId); + execution.setLifecyclePhase(phase); + + return execution; + } + + private ExecutionEvent createEvent( + ExecutionEvent.Type type, MavenSession session, MavenProject project, MojoExecution mojo) { + return new ExecutionEvent() { + @Override + public Type getType() { + return type; + } + + @Override + public MavenSession getSession() { + return session; + } + + @Override + public MavenProject getProject() { + return project; + } + + @Override + public MojoExecution getMojoExecution() { + return mojo; + } + + @Override + public Exception getException() { + return null; + } + }; + } +} \ No newline at end of file diff --git a/impl/maven-core/src/test/java/org/apache/maven/internal/build/BuildReportJsonWriterTest.java b/impl/maven-core/src/test/java/org/apache/maven/internal/build/BuildReportJsonWriterTest.java new file mode 100644 index 000000000000..9c28632b2171 --- /dev/null +++ b/impl/maven-core/src/test/java/org/apache/maven/internal/build/BuildReportJsonWriterTest.java @@ -0,0 +1,486 @@ +/* + * 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 java.time.Duration; +import java.time.Instant; +import java.util.List; + +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.LogLevel; +import org.apache.maven.api.build.report.ModuleReport; +import org.apache.maven.api.build.report.MojoReport; +import org.apache.maven.api.services.BuilderProblem; +import org.apache.maven.impl.DefaultBuilderProblem; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class BuildReportJsonWriterTest { + + private static final Instant BASE_TIME = Instant.parse("2025-01-15T10:30:00Z"); + + @Test + void testSuccessfulBuildReport() { + List mojoOutput = List.of( + new DefaultLogEvent( + BASE_TIME.plusSeconds(6), LogLevel.INFO, "Compiling 42 source files", "o.a.m.compiler", null), + new DefaultLogEvent(BASE_TIME.plusSeconds(7), LogLevel.INFO, "BUILD SUCCESS", "o.a.m.compiler", null)); + + MojoReport mojo = new DefaultMojoReport( + "org.apache.maven.plugins", + "maven-compiler-plugin", + "3.15.0", + "compile", + "default-compile", + "compile", + BuildStatus.SUCCESS, + BASE_TIME.plusSeconds(5), + Duration.ofMillis(2100), + mojoOutput); + + List moduleOutput = List.of(new DefaultLogEvent( + BASE_TIME.plusSeconds(2), + LogLevel.INFO, + "Resolving dependencies for maven-core", + "o.a.m.resolver", + null)); + + ModuleReport module = new DefaultModuleReport( + "org.apache.maven", + "maven-core", + "4.1.0-SNAPSHOT", + BuildStatus.SUCCESS, + BASE_TIME.plusSeconds(1), + Duration.ofMillis(12345), + List.of(mojo), + moduleOutput); + + List buildOutput = List.of( + new DefaultLogEvent(BASE_TIME, LogLevel.INFO, "Reactor Build Order:", "o.a.m.reactor", null), + new DefaultLogEvent(BASE_TIME, LogLevel.INFO, "Maven Core", "o.a.m.reactor", null)); + + BuildReport report = new DefaultBuildReport( + BuildStatus.SUCCESS, + Duration.ofMillis(30000), + BASE_TIME, + "4.1.0-SNAPSHOT", + "21.0.1", + List.of("clean", "install"), + "org.apache.maven:maven:4.1.0-SNAPSHOT", + true, + 4, + List.of(module), + List.of(), + List.of(), + buildOutput); + + String json = BuildReportJsonWriter.toJson(report); + + assertTrue(json.contains("\"formatVersion\": 1")); + assertTrue(json.contains("\"status\": \"SUCCESS\"")); + assertTrue(json.contains("\"mavenVersion\": \"4.1.0-SNAPSHOT\"")); + assertTrue(json.contains("\"javaVersion\": \"21.0.1\"")); + assertTrue(json.contains("\"goals\": [\"clean\", \"install\"]")); + assertTrue(json.contains("\"multiModule\": true")); + assertTrue(json.contains("\"threads\": 4")); + assertTrue(json.contains("\"groupId\": \"org.apache.maven\"")); + assertTrue(json.contains("\"artifactId\": \"maven-core\"")); + assertTrue(json.contains("\"goal\": \"compile\"")); + assertTrue(json.contains("\"executionId\": \"default-compile\"")); + assertTrue(json.contains("\"failures\": []")); + // Module and mojo start times + assertTrue(json.contains("\"startTime\": \"2025-01-15T10:30:01Z\""), "module startTime"); + assertTrue(json.contains("\"startTime\": \"2025-01-15T10:30:05Z\""), "mojo startTime"); + // Structured log events at all three levels + assertTrue(json.contains("\"message\": \"Compiling 42 source files\""), "mojo-level log event"); + assertTrue(json.contains("\"message\": \"Resolving dependencies for maven-core\""), "module-level log event"); + assertTrue(json.contains("\"message\": \"Reactor Build Order:\""), "build-level log event"); + // Log event structure + assertTrue(json.contains("\"level\": \"INFO\""), "log level"); + assertTrue(json.contains("\"loggerName\": \"o.a.m.compiler\""), "logger name"); + } + + @Test + void testFailedBuildReport() { + FailureReport failure = new DefaultFailureReport( + "org.apache.maven:maven-core:4.1.0-SNAPSHOT", + "maven-compiler-plugin:3.15.0:compile", + BASE_TIME.plusSeconds(3), + "CompilationFailureException", + "Compilation failure: 3 errors", + "org.apache.maven.plugin.compiler.CompilationFailureException: ...\n\tat ...\n"); + + BuildReport report = new DefaultBuildReport( + BuildStatus.FAILURE, + Duration.ofMillis(5000), + BASE_TIME, + "4.1.0-SNAPSHOT", + "21.0.1", + List.of("compile"), + "org.apache.maven:maven-core:4.1.0-SNAPSHOT", + false, + 1, + List.of(), + List.of(failure), + List.of(), + List.of()); + + String json = BuildReportJsonWriter.toJson(report); + + assertTrue(json.contains("\"status\": \"FAILURE\"")); + assertTrue(json.contains("\"module\": \"org.apache.maven:maven-core:4.1.0-SNAPSHOT\"")); + assertTrue(json.contains("\"mojo\": \"maven-compiler-plugin:3.15.0:compile\"")); + assertTrue(json.contains("\"message\": \"Compilation failure: 3 errors\"")); + assertTrue(json.contains("\"stackTrace\"")); + // New enriched fields + assertTrue(json.contains("\"timestamp\": \"2025-01-15T10:30:03Z\""), "failure timestamp"); + assertTrue(json.contains("\"exceptionType\": \"CompilationFailureException\""), "failure exceptionType"); + } + + @Test + void testJsonStringEscaping() { + FailureReport failure = new DefaultFailureReport( + "com.example:test:1.0", + null, + BASE_TIME, + null, + "Error: \"unexpected\" value\nwith newline\tand tab", + null); + + BuildReport report = new DefaultBuildReport( + BuildStatus.FAILURE, + Duration.ofMillis(100), + BASE_TIME, + "4.1.0", + "21", + List.of(), + "com.example:test:1.0", + false, + 1, + List.of(), + List.of(failure), + List.of(), + List.of()); + + String json = BuildReportJsonWriter.toJson(report); + + // Check proper JSON escaping + assertTrue(json.contains("\\\"unexpected\\\"")); + assertTrue(json.contains("\\n")); + assertTrue(json.contains("\\t")); + } + + @Test + void testEmptyModulesAndFailures() { + BuildReport report = new DefaultBuildReport( + BuildStatus.SUCCESS, + Duration.ofMillis(100), + BASE_TIME, + "4.1.0", + "21", + List.of(), + "com.example:test:1.0", + false, + 1, + List.of(), + List.of(), + List.of(), + List.of()); + + String json = BuildReportJsonWriter.toJson(report); + + assertTrue(json.contains("\"modules\": []")); + assertTrue(json.contains("\"failures\": []")); + } + + @Test + void testFormatVersion() { + BuildReport report = new DefaultBuildReport( + BuildStatus.SUCCESS, + Duration.ZERO, + BASE_TIME, + "4.1.0", + "21", + List.of(), + "test:test:1.0", + false, + 1, + List.of(), + List.of(), + List.of(), + List.of()); + + assertEquals(1, report.formatVersion()); + } + + @Test + void testMultipleModules() { + ModuleReport mod1 = new DefaultModuleReport( + "com.example", + "api", + "1.0", + BuildStatus.SUCCESS, + BASE_TIME.plusSeconds(1), + Duration.ofSeconds(5), + List.of(), + List.of()); + ModuleReport mod2 = new DefaultModuleReport( + "com.example", + "impl", + "1.0", + BuildStatus.SUCCESS, + BASE_TIME.plusSeconds(6), + Duration.ofSeconds(10), + List.of(), + List.of()); + ModuleReport mod3 = new DefaultModuleReport( + "com.example", + "web", + "1.0", + BuildStatus.SKIPPED, + BASE_TIME.plusSeconds(16), + Duration.ZERO, + List.of(), + List.of()); + + BuildReport report = new DefaultBuildReport( + BuildStatus.SUCCESS, + Duration.ofSeconds(15), + BASE_TIME, + "4.1.0", + "21", + List.of("install"), + "com.example:parent:1.0", + true, + 1, + List.of(mod1, mod2, mod3), + List.of(), + List.of(), + List.of()); + + String json = BuildReportJsonWriter.toJson(report); + + // All modules present + assertTrue(json.contains("\"artifactId\": \"api\"")); + assertTrue(json.contains("\"artifactId\": \"impl\"")); + assertTrue(json.contains("\"artifactId\": \"web\"")); + assertTrue(json.contains("\"status\": \"SKIPPED\"")); + } + + @Test + void testNullMojoFields() { + // mojo with null executionId and phase (direct invocation) + MojoReport mojo = new DefaultMojoReport( + "org.apache.maven.plugins", + "maven-help-plugin", + "3.4.1", + "effective-pom", + null, + null, + BuildStatus.SUCCESS, + BASE_TIME.plusSeconds(1), + Duration.ofMillis(500), + List.of()); + + String json = BuildReportJsonWriter.toJson(new DefaultBuildReport( + BuildStatus.SUCCESS, + Duration.ofSeconds(1), + BASE_TIME, + "4.1.0", + "21", + List.of("help:effective-pom"), + "test:test:1.0", + false, + 1, + List.of(new DefaultModuleReport( + "test", + "test", + "1.0", + BuildStatus.SUCCESS, + BASE_TIME, + Duration.ofSeconds(1), + List.of(mojo), + List.of())), + List.of(), + List.of(), + List.of())); + + assertTrue(json.contains("\"executionId\": null")); + assertTrue(json.contains("\"phase\": null")); + assertFalse(json.contains("\"executionId\": \"null\"")); + } + + @Test + void testLogEventWithJulMetadata() { + // LogEvent with JUL source class, method, thread ID, and sequence number + LogEvent julEvent = new DefaultLogEvent( + BASE_TIME.plusSeconds(1), + LogLevel.WARN, + "Unsupported class file major version 65", + "org.apache.maven.plugins.compiler", + null, + null, + "com.sun.tools.javac.processing.JavacProcessingEnvironment", + "doProcessing", + 42L, + 1001L); + + // LogEvent without JUL metadata (from SLF4J) + LogEvent slf4jEvent = new DefaultLogEvent( + BASE_TIME.plusSeconds(2), LogLevel.INFO, "Compiling 10 files", "o.a.m.compiler", null); + + MojoReport mojo = new DefaultMojoReport( + "org.apache.maven.plugins", + "maven-compiler-plugin", + "3.15.0", + "compile", + "default-compile", + "compile", + BuildStatus.SUCCESS, + BASE_TIME, + Duration.ofMillis(1000), + List.of(julEvent, slf4jEvent)); + + BuildReport report = new DefaultBuildReport( + BuildStatus.SUCCESS, + Duration.ofSeconds(1), + BASE_TIME, + "4.1.0", + "21", + List.of("compile"), + "test:test:1.0", + false, + 1, + List.of(new DefaultModuleReport( + "test", + "test", + "1.0", + BuildStatus.SUCCESS, + BASE_TIME, + Duration.ofSeconds(1), + List.of(mojo), + List.of())), + List.of(), + List.of(), + List.of()); + + String json = BuildReportJsonWriter.toJson(report); + + // JUL event should have sourceClassName, sourceMethodName, threadId, and sequenceNumber + assertTrue( + json.contains("\"sourceClassName\": \"com.sun.tools.javac.processing.JavacProcessingEnvironment\""), + "sourceClassName"); + assertTrue(json.contains("\"sourceMethodName\": \"doProcessing\""), "sourceMethodName"); + assertTrue(json.contains("\"threadId\": 42"), "threadId"); + assertTrue(json.contains("\"sequenceNumber\": 1001"), "sequenceNumber"); + // SLF4J event should NOT have JUL fields + // (the second log event in the output array has no sourceClassName) + assertFalse( + json.contains("\"sourceClassName\": \"o.a.m.compiler\""), + "SLF4J event should not have sourceClassName"); + } + + @Test + void testProblemsSerialization() { + BuilderProblem p1 = new DefaultBuilderProblem( + "maven-compiler-plugin:3.15.0:compile", + 42, + 1, + null, + "source/target value 8 is deprecated", + BuilderProblem.Severity.WARNING, + "deprecated-source-target", + "Update to 11 or higher", + "https://example.com/docs/compiler"); + + BuilderProblem p2 = new DefaultBuilderProblem( + "maven-compiler-plugin", + -1, + -1, + null, + "3 errors found", + BuilderProblem.Severity.ERROR, + "compilation-failure", + null, + null); + + BuildReport report = new DefaultBuildReport( + BuildStatus.FAILURE, + Duration.ofSeconds(5), + BASE_TIME, + "4.1.0-SNAPSHOT", + "21.0.1", + List.of("compile"), + "com.example:test:1.0", + false, + 1, + List.of(), + List.of(), + List.of(p1, p2), + List.of()); + + String json = BuildReportJsonWriter.toJson(report); + + // Problem structure + assertTrue(json.contains("\"problems\": ["), "problems array present"); + assertTrue(json.contains("\"key\": \"deprecated-source-target\""), "problem key"); + assertTrue(json.contains("\"severity\": \"WARNING\""), "problem severity"); + assertTrue(json.contains("\"message\": \"source/target value 8 is deprecated\""), "problem message"); + assertTrue(json.contains("\"source\": \"maven-compiler-plugin:3.15.0:compile\""), "problem source"); + assertTrue(json.contains("\"line\": 42"), "problem line"); + assertTrue(json.contains("\"column\": 1"), "problem column"); + assertTrue( + json.contains("\"suggestion\": \"Update to 11 or higher\""), + "problem suggestion"); + assertTrue( + json.contains("\"documentationUrl\": \"https://example.com/docs/compiler\""), + "problem documentationUrl"); + + // Second problem (minimal fields) + assertTrue(json.contains("\"key\": \"compilation-failure\""), "second problem key"); + assertTrue(json.contains("\"severity\": \"ERROR\""), "second problem severity"); + } + + @Test + void testEmptyProblems() { + BuildReport report = new DefaultBuildReport( + BuildStatus.SUCCESS, + Duration.ofMillis(100), + BASE_TIME, + "4.1.0", + "21", + List.of(), + "com.example:test:1.0", + false, + 1, + List.of(), + List.of(), + List.of(), + List.of()); + + String json = BuildReportJsonWriter.toJson(report); + assertTrue(json.contains("\"problems\": []"), "empty problems array"); + } +} \ No newline at end of file diff --git a/impl/maven-core/src/test/java/org/apache/maven/internal/build/DefaultDiagnosticCollectorTest.java b/impl/maven-core/src/test/java/org/apache/maven/internal/build/DefaultDiagnosticCollectorTest.java new file mode 100644 index 000000000000..c4c938de03e7 --- /dev/null +++ b/impl/maven-core/src/test/java/org/apache/maven/internal/build/DefaultDiagnosticCollectorTest.java @@ -0,0 +1,311 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.apache.maven.api.services.BuilderProblem; +import org.apache.maven.impl.DefaultBuilderProblem; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DefaultDiagnosticCollectorTest { + + private DefaultDiagnosticCollector collector; + + @BeforeEach + void setUp() { + collector = new DefaultDiagnosticCollector(); + } + + private static BuilderProblem warning(String key, String message, String source) { + return new DefaultBuilderProblem( + source, -1, -1, null, message, BuilderProblem.Severity.WARNING, key, null, null); + } + + private static BuilderProblem error(String key, String message, String source) { + return new DefaultBuilderProblem(source, -1, -1, null, message, BuilderProblem.Severity.ERROR, key, null, null); + } + + private static BuilderProblem info(String key, String message, String source) { + return new DefaultBuilderProblem(source, -1, -1, null, message, BuilderProblem.Severity.INFO, key, null, null); + } + + @Test + void testEmptyCollector() { + assertTrue(collector.getProblems().isEmpty()); + assertTrue(collector.getSummary().isEmpty()); + assertFalse(collector.hasWarnings()); + assertFalse(collector.hasErrors()); + } + + @Test + void testSingleWarning() { + BuilderProblem p = warning("deprecated-source", "source 8 is deprecated", "compiler:3.15.0"); + collector.report(p); + + assertEquals(1, collector.getProblems().size()); + assertEquals("deprecated-source", collector.getProblems().get(0).getKey()); + assertTrue(collector.hasWarnings()); + assertFalse(collector.hasErrors()); + } + + @Test + void testSingleError() { + BuilderProblem p = error("compilation-failure", "3 errors found", "compiler:3.15.0"); + collector.report(p); + + assertEquals(1, collector.getProblems().size()); + assertTrue(collector.hasWarnings()); // errors are >= WARNING severity + assertTrue(collector.hasErrors()); + } + + @Test + void testInfoDoesNotCountAsWarningOrError() { + BuilderProblem p = info("build-summary", "Build completed", "reactor"); + collector.report(p); + + assertEquals(1, collector.getProblems().size()); + assertFalse(collector.hasWarnings()); + assertFalse(collector.hasErrors()); + } + + @Test + void testDeduplicationByKey() { + BuilderProblem d1 = warning("deprecated-source", "source 8 is deprecated", "module-a"); + BuilderProblem d2 = warning("deprecated-source", "source 8 is deprecated", "module-b"); + BuilderProblem d3 = warning("deprecated-source", "source 8 is deprecated", "module-c"); + + collector.report(d1); + collector.report(d2); + collector.report(d3); + + // Only one unique problem + assertEquals(1, collector.getProblems().size()); + assertEquals("deprecated-source", collector.getProblems().get(0).getKey()); + + // But the summary shows count = 3 + List summary = collector.getSummary(); + assertEquals(1, summary.size()); + assertEquals(3, summary.get(0).count()); + } + + @Test + void testMultipleDistinctProblems() { + collector.report(warning("deprecated-source", "source 8 is deprecated", "compiler")); + collector.report(warning("unused-dep", "unused dependency: guava", "dependency")); + collector.report(error("test-failure", "2 tests failed", "surefire")); + + assertEquals(3, collector.getProblems().size()); + assertTrue(collector.hasWarnings()); + assertTrue(collector.hasErrors()); + + List summary = collector.getSummary(); + assertEquals(3, summary.size()); + // Each reported once + for (DefaultDiagnosticSummary s : summary) { + assertEquals(1, s.count()); + } + } + + @Test + void testInsertionOrderPreserved() { + collector.report(warning("c-warning", "third", null)); + collector.report(warning("a-warning", "first", null)); + collector.report(warning("b-warning", "second", null)); + + List problems = collector.getProblems(); + assertEquals("c-warning", problems.get(0).getKey()); + assertEquals("a-warning", problems.get(1).getKey()); + assertEquals("b-warning", problems.get(2).getKey()); + } + + @Test + void testProblemsListIsUnmodifiable() { + collector.report(warning("test", "test", null)); + List problems = collector.getProblems(); + + try { + problems.add(warning("another", "another", null)); + // Should not reach here + assertFalse(true, "Expected UnsupportedOperationException"); + } catch (UnsupportedOperationException e) { + // expected + } + } + + @Test + void testSummaryListIsUnmodifiable() { + collector.report(warning("test", "test", null)); + List summary = collector.getSummary(); + + try { + summary.add(new DefaultDiagnosticSummary(warning("x", "x", null), 1)); + assertFalse(true, "Expected UnsupportedOperationException"); + } catch (UnsupportedOperationException e) { + // expected + } + } + + @Test + void testFullProblemFields() { + BuilderProblem p = new DefaultBuilderProblem( + "maven-compiler-plugin:3.15.0:compile", + 42, + 15, + null, + "unchecked cast from Object to List", + BuilderProblem.Severity.WARNING, + "unchecked-cast", + "Add @SuppressWarnings(\"unchecked\") or use a type-safe alternative", + "https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html"); + + collector.report(p); + + BuilderProblem stored = collector.getProblems().get(0); + assertEquals("unchecked-cast", stored.getKey()); + assertEquals(BuilderProblem.Severity.WARNING, stored.getSeverity()); + assertEquals("unchecked cast from Object to List", stored.getMessage()); + assertEquals("maven-compiler-plugin:3.15.0:compile", stored.getSource()); + assertEquals(42, stored.getLineNumber()); + assertEquals(15, stored.getColumnNumber()); + assertEquals("Add @SuppressWarnings(\"unchecked\") or use a type-safe alternative", stored.getSuggestion()); + assertEquals( + "https://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html", stored.getDocumentationUrl()); + } + + // ---- Suppression tests ---- + + @Test + void testSuppressionByExactKey() { + collector.setSuppressedKeys(Set.of("deprecated-source")); + + collector.report(warning("deprecated-source", "source 8 is deprecated", "compiler")); + collector.report(warning("unused-dep", "unused dependency: guava", "dependency")); + + assertEquals(1, collector.getProblems().size()); + assertEquals("unused-dep", collector.getProblems().get(0).getKey()); + } + + @Test + void testSuppressionByPrefixWildcard() { + collector.setSuppressedKeys(Set.of("auto:*")); + + collector.report(warning("auto:Compiler:1a2b3c", "unchecked cast", "compiler")); + collector.report(warning("auto:Surefire:4d5e6f", "deprecated API", "surefire")); + collector.report(warning("explicit-key", "some warning", "plugin")); + + assertEquals(1, collector.getProblems().size()); + assertEquals("explicit-key", collector.getProblems().get(0).getKey()); + } + + @Test + void testSuppressionMultipleKeys() { + collector.setSuppressedKeys(Set.of("key-a", "key-b")); + + collector.report(warning("key-a", "warning a", null)); + collector.report(warning("key-b", "warning b", null)); + collector.report(warning("key-c", "warning c", null)); + + assertEquals(1, collector.getProblems().size()); + assertEquals("key-c", collector.getProblems().get(0).getKey()); + } + + @Test + void testSuppressionDoesNotAffectCounts() { + collector.setSuppressedKeys(Set.of("suppressed")); + + // Report suppressed key — should be silently dropped, no count + collector.report(warning("suppressed", "suppressed warning", null)); + collector.report(warning("kept", "kept warning", null)); + + assertEquals(1, collector.getSummary().size()); + assertEquals("kept", collector.getSummary().get(0).problem().getKey()); + assertEquals(1, collector.getSummary().get(0).count()); + } + + @Test + void testEmptySuppressionSetAllowsAll() { + collector.setSuppressedKeys(Set.of()); + + collector.report(warning("key-a", "warning a", null)); + collector.report(warning("key-b", "warning b", null)); + + assertEquals(2, collector.getProblems().size()); + } + + @Test + void testConcurrentReporting() throws Exception { + int threadCount = 8; + int reportsPerThread = 100; + ExecutorService executor = Executors.newFixedThreadPool(threadCount); + CountDownLatch startLatch = new CountDownLatch(1); + + List> futures = new ArrayList<>(); + for (int t = 0; t < threadCount; t++) { + int threadId = t; + futures.add(executor.submit(() -> { + try { + startLatch.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + for (int i = 0; i < reportsPerThread; i++) { + // Half use a shared key (will be deduped), half use unique keys + if (i % 2 == 0) { + collector.report(warning("shared-key", "shared warning", "thread-" + threadId)); + } else { + collector.report( + warning("unique-" + threadId + "-" + i, "unique warning " + i, "thread-" + threadId)); + } + } + })); + } + + startLatch.countDown(); + for (Future f : futures) { + f.get(); + } + executor.shutdown(); + + // "shared-key" should be deduplicated to 1 entry + // Unique keys: 8 threads * 50 unique = 400 + // Total unique = 401 + int expectedUniqueKeys = 1 + (threadCount * (reportsPerThread / 2)); + assertEquals(expectedUniqueKeys, collector.getProblems().size()); + + // shared-key should have count = 8 threads * 50 = 400 + DefaultDiagnosticSummary sharedSummary = collector.getSummary().stream() + .filter(s -> "shared-key".equals(s.problem().getKey())) + .findFirst() + .orElseThrow(); + assertEquals(threadCount * (reportsPerThread / 2), sharedSummary.count()); + } +} \ No newline at end of file diff --git a/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultLogTest.java b/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultLogTest.java deleted file mode 100644 index eb290734fb98..000000000000 --- a/impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultLogTest.java +++ /dev/null @@ -1,196 +0,0 @@ -/* - * 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.impl; - -import org.apache.maven.api.plugin.Log; -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotSame; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -/** - * Tests for {@link DefaultLog}. - */ -class DefaultLogTest { - - /** - * Regression test: {@code warn(Supplier, Throwable)} was incorrectly - * calling {@code logger.info()} instead of {@code logger.warn()}. - */ - @Test - void warnWithSupplierAndThrowableDelegatesToWarn() { - Logger mockLogger = mock(Logger.class); - when(mockLogger.isWarnEnabled()).thenReturn(true); - - DefaultLog log = new DefaultLog(mockLogger); - RuntimeException ex = new RuntimeException("test"); - log.warn(() -> "warning message", ex); - - verify(mockLogger).warn("warning message", ex); - } - - /** - * Verify trace methods delegate to the SLF4J logger correctly. - */ - @Test - void traceMethodsDelegateToSlf4jTrace() { - Logger mockLogger = mock(Logger.class); - when(mockLogger.isTraceEnabled()).thenReturn(true); - - DefaultLog log = new DefaultLog(mockLogger); - log.trace("trace message"); - - verify(mockLogger).trace("trace message"); - } - - /** - * Verify that trace methods are no-ops when trace is disabled. - */ - @Test - void traceIsNoOpWhenDisabled() { - Logger mockLogger = mock(Logger.class); - when(mockLogger.isTraceEnabled()).thenReturn(false); - - DefaultLog log = new DefaultLog(mockLogger); - log.trace("should not be logged"); - - verify(mockLogger).isTraceEnabled(); - verifyNoMoreInteractions(mockLogger); - } - - /** - * Verify that {@code child()} creates a new logger with the - * expected hierarchical name. - */ - @Test - void childCreatesSubLogger() { - Logger mockLogger = mock(Logger.class); - when(mockLogger.getName()).thenReturn("org.apache.maven.plugins.compiler.CompilerMojo"); - - DefaultLog parent = new DefaultLog(mockLogger); - Log child = parent.child("diagnostics"); - - assertNotSame(parent, child); - assertTrue(child instanceof DefaultLog, "child should be a DefaultLog"); - } - - /** - * Verify that the default {@code Log.isTraceEnabled()} returns false, - * preventing {@code AbstractMethodError} for third-party implementors. - */ - @Test - void defaultTraceIsDisabled() { - // Use a minimal Log implementation that relies on defaults - Log minimal = new Log() { - @Override - public boolean isDebugEnabled() { - return false; - } - - @Override - public void debug(CharSequence c) {} - - @Override - public void debug(CharSequence c, Throwable e) {} - - @Override - public void debug(Throwable e) {} - - @Override - public void debug(java.util.function.Supplier c) {} - - @Override - public void debug(java.util.function.Supplier c, Throwable e) {} - - @Override - public boolean isInfoEnabled() { - return false; - } - - @Override - public void info(CharSequence c) {} - - @Override - public void info(CharSequence c, Throwable e) {} - - @Override - public void info(Throwable e) {} - - @Override - public void info(java.util.function.Supplier c) {} - - @Override - public void info(java.util.function.Supplier c, Throwable e) {} - - @Override - public boolean isWarnEnabled() { - return false; - } - - @Override - public void warn(CharSequence c) {} - - @Override - public void warn(CharSequence c, Throwable e) {} - - @Override - public void warn(Throwable e) {} - - @Override - public void warn(java.util.function.Supplier c) {} - - @Override - public void warn(java.util.function.Supplier c, Throwable e) {} - - @Override - public boolean isErrorEnabled() { - return false; - } - - @Override - public void error(CharSequence c) {} - - @Override - public void error(CharSequence c, Throwable e) {} - - @Override - public void error(Throwable e) {} - - @Override - public void error(java.util.function.Supplier c) {} - - @Override - public void error(java.util.function.Supplier c, Throwable e) {} - }; - - // These should NOT throw AbstractMethodError — they use defaults - assertEquals(false, minimal.isTraceEnabled()); - minimal.trace("should be a no-op"); - minimal.trace("no-op", new RuntimeException()); - minimal.trace(new RuntimeException()); - minimal.trace(() -> "no-op"); - minimal.trace(() -> "no-op", new RuntimeException()); - } -} diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultBuilderProblem.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultBuilderProblem.java index bf223cc0daa1..eb746c88dee7 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultBuilderProblem.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultBuilderProblem.java @@ -25,22 +25,42 @@ * thrown or a simple string message. In addition, a problem carries a hint about its source, e.g. the settings file * that exhibits the problem. */ -class DefaultBuilderProblem implements BuilderProblem { +public class DefaultBuilderProblem implements BuilderProblem { final String source; final int lineNumber; final int columnNumber; final Exception exception; final String message; final Severity severity; + final String key; + final String suggestion; + final String documentationUrl; - DefaultBuilderProblem( + public DefaultBuilderProblem( String source, int lineNumber, int columnNumber, Exception exception, String message, Severity severity) { + this(source, lineNumber, columnNumber, exception, message, severity, null, null, null); + } + + @SuppressWarnings("checkstyle:ParameterNumber") + public DefaultBuilderProblem( + String source, + int lineNumber, + int columnNumber, + Exception exception, + String message, + Severity severity, + String key, + String suggestion, + String documentationUrl) { this.source = source; this.lineNumber = lineNumber; this.columnNumber = columnNumber; this.exception = exception; this.message = message; this.severity = severity; + this.key = key; + this.suggestion = suggestion; + this.documentationUrl = documentationUrl; } @Override @@ -73,6 +93,21 @@ public Severity getSeverity() { return severity; } + @Override + public String getKey() { + return key; + } + + @Override + public String getSuggestion() { + return suggestion; + } + + @Override + public String getDocumentationUrl() { + return documentationUrl; + } + @Override public String getLocation() { StringBuilder buffer = new StringBuilder(256); @@ -108,4 +143,4 @@ public String toString() { } return buffer.toString(); } -} +} \ No newline at end of file diff --git a/impl/maven-jline/src/main/java/org/apache/maven/jline/JLineMessageBuilderFactory.java b/impl/maven-jline/src/main/java/org/apache/maven/jline/JLineMessageBuilderFactory.java index cfae3c000c3a..f02504809cad 100644 --- a/impl/maven-jline/src/main/java/org/apache/maven/jline/JLineMessageBuilderFactory.java +++ b/impl/maven-jline/src/main/java/org/apache/maven/jline/JLineMessageBuilderFactory.java @@ -136,7 +136,18 @@ private MavenStyleResolver() { @Override public AttributedStyle resolve(String spec) { - return styles.computeIfAbsent(spec, this::doResolve); + try { + return styles.computeIfAbsent(spec, this::doResolve); + } catch (IllegalStateException e) { + // ConcurrentHashMap.computeIfAbsent throws IllegalStateException("Recursive update") + // when the same map is re-entered from within a computeIfAbsent call on the same + // thread. This can happen during FastTerminal initialization: JLine's StyleResolver + // logs via JUL, MavenJulHandler routes to SLF4J, MavenSimpleLogger.renderLevel() + // lazily initialises styled level strings by calling style() → resolve() here, + // re-entering the same computeIfAbsent. Fall back to DEFAULT for this event; + // subsequent calls will hit the populated cache and succeed normally. + return AttributedStyle.DEFAULT; + } } @Override diff --git a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java index 20a5e7ab666b..6faeba5feacd 100644 --- a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java +++ b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenBaseLogger.java @@ -232,6 +232,23 @@ protected void write(StringBuilder buf, Throwable t) { } } + /** + * Context-aware write that includes the log level, logger name, and + * clean message alongside the formatted output. Subclasses can override + * to forward structured data to a log sink. + *

+ * The default implementation delegates to {@link #write(StringBuilder, Throwable)}. + * + * @param level the SLF4J log level constant + * @param loggerName the name of the logger + * @param cleanMessage the formatted message without level/timestamp prefix + * @param formattedBuf the fully formatted log line + * @param t the throwable, may be {@code null} + */ + protected void write(int level, String loggerName, String cleanMessage, StringBuilder formattedBuf, Throwable t) { + write(formattedBuf, t); + } + protected void writeThrowable(Throwable t, PrintStream targetStream) { if (t != null) { t.printStackTrace(targetStream); @@ -375,7 +392,7 @@ private void innerHandleNormalizedLoggingCall( // Append the message buf.append(formattedMessage); - write(buf, t); + write(level.toInt(), name, formattedMessage, buf, t); } protected String renderLevel(int levelInt) { diff --git a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenJulHandler.java b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenJulHandler.java new file mode 100644 index 000000000000..9be299faf0fd --- /dev/null +++ b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenJulHandler.java @@ -0,0 +1,249 @@ +/* + * 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.slf4j; + +import java.text.MessageFormat; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogManager; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import org.apache.maven.api.services.MessageBuilder; +import org.slf4j.LoggerFactory; +import org.slf4j.spi.LocationAwareLogger; + +import static org.apache.maven.jline.MessageUtils.builder; + +/** + * A JUL {@link Handler} that routes {@code java.util.logging} events into + * Maven's structured logging pipeline, preserving the rich {@link LogRecord} + * metadata that the standard {@code SLF4JBridgeHandler} silently drops + * (source class name, source method name, thread ID). + *

+ * When a {@link MavenSimpleLogger.LogSink LogSink} is installed (i.e. during + * a build), JUL events are sent directly to the sink — bypassing SLF4J + * entirely. The JUL metadata is stashed in a thread-local so downstream + * consumers (e.g. {@code ProjectBuildLogAppender}) can read it when + * constructing a structured {@code LogEvent}. + *

+ * When no LogSink is installed (e.g. during early bootstrap), the handler + * falls back to routing through SLF4J for console output. + *

+ * Usage — replace the standard SLF4J bridge in {@code LookupInvoker}: + *

+ *     MavenJulHandler.install();
+ * 
+ * + * @since 4.1.0 + * @see #install() + * @see #getJulMetadata() + */ +public class MavenJulHandler extends Handler { + + /** + * JUL metadata captured from a {@link LogRecord} that would otherwise + * be lost when bridging to SLF4J. + * + * @param sourceClassName the source class, or {@code null} + * @param sourceMethodName the source method, or {@code null} + * @param threadId the originating thread ID + */ + public record JulMetadata(String sourceClassName, String sourceMethodName, long threadId) {} + + private static final ThreadLocal METADATA = new ThreadLocal<>(); + + /** + * Returns the JUL metadata for the current log event being processed, + * or {@code null} if the current log event did not originate from JUL. + *

+ * This method is intended to be called from within a + * {@link MavenSimpleLogger.LogSink} callback (e.g. in + * {@code ProjectBuildLogAppender.accept()}). + * + * @return the current JUL metadata, or {@code null} + */ + public static JulMetadata getJulMetadata() { + return METADATA.get(); + } + + /** + * Installs this handler on the JUL root logger, removing any + * previously installed handlers. This replaces the standard + * {@code SLF4JBridgeHandler.install()} call. + */ + public static void install() { + Logger rootLogger = LogManager.getLogManager().getLogger(""); + // Remove all existing handlers (including any SLF4JBridgeHandler) + for (Handler handler : rootLogger.getHandlers()) { + rootLogger.removeHandler(handler); + } + rootLogger.addHandler(new MavenJulHandler()); + // Accept all levels — filtering is done by SLF4J + rootLogger.setLevel(Level.ALL); + } + + /** + * Returns {@code true} if a {@code MavenJulHandler} is installed + * on the JUL root logger. + */ + public static boolean isInstalled() { + Logger rootLogger = LogManager.getLogManager().getLogger(""); + for (Handler handler : rootLogger.getHandlers()) { + if (handler instanceof MavenJulHandler) { + return true; + } + } + return false; + } + + @Override + public void publish(LogRecord record) { + if (record == null) { + return; + } + + String loggerName = record.getLoggerName(); + org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger(loggerName); + int slf4jLevel = julLevelToSlf4j(record.getLevel()); + + // Quick exit if this level is not enabled + if (!isLevelEnabled(slf4jLogger, slf4jLevel)) { + return; + } + + String message = formatMessage(record); + Throwable throwable = record.getThrown(); + + // If a LogSink is installed, bypass SLF4J entirely: call the sink + // directly with the JUL metadata so no information is lost in transit. + MavenSimpleLogger.LogSink sink = MavenSimpleLogger.getLogSink(); + if (sink != null) { + METADATA.set(new JulMetadata( + record.getSourceClassName(), record.getSourceMethodName(), record.getLongThreadID())); + try { + String formatted = formatForConsole(slf4jLevel, message); + sink.accept(slf4jLevel, loggerName, message, formatted, throwable); + } finally { + METADATA.remove(); + } + } else { + // No LogSink — fall through to SLF4J for console output + logToSlf4j(slf4jLogger, slf4jLevel, message, throwable); + } + } + + @Override + public void flush() { + // nothing to flush + } + + @Override + public void close() throws SecurityException { + // nothing to close + } + + /** + * Formats the log message, applying i18n resource bundle lookup and + * {@link MessageFormat} parameter substitution, matching the behavior + * of {@code SLF4JBridgeHandler}. + */ + private static String formatMessage(LogRecord record) { + String message = record.getMessage(); + if (message == null) { + return ""; + } + + // Try resource bundle lookup + ResourceBundle bundle = record.getResourceBundle(); + if (bundle != null) { + try { + message = bundle.getString(message); + } catch (MissingResourceException e) { + // use raw message + } + } + + // Apply MessageFormat parameters + Object[] params = record.getParameters(); + if (params != null && params.length > 0) { + try { + message = MessageFormat.format(message, params); + } catch (IllegalArgumentException e) { + // use message as-is if formatting fails + } + } + + return message; + } + + /** + * Formats a JUL message for console output, matching the {@code [LEVEL] message} + * style used by MavenSimpleLogger with ANSI coloring when available. + */ + private static String formatForConsole(int level, String message) { + MessageBuilder mb = builder(); + String levelStr = + switch (level) { + case LocationAwareLogger.TRACE_INT -> mb.trace("TRACE").build(); + case LocationAwareLogger.DEBUG_INT -> mb.debug("DEBUG").build(); + case LocationAwareLogger.INFO_INT -> mb.info("INFO").build(); + case LocationAwareLogger.WARN_INT -> mb.warning("WARNING").build(); + default -> mb.error("ERROR").build(); + }; + return "[" + levelStr + "] " + message; + } + + private static int julLevelToSlf4j(Level julLevel) { + int value = julLevel.intValue(); + if (value <= Level.FINEST.intValue()) { + return LocationAwareLogger.TRACE_INT; + } else if (value <= Level.FINE.intValue()) { + return LocationAwareLogger.DEBUG_INT; + } else if (value <= Level.INFO.intValue()) { + return LocationAwareLogger.INFO_INT; + } else if (value <= Level.WARNING.intValue()) { + return LocationAwareLogger.WARN_INT; + } else { + return LocationAwareLogger.ERROR_INT; + } + } + + private static boolean isLevelEnabled(org.slf4j.Logger logger, int level) { + return switch (level) { + case LocationAwareLogger.TRACE_INT -> logger.isTraceEnabled(); + case LocationAwareLogger.DEBUG_INT -> logger.isDebugEnabled(); + case LocationAwareLogger.INFO_INT -> logger.isInfoEnabled(); + case LocationAwareLogger.WARN_INT -> logger.isWarnEnabled(); + default -> logger.isErrorEnabled(); + }; + } + + private static void logToSlf4j(org.slf4j.Logger logger, int level, String message, Throwable throwable) { + switch (level) { + case LocationAwareLogger.TRACE_INT -> logger.trace(message, throwable); + case LocationAwareLogger.DEBUG_INT -> logger.debug(message, throwable); + case LocationAwareLogger.INFO_INT -> logger.info(message, throwable); + case LocationAwareLogger.WARN_INT -> logger.warn(message, throwable); + default -> logger.error(message, throwable); + } + } +} diff --git a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenSimpleLogger.java b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenSimpleLogger.java index 02767987e2a1..77fc8b1eb846 100644 --- a/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenSimpleLogger.java +++ b/impl/maven-logging/src/main/java/org/apache/maven/slf4j/MavenSimpleLogger.java @@ -39,14 +39,45 @@ public class MavenSimpleLogger extends MavenBaseLogger { private String warnRenderedLevel; private String errorRenderedLevel; - static Consumer logSink; + /** + * Structured log sink that receives level, logger name, clean message, + * formatted console output, and throwable for each log event. + *

+ * This replaces the previous {@code Consumer} sink to enable + * console renderers (e.g. rich mode) to filter by log level and access + * the clean message independently of ANSI formatting. + * + * @since 4.1.0 + */ + @FunctionalInterface + public interface LogSink { + void accept(int level, String loggerName, String cleanMessage, String formattedMessage, Throwable throwable); + } + + static volatile LogSink logSink; public static final String DEFAULT_LOG_LEVEL_KEY = "org.slf4j.simpleLogger.defaultLogLevel"; - public static void setLogSink(Consumer logSink) { + /** + * Sets the structured log sink. + * + * @param logSink the sink, or {@code null} to remove + * @since 4.1.0 + */ + public static void setLogSink(LogSink logSink) { MavenSimpleLogger.logSink = logSink; } + /** + * Returns the current log sink, or {@code null} if none is set. + * + * @return the current log sink, or {@code null} + * @since 4.1.0 + */ + public static LogSink getLogSink() { + return logSink; + } + MavenSimpleLogger(String name) { super(name); } @@ -70,15 +101,74 @@ protected String renderLevel(int level) { } @Override - protected void write(StringBuilder buf, Throwable t) { - Consumer sink = logSink; + protected void write(int level, String loggerName, String cleanMessage, StringBuilder formattedBuf, Throwable t) { + LogSink sink = logSink; if (sink != null) { - sink.accept(buf.toString()); + // Build the full formatted output including throwable rendering + String formatted = formattedBuf.toString(); if (t != null) { - writeThrowable(t, sink); + StringBuilder full = new StringBuilder(formatted); + full.append(System.lineSeparator()); + appendFormattedThrowable(full, t, ""); + formatted = full.toString(); } + sink.accept(level, loggerName, cleanMessage, formatted, t); } else { - super.write(buf, t); + super.write(formattedBuf, t); + } + } + + /** + * Append a colorized throwable rendering to the given builder. + * Reuses the existing formatting logic for consistency with console output. + */ + private void appendFormattedThrowable(StringBuilder sb, Throwable t, String prefix) { + MessageBuilder builder = builder().a(prefix).failure(t.getClass().getName()); + if (t.getMessage() != null) { + builder.a(": ").failure(t.getMessage()); + } + sb.append(builder.toString()).append(System.lineSeparator()); + appendStackTrace(sb, t, prefix); + } + + private void appendStackTrace(StringBuilder sb, Throwable t, String prefix) { + MessageBuilder builder = builder(); + for (StackTraceElement e : t.getStackTrace()) { + builder.a(prefix); + builder.a(" "); + builder.strong("at"); + builder.a(" "); + builder.a(e.getClassName()); + builder.a("."); + builder.a(e.getMethodName()); + builder.a("("); + builder.strong(getLocation(e)); + builder.a(")"); + sb.append(builder.toString()).append(System.lineSeparator()); + builder.setLength(0); + } + for (Throwable se : t.getSuppressed()) { + builder.a(prefix) + .a(" ") + .strong("Suppressed") + .a(": ") + .a(se.getClass().getName()); + if (se.getMessage() != null) { + builder.a(": ").failure(se.getMessage()); + } + sb.append(builder.toString()).append(System.lineSeparator()); + builder.setLength(0); + appendStackTrace(sb, se, prefix + " "); + } + Throwable cause = t.getCause(); + if (cause != null && t != cause) { + builder.a(prefix).strong("Caused by").a(": ").a(cause.getClass().getName()); + if (cause.getMessage() != null) { + builder.a(": ").failure(cause.getMessage()); + } + sb.append(builder.toString()).append(System.lineSeparator()); + builder.setLength(0); + appendStackTrace(sb, cause, prefix); } } diff --git a/pom.xml b/pom.xml index d88cb7ea0d0f..7f2268eb406b 100644 --- a/pom.xml +++ b/pom.xml @@ -157,7 +157,6 @@ under the License. 1.37 6.1.3 1.4.0 - 1.6.3 5.23.0 1.6.0 1.30.0 @@ -508,17 +507,6 @@ under the License. ${slf4jVersion} true - - org.slf4j - jul-to-slf4j - ${slf4jVersion} - - - ch.qos.logback - logback-classic - ${logbackClassicVersion} - true - org.apache.maven.wagon