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

slachiewicz pushed a commit to branch maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-3.9.x by this push:
     new 0242b1faf6 [maven-3.9.x] Expose the Maven 4 logging API to plugins
0242b1faf6 is described below

commit 0242b1faf6e13cc04bc82d5547e05cf3595054da
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Nov 21 15:32:46 2026 +0100

    [maven-3.9.x] Expose the Maven 4 logging API to plugins
    
    Fixes #13917.
---
 .../main/java/org/apache/maven/api/Constants.java  | 133 ++++++++++
 .../main/java/org/apache/maven/api/Service.java    |  34 +++
 .../apache/maven/api/annotations/Experimental.java |  34 +++
 .../org/apache/maven/api/annotations/Nonnull.java  |  40 +++
 .../apache/maven/api/annotations/ThreadSafe.java   |  37 +++
 .../apache/maven/api/services/MessageBuilder.java  | 274 +++++++++++++++++++++
 .../maven/api/services/MessageBuilderFactory.java  |  58 +++++
 .../internal/message/ApiMessageBuilderFactory.java | 264 ++++++++++++++++++++
 .../main/resources/META-INF/maven/extension.xml    |   4 +
 .../ApiMessageBuilderFactoryBindingTest.java       |  50 ++++
 .../message/ApiMessageBuilderFactoryTest.java      |  78 ++++++
 11 files changed, 1006 insertions(+)

diff --git a/maven-core/src/main/java/org/apache/maven/api/Constants.java 
b/maven-core/src/main/java/org/apache/maven/api/Constants.java
new file mode 100644
index 0000000000..3f7c5ca4d1
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/api/Constants.java
@@ -0,0 +1,133 @@
+/*
+ * 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;
+
+/**
+ * Configuration constants.
+ * <p>
+ * Maven 3 carries only the style names of the Maven 4 class, for {@link 
org.apache.maven.api.services.MessageBuilder}.
+ *
+ * @since 3.9.17
+ */
+public final class Constants {
+
+    public static final String MAVEN_STYLE_PREFIX = "maven.style.";
+
+    // Style Names
+    public static final String MAVEN_STYLE_TRANSFER_NAME = "transfer";
+    public static final String MAVEN_STYLE_TRACE_NAME = "trace";
+    public static final String MAVEN_STYLE_DEBUG_NAME = "debug";
+    public static final String MAVEN_STYLE_INFO_NAME = "info";
+    public static final String MAVEN_STYLE_WARNING_NAME = "warning";
+    public static final String MAVEN_STYLE_ERROR_NAME = "error";
+    public static final String MAVEN_STYLE_SUCCESS_NAME = "success";
+    public static final String MAVEN_STYLE_FAILURE_NAME = "failure";
+    public static final String MAVEN_STYLE_STRONG_NAME = "strong";
+    public static final String MAVEN_STYLE_MOJO_NAME = "mojo";
+    public static final String MAVEN_STYLE_PROJECT_NAME = "project";
+
+    // Default Values
+    public static final String MAVEN_STYLE_TRANSFER_DEFAULT = "f:bright-black";
+    public static final String MAVEN_STYLE_TRACE_DEFAULT = "bold,f:magenta";
+    public static final String MAVEN_STYLE_DEBUG_DEFAULT = "bold,f:cyan";
+    public static final String MAVEN_STYLE_INFO_DEFAULT = "bold,f:blue";
+    public static final String MAVEN_STYLE_WARNING_DEFAULT = "bold,f:yellow";
+    public static final String MAVEN_STYLE_ERROR_DEFAULT = "bold,f:red";
+    public static final String MAVEN_STYLE_SUCCESS_DEFAULT = "bold,f:green";
+    public static final String MAVEN_STYLE_FAILURE_DEFAULT = "bold,f:red";
+    public static final String MAVEN_STYLE_STRONG_DEFAULT = "bold";
+    public static final String MAVEN_STYLE_MOJO_DEFAULT = "f:green";
+    public static final String MAVEN_STYLE_PROJECT_DEFAULT = "f:cyan";
+
+    /**
+     * Maven output color mode.
+     * Allowed values are <code>auto</code>, <code>always</code>, 
<code>never</code>.
+     *
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_COLOR_PROPERTY = MAVEN_STYLE_PREFIX 
+ "color";
+
+    /**
+     * Color style for transfer messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_TRANSFER = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_TRANSFER_NAME;
+
+    /**
+     * Color style for trace messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_TRACE = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_TRACE_NAME;
+
+    /**
+     * Color style for debug messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_DEBUG = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_DEBUG_NAME;
+
+    /**
+     * Color style for info messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_INFO = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_INFO_NAME;
+
+    /**
+     * Color style for warning messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_WARNING = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_WARNING_NAME;
+
+    /**
+     * Color style for error messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_ERROR = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_ERROR_NAME;
+
+    /**
+     * Color style for success messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_SUCCESS = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_SUCCESS_NAME;
+
+    /**
+     * Color style for failure messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_FAILURE = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_FAILURE_NAME;
+
+    /**
+     * Color style for strong messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_STRONG = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_STRONG_NAME;
+
+    /**
+     * Color style for mojo messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_MOJO = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_MOJO_NAME;
+
+    /**
+     * Color style for project messages.
+     * @since 3.9.17
+     */
+    public static final String MAVEN_STYLE_PROJECT = MAVEN_STYLE_PREFIX + 
MAVEN_STYLE_PROJECT_NAME;
+
+    private Constants() {}
+}
diff --git a/maven-core/src/main/java/org/apache/maven/api/Service.java 
b/maven-core/src/main/java/org/apache/maven/api/Service.java
new file mode 100644
index 0000000000..115d39480a
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/api/Service.java
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+import org.apache.maven.api.annotations.Experimental;
+import org.apache.maven.api.annotations.ThreadSafe;
+
+/**
+ * Marker interface for all services provided by the {@code Session}.
+ * <p>
+ * Services can be retrieved from the session using the
+ * {@code Session#getService(Class)} method.
+ *
+ * @since 3.9.17
+ */
+@Experimental
+@ThreadSafe
+public interface Service {}
diff --git 
a/maven-core/src/main/java/org/apache/maven/api/annotations/Experimental.java 
b/maven-core/src/main/java/org/apache/maven/api/annotations/Experimental.java
new file mode 100644
index 0000000000..4635a134d0
--- /dev/null
+++ 
b/maven-core/src/main/java/org/apache/maven/api/annotations/Experimental.java
@@ -0,0 +1,34 @@
+/*
+ * 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.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * This annotation tags types that are part of an experimental API.
+ * Classes or methods annotated with this annotation may be changed / removed 
without notice.
+ *
+ * @since 3.9.17
+ */
+@Experimental
+@Documented
+@Retention(RetentionPolicy.CLASS)
+public @interface Experimental {}
diff --git 
a/maven-core/src/main/java/org/apache/maven/api/annotations/Nonnull.java 
b/maven-core/src/main/java/org/apache/maven/api/annotations/Nonnull.java
new file mode 100644
index 0000000000..b84bd56d90
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/api/annotations/Nonnull.java
@@ -0,0 +1,40 @@
+/*
+ * 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.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * The annotated element must not be null.
+ * <p>
+ * Annotated fields must not be null after construction has completed.
+ * <p>
+ * When this annotation is applied to a method it applies to the method return 
value.
+ *
+ * @since 3.9.17
+ */
+@Experimental
+@Documented
+@Retention(RetentionPolicy.CLASS)
+@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
+public @interface Nonnull {}
diff --git 
a/maven-core/src/main/java/org/apache/maven/api/annotations/ThreadSafe.java 
b/maven-core/src/main/java/org/apache/maven/api/annotations/ThreadSafe.java
new file mode 100644
index 0000000000..50835e4c0c
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/api/annotations/ThreadSafe.java
@@ -0,0 +1,37 @@
+/*
+ * 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.annotations;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * The {@code ThreadSafe} annotation can be used to indicate a given type
+ * is thread safe.  {@code Immutable} objects are automatically thread safe.
+ *
+ * @since 3.9.17
+ */
+@Experimental
+@Documented
+@Retention(RetentionPolicy.CLASS)
+@Target(ElementType.TYPE)
+public @interface ThreadSafe {}
diff --git 
a/maven-core/src/main/java/org/apache/maven/api/services/MessageBuilder.java 
b/maven-core/src/main/java/org/apache/maven/api/services/MessageBuilder.java
new file mode 100644
index 0000000000..d7f125bf68
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/api/services/MessageBuilder.java
@@ -0,0 +1,274 @@
+/*
+ * 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.Constants;
+import org.apache.maven.api.annotations.Nonnull;
+
+/**
+ * Message builder that supports configurable styling.
+ *
+ * @since 3.9.17
+ * @see MessageBuilderFactory
+ */
+public interface MessageBuilder extends Appendable {
+
+    /**
+     * Append message content in trace style.
+     * By default, bold magenta
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder trace(Object message) {
+        return style("." + Constants.MAVEN_STYLE_TRACE_NAME + ":-" + 
Constants.MAVEN_STYLE_TRACE_DEFAULT, message);
+    }
+
+    /**
+     * Append message content in debug style.
+     * By default, bold cyan
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder debug(Object message) {
+        return style("." + Constants.MAVEN_STYLE_DEBUG_NAME + ":-" + 
Constants.MAVEN_STYLE_DEBUG_DEFAULT, message);
+    }
+
+    /**
+     * Append message content in info style.
+     * By default, bold blue
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder info(Object message) {
+        return style("." + Constants.MAVEN_STYLE_INFO_NAME + ":-" + 
Constants.MAVEN_STYLE_INFO_DEFAULT, message);
+    }
+
+    /**
+     * Append message content in warning style.
+     * By default, bold yellow
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder warning(Object message) {
+        return style("." + Constants.MAVEN_STYLE_WARNING_NAME + ":-" + 
Constants.MAVEN_STYLE_WARNING_DEFAULT, message);
+    }
+
+    /**
+     * Append message content in error style.
+     * By default, bold red
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder error(Object message) {
+        return style("." + Constants.MAVEN_STYLE_ERROR_NAME + ":-" + 
Constants.MAVEN_STYLE_ERROR_DEFAULT, message);
+    }
+
+    /**
+     * Append message content in success style.
+     * By default, bold green
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder success(Object message) {
+        return style("." + Constants.MAVEN_STYLE_SUCCESS_NAME + ":-" + 
Constants.MAVEN_STYLE_SUCCESS_DEFAULT, message);
+    }
+
+    /**
+     * Append message content in failure style.
+     * By default, bold red
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder failure(Object message) {
+        return style("." + Constants.MAVEN_STYLE_FAILURE_NAME + ":-" + 
Constants.MAVEN_STYLE_FAILURE_DEFAULT, message);
+    }
+
+    /**
+     * Append message content in strong style.
+     * By default, bold
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder strong(Object message) {
+        return style("." + Constants.MAVEN_STYLE_STRONG_NAME + ":-" + 
Constants.MAVEN_STYLE_STRONG_DEFAULT, message);
+    }
+
+    /**
+     * Append message content in mojo style.
+     * By default, green
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder mojo(Object message) {
+        return style("." + Constants.MAVEN_STYLE_MOJO_NAME + ":-" + 
Constants.MAVEN_STYLE_MOJO_DEFAULT, message);
+    }
+
+    /**
+     * Append message content in project style.
+     * By default, cyan
+     *
+     * @param message the message to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder project(Object message) {
+        return style("." + Constants.MAVEN_STYLE_PROJECT_NAME + ":-" + 
Constants.MAVEN_STYLE_PROJECT_DEFAULT, message);
+    }
+
+    @Nonnull
+    default MessageBuilder style(String style, Object message) {
+        return style(style).a(message).resetStyle();
+    }
+
+    MessageBuilder style(String style);
+
+    MessageBuilder resetStyle();
+
+    //
+    // message building methods modelled after Ansi methods
+    //
+
+    @Nonnull
+    @Override
+    MessageBuilder append(CharSequence cs);
+
+    @Nonnull
+    @Override
+    MessageBuilder append(CharSequence cs, int start, int end);
+
+    @Nonnull
+    @Override
+    MessageBuilder append(char c);
+
+    /**
+     * Append content to the message buffer.
+     *
+     * @param value the content to append
+     * @param offset the index of the first {@code char} to append
+     * @param len the number of {@code char}s to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder a(char[] value, int offset, int len) {
+        return append(String.valueOf(value, offset, len));
+    }
+
+    /**
+     * Append content to the message buffer.
+     *
+     * @param value the content to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder a(char[] value) {
+        return append(String.valueOf(value));
+    }
+
+    /**
+     * Append content to the message buffer.
+     *
+     * @param value the content to append
+     * @param start the starting index of the subsequence to be appended
+     * @param end the end index of the subsequence to be appended
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder a(CharSequence value, int start, int end) {
+        return append(value, start, end);
+    }
+
+    /**
+     * Append content to the message buffer.
+     *
+     * @param value the content to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder a(CharSequence value) {
+        return append(value);
+    }
+
+    /**
+     * Append content to the message buffer.
+     *
+     * @param value the content to append
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder a(Object value) {
+        return append(String.valueOf(value));
+    }
+
+    /**
+     * Append newline to the message buffer.
+     *
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder newline() {
+        return append(System.lineSeparator());
+    }
+
+    /**
+     * Append formatted content to the buffer.
+     * @see String#format(String, Object...)
+     *
+     * @param pattern a <a href="../util/Formatter.html#syntax">format 
string</a>
+     * @param args arguments referenced by the format specifiers in the format 
string
+     * @return the current builder
+     */
+    @Nonnull
+    default MessageBuilder format(String pattern, Object... args) {
+        return append(String.format(pattern, args));
+    }
+
+    /**
+     * Set the buffer length.
+     *
+     * @param length the new length
+     * @return the current builder
+     */
+    MessageBuilder setLength(int length);
+
+    /**
+     * Return the built message.
+     *
+     * @return the message
+     */
+    @Nonnull
+    String build();
+}
diff --git 
a/maven-core/src/main/java/org/apache/maven/api/services/MessageBuilderFactory.java
 
b/maven-core/src/main/java/org/apache/maven/api/services/MessageBuilderFactory.java
new file mode 100644
index 0000000000..bba0e78124
--- /dev/null
+++ 
b/maven-core/src/main/java/org/apache/maven/api/services/MessageBuilderFactory.java
@@ -0,0 +1,58 @@
+/*
+ * 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;
+
+/**
+ * A factory for {@link MessageBuilder}.
+ *
+ * @since 3.9.17
+ */
+@Experimental
+public interface MessageBuilderFactory extends Service {
+    /**
+     * Checks if the underlying output does support styling or not.
+     * @return whether color styling is supported or not
+     */
+    boolean isColorEnabled();
+
+    /**
+     * Returns the terminal width or <code>-1</code> if not supported.
+     * @return the terminal width
+     */
+    int getTerminalWidth();
+
+    /**
+     * Creates a new message builder.
+     * @return a new message builder
+     */
+    @Nonnull
+    MessageBuilder builder();
+
+    /**
+     * Creates a new message builder of the specified size.
+     * @param size the initial size of the message builder buffer
+     * @return a new message builder
+     */
+    @Nonnull
+    MessageBuilder builder(int size);
+}
diff --git 
a/maven-core/src/main/java/org/apache/maven/internal/message/ApiMessageBuilderFactory.java
 
b/maven-core/src/main/java/org/apache/maven/internal/message/ApiMessageBuilderFactory.java
new file mode 100644
index 0000000000..4ec19fa92b
--- /dev/null
+++ 
b/maven-core/src/main/java/org/apache/maven/internal/message/ApiMessageBuilderFactory.java
@@ -0,0 +1,264 @@
+/*
+ * 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.message;
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.TreeSet;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.BooleanSupplier;
+
+import org.apache.maven.api.services.MessageBuilder;
+import org.apache.maven.api.services.MessageBuilderFactory;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+
+/**
+ * Provides the Maven 4 API {@link MessageBuilderFactory}, so that a plugin 
can use one API on Maven 3 and Maven 4.
+ * Colours follow the {@link MessageUtils} setting Maven applies from {@code 
--color} and {@code --batch-mode}; style
+ * names are resolved as Maven 3.10 and Maven 4 resolve them.
+ * <p>
+ * Resolved styles are cached for the lifetime of the JVM, so a {@code 
maven.style.*} or {@code style.*} system
+ * property must be set before the style is first used. Maven 3.10 and Maven 4 
cache them the same way.
+ * </p>
+ */
+@Named
+@Singleton
+public class ApiMessageBuilderFactory implements MessageBuilderFactory {
+
+    static final String RESET = "\u001B[0m";
+
+    private static final List<String> COLORS =
+            Arrays.asList("black", "red", "green", "yellow", "blue", 
"magenta", "cyan", "white");
+
+    private static final Map<String, String> STYLES = new 
ConcurrentHashMap<>();
+
+    private final BooleanSupplier colorEnabled;
+
+    public ApiMessageBuilderFactory() {
+        this(MessageUtils::isColorEnabled);
+    }
+
+    ApiMessageBuilderFactory(BooleanSupplier colorEnabled) {
+        this.colorEnabled = colorEnabled;
+    }
+
+    @Override
+    public boolean isColorEnabled() {
+        return colorEnabled.getAsBoolean();
+    }
+
+    @Override
+    public int getTerminalWidth() {
+        return MessageUtils.getTerminalWidth();
+    }
+
+    @Override
+    public MessageBuilder builder() {
+        return builder(64);
+    }
+
+    @Override
+    public MessageBuilder builder(int size) {
+        return new AnsiMessageBuilder(size, colorEnabled);
+    }
+
+    /**
+     * Resolves a style to its ANSI escape sequence (empty if it has no 
effect). A style is either a spec like
+     * {@code bold,f:red}, or a reference {@code .name:-default} looked up 
from the {@code maven.style.name} or
+     * {@code style.name} system property, falling back to {@code default}.
+     */
+    static String resolve(String style) {
+        return STYLES.computeIfAbsent(style, 
ApiMessageBuilderFactory::doResolve);
+    }
+
+    private static String doResolve(String style) {
+        String spec = style;
+        if (spec.startsWith(".")) {
+            int i = spec.indexOf(":-");
+            String name = i < 0 ? spec.substring(1) : spec.substring(1, i);
+            String def = i < 0 ? "" : spec.substring(i + 2);
+            spec = System.getProperty("maven.style." + name, 
System.getProperty("style." + name, def));
+        }
+        // same order as Maven 3.10 and 4 emit: attributes 3-9, foreground, 
background, bold, faint
+        String[] slots = new String[5];
+        TreeSet<String> attributes = new TreeSet<>();
+        for (String item : spec.split(",")) {
+            String code = code(item.trim().toLowerCase(Locale.ROOT));
+            if (code == null) {
+                continue;
+            }
+            int colon = item.trim().indexOf(':');
+            if (colon > 0) {
+                String target = item.trim().substring(0, 
colon).toLowerCase(Locale.ROOT);
+                slots[target.startsWith("f") ? 1 : 2] = code;
+            } else if (code.equals("1")) {
+                slots[3] = code;
+            } else if (code.equals("2")) {
+                slots[4] = code;
+            } else {
+                attributes.add(code);
+            }
+        }
+        slots[0] = attributes.isEmpty() ? null : String.join(";", attributes);
+        StringBuilder codes = new StringBuilder();
+        for (String slot : slots) {
+            if (slot != null) {
+                codes.append(codes.length() > 0 ? ";" : "").append(slot);
+            }
+        }
+        return codes.length() > 0 ? "\u001B[" + codes + "m" : "";
+    }
+
+    // the subset of the style syntax Maven styles use; unknown items are 
ignored
+    private static String code(String item) {
+        switch (item) {
+            case "bold":
+                return "1";
+            case "faint":
+                return "2";
+            case "italic":
+                return "3";
+            case "underline":
+                return "4";
+            case "blink":
+                return "5";
+            case "inverse":
+                return "7";
+            case "conceal":
+            case "hidden":
+                return "8";
+            case "crossed-out":
+                return "9";
+            default:
+        }
+        int colon = item.indexOf(':');
+        if (colon < 0) {
+            return null;
+        }
+        String target = item.substring(0, colon);
+        boolean fg = target.equals("f") || target.equals("fg") || 
target.equals("foreground");
+        boolean bg = target.equals("b") || target.equals("bg") || 
target.equals("background");
+        return fg || bg ? color(item.substring(colon + 1), fg) : null;
+    }
+
+    private static String color(String color, boolean fg) {
+        if (color.equals("default")) {
+            return fg ? "39" : "49";
+        }
+        boolean bright = false;
+        if (color.startsWith("bright-")) {
+            bright = true;
+            color = color.substring("bright-".length());
+        } else if (color.startsWith("!")) {
+            bright = true;
+            color = color.substring(1);
+        }
+        int index = COLORS.indexOf(color);
+        if (index >= 0) {
+            return String.valueOf((fg ? 30 : 40) + (bright ? 60 : 0) + index);
+        }
+        try {
+            if (color.startsWith("#") || color.startsWith("x")) {
+                int rgb = Integer.parseInt(color.substring(1), 16);
+                return (fg ? "38" : "48") + ";2;" + (rgb >> 16 & 0xff) + ";" + 
(rgb >> 8 & 0xff) + ";" + (rgb & 0xff);
+            }
+            return (fg ? "38" : "48") + ";5;" + (Integer.parseInt(color) & 
0xff);
+        } catch (NumberFormatException e) {
+            return null;
+        }
+    }
+
+    private static class AnsiMessageBuilder implements MessageBuilder {
+        private final StringBuilder builder;
+        private boolean styled;
+
+        private final BooleanSupplier colorEnabled;
+
+        private AnsiMessageBuilder(int size, BooleanSupplier colorEnabled) {
+            this.builder = new StringBuilder(size);
+            this.colorEnabled = colorEnabled;
+        }
+
+        @Override
+        public MessageBuilder style(String style) {
+            if (colorEnabled.getAsBoolean()) {
+                String ansi = resolve(style);
+                if (!ansi.isEmpty()) {
+                    if (styled) {
+                        builder.append(RESET);
+                    }
+                    builder.append(ansi);
+                    styled = true;
+                }
+            }
+            return this;
+        }
+
+        @Override
+        public MessageBuilder resetStyle() {
+            if (styled) {
+                builder.append(RESET);
+                styled = false;
+            }
+            return this;
+        }
+
+        @Override
+        public MessageBuilder append(CharSequence cs) {
+            builder.append(cs);
+            return this;
+        }
+
+        @Override
+        public MessageBuilder append(CharSequence cs, int start, int end) {
+            builder.append(cs, start, end);
+            return this;
+        }
+
+        @Override
+        public MessageBuilder append(char c) {
+            builder.append(c);
+            return this;
+        }
+
+        @Override
+        public MessageBuilder setLength(int length) {
+            builder.setLength(length);
+            if (length == 0) {
+                styled = false;
+            }
+            return this;
+        }
+
+        @Override
+        public String build() {
+            return styled ? builder + RESET : builder.toString();
+        }
+
+        @Override
+        public String toString() {
+            return build();
+        }
+    }
+}
diff --git a/maven-core/src/main/resources/META-INF/maven/extension.xml 
b/maven-core/src/main/resources/META-INF/maven/extension.xml
index a419b3e835..3ad687feea 100644
--- a/maven-core/src/main/resources/META-INF/maven/extension.xml
+++ b/maven-core/src/main/resources/META-INF/maven/extension.xml
@@ -23,6 +23,10 @@ under the License.
   <exportedPackages>
     <!-- maven-* -->
     <exportedPackage>org.apache.maven.*</exportedPackage>
+    <!-- the Maven 4 API types Maven 3 provides, see 
org.apache.maven.internal.message.ApiMessageBuilderFactory -->
+    <exportedPackage>org.apache.maven.api.*</exportedPackage>
+    <exportedPackage>org.apache.maven.api.annotations.*</exportedPackage>
+    <exportedPackage>org.apache.maven.api.services.*</exportedPackage>
     <exportedPackage>org.apache.maven.artifact</exportedPackage>
     <exportedPackage>org.apache.maven.classrealm</exportedPackage>
     <exportedPackage>org.apache.maven.cli</exportedPackage>
diff --git 
a/maven-core/src/test/java/org/apache/maven/internal/message/ApiMessageBuilderFactoryBindingTest.java
 
b/maven-core/src/test/java/org/apache/maven/internal/message/ApiMessageBuilderFactoryBindingTest.java
new file mode 100644
index 0000000000..6abdc49dde
--- /dev/null
+++ 
b/maven-core/src/test/java/org/apache/maven/internal/message/ApiMessageBuilderFactoryBindingTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.message;
+
+import javax.inject.Inject;
+
+import org.apache.maven.api.services.MessageBuilderFactory;
+import org.codehaus.plexus.testing.PlexusTest;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@PlexusTest
+class ApiMessageBuilderFactoryBindingTest {
+
+    @Inject
+    MessageBuilderFactory factory;
+
+    @Test
+    void theApiFactoryIsBound() {
+        assertTrue(factory instanceof ApiMessageBuilderFactory);
+        assertEquals(
+                "strong ok 42",
+                factory.builder()
+                        .strong("strong")
+                        .a(" ")
+                        .success("ok")
+                        .a(' ')
+                        .a(42)
+                        .build());
+        assertEquals("", factory.builder(16).a("x").setLength(0).build());
+    }
+}
diff --git 
a/maven-core/src/test/java/org/apache/maven/internal/message/ApiMessageBuilderFactoryTest.java
 
b/maven-core/src/test/java/org/apache/maven/internal/message/ApiMessageBuilderFactoryTest.java
new file mode 100644
index 0000000000..77323e271a
--- /dev/null
+++ 
b/maven-core/src/test/java/org/apache/maven/internal/message/ApiMessageBuilderFactoryTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.message;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.maven.shared.utils.logging.MessageUtils;
+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 ApiMessageBuilderFactoryTest {
+
+    private final AtomicBoolean color = new AtomicBoolean();
+
+    private final ApiMessageBuilderFactory factory = new 
ApiMessageBuilderFactory(color::get);
+
+    @Test
+    void resolvesStyleSpecsLikeMaven310() {
+        assertEquals("\u001B[31;1m", 
ApiMessageBuilderFactory.resolve("bold,f:red"));
+        assertEquals("\u001B[36;1m", 
ApiMessageBuilderFactory.resolve("f:cyan,bold"));
+        assertEquals("\u001B[4;31;44;1m", 
ApiMessageBuilderFactory.resolve("bold,underline,f:red,bg:blue"));
+        assertEquals("\u001B[90m", 
ApiMessageBuilderFactory.resolve("f:bright-black"));
+        assertEquals("\u001B[38;5;208;44m", 
ApiMessageBuilderFactory.resolve("bg:blue,fg:208"));
+        assertEquals("\u001B[38;2;255;0;16m", 
ApiMessageBuilderFactory.resolve("f:#ff0010"));
+        assertEquals("", 
ApiMessageBuilderFactory.resolve("nonsense,f:nocolor"));
+    }
+
+    @Test
+    void namedStylesFollowSystemProperties() {
+        assertEquals("\u001B[34;1m", 
ApiMessageBuilderFactory.resolve(".test-unset:-bold,f:blue"));
+        System.setProperty("maven.style.test-set", "f:green");
+        try {
+            assertEquals("\u001B[32m", 
ApiMessageBuilderFactory.resolve(".test-set:-bold,f:blue"));
+        } finally {
+            System.clearProperty("maven.style.test-set");
+        }
+    }
+
+    @Test
+    void colorFlagAndWidthFollowMavenSharedUtils() {
+        assertFalse(factory.isColorEnabled());
+        color.set(true);
+        assertTrue(factory.isColorEnabled());
+        assertEquals(MessageUtils.getTerminalWidth(), 
factory.getTerminalWidth());
+    }
+
+    @Test
+    void colorsOnlyWhenEnabled() {
+        color.set(false);
+        assertEquals("a b c", factory.builder().a("a ").failure("b").a(" 
c").build());
+
+        color.set(true);
+        assertEquals(
+                "a \u001B[31;1mb\u001B[0m c",
+                factory.builder().a("a ").failure("b").a(" c").build());
+        assertEquals(
+                "\u001B[1mx\u001B[0m", 
factory.builder(8).style("bold").a("x").build());
+    }
+}

Reply via email to