From 07aabd9e188f362f65a2593fa3a7c6288572ea17 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 19 Oct 2025 17:48:53 +0000 Subject: [PATCH 1/5] Initial plan From b17ea7c0c9ed3eb0a0bfc8af812cd0a1c9b8ef7f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 19 Oct 2025 17:54:19 +0000 Subject: [PATCH 2/5] Implement Messages API with builder pattern Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com> --- README.md | 41 +++++ .../build/messages/DefaultMessages.java | 170 ++++++++++++++++++ .../plexus/build/messages/Message.java | 89 +++++++++ .../plexus/build/messages/MessageBuilder.java | 105 +++++++++++ .../plexus/build/messages/MessageType.java | 33 ++++ .../plexus/build/messages/Messages.java | 84 +++++++++ 6 files changed, 522 insertions(+) create mode 100644 src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java create mode 100644 src/main/java/org/codehaus/plexus/build/messages/Message.java create mode 100644 src/main/java/org/codehaus/plexus/build/messages/MessageBuilder.java create mode 100644 src/main/java/org/codehaus/plexus/build/messages/MessageType.java create mode 100644 src/main/java/org/codehaus/plexus/build/messages/Messages.java diff --git a/README.md b/README.md index 84a33e8..6b384d7 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,47 @@ The project was relocated from . Als ## Provided APIs +### Messages API + +The Messages API provides a modern, flexible way to create and manage build messages/markers that inform users in an IDE about issues in their files. It uses a builder pattern for constructing messages in a more convenient and extensible way compared to the legacy BuildContext message methods. + +**Key Features:** +- Builder pattern for flexible message construction +- Clear separation of concerns from resource operations +- Support for error, warning, and info messages +- File path-based message management +- Optional line and column information +- Optional exception cause association + +**Example Usage:** + +```java +@Inject +private Messages messages; + +public void execute() { + // Create an error message + messages.buildError(Paths.get("/path/to/file.java")) + .line(42) + .column(10) + .message("Syntax error") + .cause(exception) + .create(); + + // Create a warning message + messages.buildWarning(Paths.get("/path/to/file.java")) + .line(15) + .message("Deprecated method used") + .create(); + + // Clear messages for a specific file + messages.clear(Paths.get("/path/to/file.java")); + + // Clear all messages + messages.clearAll(); +} +``` + ### Progress The API allows a mojo to report progress in a way that is suitable to be shown as a progressbar as well as check if the user wants the mojo to gracefully abort its current operation. diff --git a/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java b/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java new file mode 100644 index 0000000..ab54293 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java @@ -0,0 +1,170 @@ +/* +Copyright (c) 2008 Sonatype, Inc. All rights reserved. + +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.messages; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import java.nio.file.Path; + +import org.codehaus.plexus.build.BuildContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Default implementation of the Messages interface. + *

+ * This implementation delegates to the BuildContext for compatibility with existing + * message handling infrastructure. It logs messages and calls the legacy BuildContext + * message API. + *

+ */ +@Named("default") +@Singleton +public class DefaultMessages implements Messages { + + private static final Logger logger = LoggerFactory.getLogger(DefaultMessages.class); + + private final BuildContext buildContext; + + /** + * Creates a new DefaultMessages instance. + * + * @param buildContext the BuildContext to which messages will be delegated + */ + @Inject + public DefaultMessages(BuildContext buildContext) { + this.buildContext = buildContext; + } + + @Override + public void clearAll() { + // This is a no-op in the default implementation + // Custom implementations may provide actual clearing functionality + } + + @Override + public void clear(Path path) { + if (path != null) { + buildContext.removeMessages(path.toFile()); + } + } + + @Override + public MessageBuilder buildError(Path path) { + return build(MessageType.ERROR, path); + } + + @Override + public MessageBuilder buildWarning(Path path) { + return build(MessageType.WARNING, path); + } + + @Override + public MessageBuilder buildInfo(Path path) { + return build(MessageType.INFO, path); + } + + @Override + public MessageBuilder build(MessageType type, Path path) { + return new MessageBuilder(type, path, this::handleMessage); + } + + /** + * Handles a message by logging it and delegating to the BuildContext. + * + * @param message the message to handle + */ + private void handleMessage(Message message) { + // Log the message + String logMessage = formatLogMessage(message); + + switch (message.getType()) { + case ERROR: + logger.error(logMessage, message.getCause()); + break; + case WARNING: + logger.warn(logMessage, message.getCause()); + break; + case INFO: + logger.info(logMessage, message.getCause()); + break; + } + + // Delegate to BuildContext for compatibility + if (message.getPath() != null) { + int severity = mapTypeToSeverity(message.getType()); + buildContext.addMessage( + message.getPath().toFile(), + message.getLine(), + message.getColumn(), + message.getMessage(), + severity, + message.getCause()); + } + } + + /** + * Formats a message for logging. + * + * @param message the message to format + * @return the formatted log message + */ + private String formatLogMessage(Message message) { + StringBuilder sb = new StringBuilder(); + + if (message.getPath() != null) { + sb.append(message.getPath().toAbsolutePath()); + } + + if (message.getLine() > 0 || message.getColumn() > 0) { + sb.append(" ["); + if (message.getLine() > 0) { + sb.append(message.getLine()); + } + if (message.getColumn() > 0) { + sb.append(':').append(message.getColumn()); + } + sb.append("]"); + } + + if (message.getMessage() != null) { + if (sb.length() > 0) { + sb.append(": "); + } + sb.append(message.getMessage()); + } + + return sb.toString(); + } + + /** + * Maps a MessageType to a BuildContext severity level. + * + * @param type the message type + * @return the corresponding BuildContext severity + */ + private int mapTypeToSeverity(MessageType type) { + switch (type) { + case ERROR: + return BuildContext.SEVERITY_ERROR; + case WARNING: + return BuildContext.SEVERITY_WARNING; + case INFO: + default: + // There's no INFO severity in BuildContext, use WARNING as fallback + return BuildContext.SEVERITY_WARNING; + } + } +} diff --git a/src/main/java/org/codehaus/plexus/build/messages/Message.java b/src/main/java/org/codehaus/plexus/build/messages/Message.java new file mode 100644 index 0000000..0ae51a2 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/messages/Message.java @@ -0,0 +1,89 @@ +/* +Copyright (c) 2008 Sonatype, Inc. All rights reserved. + +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.messages; + +import java.nio.file.Path; + +/** + * Represents a message with all its parameters. + * This class holds the collected parameters for a message that can be created through the MessageBuilder. + */ +public class Message { + private final MessageType type; + private final Path path; + private final int line; + private final int column; + private final String message; + private final Throwable cause; + + /** + * Creates a new message with the specified parameters. + * + * @param type the message type + * @param path the file path associated with this message + * @param line the line number (1-based, 0 for unknown) + * @param column the column number (1-based, 0 for unknown) + * @param message the message text + * @param cause the exception cause, can be null + */ + public Message(MessageType type, Path path, int line, int column, String message, Throwable cause) { + this.type = type; + this.path = path; + this.line = line; + this.column = column; + this.message = message; + this.cause = cause; + } + + /** + * @return the message type + */ + public MessageType getType() { + return type; + } + + /** + * @return the file path + */ + public Path getPath() { + return path; + } + + /** + * @return the line number (1-based, 0 for unknown) + */ + public int getLine() { + return line; + } + + /** + * @return the column number (1-based, 0 for unknown) + */ + public int getColumn() { + return column; + } + + /** + * @return the message text + */ + public String getMessage() { + return message; + } + + /** + * @return the exception cause, or null if none + */ + public Throwable getCause() { + return cause; + } +} diff --git a/src/main/java/org/codehaus/plexus/build/messages/MessageBuilder.java b/src/main/java/org/codehaus/plexus/build/messages/MessageBuilder.java new file mode 100644 index 0000000..86dafd9 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/messages/MessageBuilder.java @@ -0,0 +1,105 @@ +/* +Copyright (c) 2008 Sonatype, Inc. All rights reserved. + +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.messages; + +import java.nio.file.Path; +import java.util.function.Consumer; + +/** + * Builder class for constructing messages. + *

+ * This class implements the builder pattern for creating messages with various parameters. + * It is typically not called directly by client code, but is used internally by the Messages API + * implementations. + *

+ */ +public class MessageBuilder { + private final MessageType type; + private final Path path; + private final Consumer consumer; + + private int line = 0; + private int column = 0; + private String message; + private Throwable cause; + + /** + * Creates a new MessageBuilder. + *

+ * Note: This constructor is usually not called by client code. Use the builder methods + * provided by the {@link Messages} interface instead (e.g., buildError, buildWarning, buildInfo). + *

+ * + * @param type the type of message to build + * @param path the file path for which the message should be created + * @param consumer the consumer that will receive the constructed message + */ + public MessageBuilder(MessageType type, Path path, Consumer consumer) { + this.type = type; + this.path = path; + this.consumer = consumer; + } + + /** + * Sets the line number for the message. + * + * @param line the line number (1-based, use 0 for unknown) + * @return this builder for method chaining + */ + public MessageBuilder line(int line) { + this.line = line; + return this; + } + + /** + * Sets the column number for the message. + * + * @param column the column number (1-based, use 0 for unknown) + * @return this builder for method chaining + */ + public MessageBuilder column(int column) { + this.column = column; + return this; + } + + /** + * Sets the message text. + * + * @param message the message text + * @return this builder for method chaining + */ + public MessageBuilder message(String message) { + this.message = message; + return this; + } + + /** + * Sets the exception cause for the message. + * + * @param cause the exception that caused this message + * @return this builder for method chaining + */ + public MessageBuilder cause(Throwable cause) { + this.cause = cause; + return this; + } + + /** + * Creates the message object with all collected parameters and informs the consumer. + * This method finalizes the builder and creates the message. + */ + public void create() { + Message msg = new Message(type, path, line, column, message, cause); + consumer.accept(msg); + } +} diff --git a/src/main/java/org/codehaus/plexus/build/messages/MessageType.java b/src/main/java/org/codehaus/plexus/build/messages/MessageType.java new file mode 100644 index 0000000..00fdd65 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/messages/MessageType.java @@ -0,0 +1,33 @@ +/* +Copyright (c) 2008 Sonatype, Inc. All rights reserved. + +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.messages; + +/** + * Enum representing the type/severity of a message. + */ +public enum MessageType { + /** + * Informational message + */ + INFO, + + /** + * Warning message + */ + WARNING, + + /** + * Error message + */ + ERROR +} diff --git a/src/main/java/org/codehaus/plexus/build/messages/Messages.java b/src/main/java/org/codehaus/plexus/build/messages/Messages.java new file mode 100644 index 0000000..400122a --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/messages/Messages.java @@ -0,0 +1,84 @@ +/* +Copyright (c) 2008 Sonatype, Inc. All rights reserved. + +This program is licensed to you under the Apache License Version 2.0, +and you may not use this file except in compliance with the Apache License Version 2.0. +You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. + +Unless required by applicable law or agreed to in writing, +software distributed under the Apache License Version 2.0 is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. +*/ +package org.codehaus.plexus.build.messages; + +import java.nio.file.Path; + +/** + *

Messages interface.

+ *

+ * This API provides a modern, flexible way to create and manage build messages/markers + * that inform users in an IDE about issues in their files. It uses a builder pattern + * for constructing messages in a more convenient and extensible way compared to the + * legacy BuildContext message methods. + *

+ *

+ * Example usage: + *

+ *
+ * messages.buildError(Paths.get("/path/to/file.java"))
+ *     .line(42)
+ *     .column(10)
+ *     .message("Syntax error")
+ *     .create();
+ * 
+ */ +public interface Messages { + + /** + * Clears all messages. + * This removes all messages that were previously created through this API. + */ + void clearAll(); + + /** + * Clears messages associated with a specific path. + * + * @param path the file path for which to clear messages + */ + void clear(Path path); + + /** + * Creates a builder for an error message. + * + * @param path the file path for which the error message should be created + * @return a MessageBuilder for constructing the error message + */ + MessageBuilder buildError(Path path); + + /** + * Creates a builder for a warning message. + * + * @param path the file path for which the warning message should be created + * @return a MessageBuilder for constructing the warning message + */ + MessageBuilder buildWarning(Path path); + + /** + * Creates a builder for an informational message. + * + * @param path the file path for which the info message should be created + * @return a MessageBuilder for constructing the info message + */ + MessageBuilder buildInfo(Path path); + + /** + * Creates a builder for a message of a specific type. + * This is the generic method that the other build methods delegate to. + * + * @param type the type of message to build + * @param path the file path for which the message should be created + * @return a MessageBuilder for constructing the message + */ + MessageBuilder build(MessageType type, Path path); +} From 738c950ba25b4744c4c6fea512b5c0bab2e78ffd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 19 Oct 2025 17:56:08 +0000 Subject: [PATCH 3/5] Fix log message formatting and clarify INFO mapping Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com> --- .../build/messages/DefaultMessages.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java b/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java index ab54293..6e37b84 100644 --- a/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java +++ b/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java @@ -128,14 +128,18 @@ private String formatLogMessage(Message message) { sb.append(message.getPath().toAbsolutePath()); } - if (message.getLine() > 0 || message.getColumn() > 0) { + if (message.getLine() > 0 && message.getColumn() > 0) { sb.append(" ["); - if (message.getLine() > 0) { - sb.append(message.getLine()); - } - if (message.getColumn() > 0) { - sb.append(':').append(message.getColumn()); - } + sb.append(message.getLine()); + sb.append(':').append(message.getColumn()); + sb.append("]"); + } else if (message.getLine() > 0) { + sb.append(" ["); + sb.append(message.getLine()); + sb.append("]"); + } else if (message.getColumn() > 0) { + sb.append(" [:"); + sb.append(message.getColumn()); sb.append("]"); } @@ -163,7 +167,9 @@ private int mapTypeToSeverity(MessageType type) { return BuildContext.SEVERITY_WARNING; case INFO: default: - // There's no INFO severity in BuildContext, use WARNING as fallback + // There's no INFO severity in BuildContext (only WARNING and ERROR), + // so we map INFO messages to WARNING to ensure they are still visible in the IDE. + // Custom implementations may provide different mappings. return BuildContext.SEVERITY_WARNING; } } From 99115dd0390143172f420f30b8d2a287774b196e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Oct 2025 05:09:22 +0000 Subject: [PATCH 4/5] Address review feedback: shorter method names, package-private constructor, copyright removal, create(String message) Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com> --- README.md | 10 ++++----- .../codehaus/plexus/build/BuildContext.java | 2 ++ .../build/messages/DefaultMessages.java | 14 +++++-------- .../plexus/build/messages/Message.java | 4 +--- .../plexus/build/messages/MessageBuilder.java | 21 ++++++------------- .../plexus/build/messages/MessageType.java | 2 -- .../plexus/build/messages/Messages.java | 13 +++++------- 7 files changed, 23 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 6b384d7..4509abf 100644 --- a/README.md +++ b/README.md @@ -52,18 +52,16 @@ private Messages messages; public void execute() { // Create an error message - messages.buildError(Paths.get("/path/to/file.java")) + messages.error(Paths.get("/path/to/file.java")) .line(42) .column(10) - .message("Syntax error") .cause(exception) - .create(); + .create("Syntax error"); // Create a warning message - messages.buildWarning(Paths.get("/path/to/file.java")) + messages.warning(Paths.get("/path/to/file.java")) .line(15) - .message("Deprecated method used") - .create(); + .create("Deprecated method used"); // Clear messages for a specific file messages.clear(Paths.get("/path/to/file.java")); diff --git a/src/main/java/org/codehaus/plexus/build/BuildContext.java b/src/main/java/org/codehaus/plexus/build/BuildContext.java index 8f54425..41ddc98 100644 --- a/src/main/java/org/codehaus/plexus/build/BuildContext.java +++ b/src/main/java/org/codehaus/plexus/build/BuildContext.java @@ -209,6 +209,7 @@ public interface BuildContext { * @param cause A Throwable object associated with the message. Can be null. * @since 0.0.7 * @param message a {@link java.lang.String} object. + * @deprecated Use {@link org.codehaus.plexus.build.messages.Messages} API instead */ void addMessage(File file, int line, int column, String message, int severity, Throwable cause); @@ -218,6 +219,7 @@ public interface BuildContext { * * @since 0.0.7 * @param file a {@link java.io.File} object. + * @deprecated Use {@link org.codehaus.plexus.build.messages.Messages#clear(java.nio.file.Path)} instead */ void removeMessages(File file); diff --git a/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java b/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java index 6e37b84..c5d5718 100644 --- a/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java +++ b/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java @@ -1,6 +1,4 @@ /* -Copyright (c) 2008 Sonatype, Inc. All rights reserved. - This program is licensed to you under the Apache License Version 2.0, and you may not use this file except in compliance with the Apache License Version 2.0. You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. @@ -62,17 +60,17 @@ public void clear(Path path) { } @Override - public MessageBuilder buildError(Path path) { + public MessageBuilder error(Path path) { return build(MessageType.ERROR, path); } @Override - public MessageBuilder buildWarning(Path path) { + public MessageBuilder warning(Path path) { return build(MessageType.WARNING, path); } @Override - public MessageBuilder buildInfo(Path path) { + public MessageBuilder info(Path path) { return build(MessageType.INFO, path); } @@ -167,10 +165,8 @@ private int mapTypeToSeverity(MessageType type) { return BuildContext.SEVERITY_WARNING; case INFO: default: - // There's no INFO severity in BuildContext (only WARNING and ERROR), - // so we map INFO messages to WARNING to ensure they are still visible in the IDE. - // Custom implementations may provide different mappings. - return BuildContext.SEVERITY_WARNING; + // BuildContext supports 0 as an info severity level (undocumented) + return 0; } } } diff --git a/src/main/java/org/codehaus/plexus/build/messages/Message.java b/src/main/java/org/codehaus/plexus/build/messages/Message.java index 0ae51a2..cdc89b4 100644 --- a/src/main/java/org/codehaus/plexus/build/messages/Message.java +++ b/src/main/java/org/codehaus/plexus/build/messages/Message.java @@ -1,6 +1,4 @@ /* -Copyright (c) 2008 Sonatype, Inc. All rights reserved. - This program is licensed to you under the Apache License Version 2.0, and you may not use this file except in compliance with the Apache License Version 2.0. You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. @@ -36,7 +34,7 @@ public class Message { * @param message the message text * @param cause the exception cause, can be null */ - public Message(MessageType type, Path path, int line, int column, String message, Throwable cause) { + Message(MessageType type, Path path, int line, int column, String message, Throwable cause) { this.type = type; this.path = path; this.line = line; diff --git a/src/main/java/org/codehaus/plexus/build/messages/MessageBuilder.java b/src/main/java/org/codehaus/plexus/build/messages/MessageBuilder.java index 86dafd9..55c8491 100644 --- a/src/main/java/org/codehaus/plexus/build/messages/MessageBuilder.java +++ b/src/main/java/org/codehaus/plexus/build/messages/MessageBuilder.java @@ -1,6 +1,4 @@ /* -Copyright (c) 2008 Sonatype, Inc. All rights reserved. - This program is licensed to you under the Apache License Version 2.0, and you may not use this file except in compliance with the Apache License Version 2.0. You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. @@ -30,7 +28,6 @@ public class MessageBuilder { private int line = 0; private int column = 0; - private String message; private Throwable cause; /** @@ -72,17 +69,6 @@ public MessageBuilder column(int column) { return this; } - /** - * Sets the message text. - * - * @param message the message text - * @return this builder for method chaining - */ - public MessageBuilder message(String message) { - this.message = message; - return this; - } - /** * Sets the exception cause for the message. * @@ -97,8 +83,13 @@ public MessageBuilder cause(Throwable cause) { /** * Creates the message object with all collected parameters and informs the consumer. * This method finalizes the builder and creates the message. + * + * @param message the message text (must not be null or blank) */ - public void create() { + public void create(String message) { + if (message == null || message.trim().isEmpty()) { + throw new IllegalArgumentException("Message text must not be null or blank"); + } Message msg = new Message(type, path, line, column, message, cause); consumer.accept(msg); } diff --git a/src/main/java/org/codehaus/plexus/build/messages/MessageType.java b/src/main/java/org/codehaus/plexus/build/messages/MessageType.java index 00fdd65..69a859c 100644 --- a/src/main/java/org/codehaus/plexus/build/messages/MessageType.java +++ b/src/main/java/org/codehaus/plexus/build/messages/MessageType.java @@ -1,6 +1,4 @@ /* -Copyright (c) 2008 Sonatype, Inc. All rights reserved. - This program is licensed to you under the Apache License Version 2.0, and you may not use this file except in compliance with the Apache License Version 2.0. You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. diff --git a/src/main/java/org/codehaus/plexus/build/messages/Messages.java b/src/main/java/org/codehaus/plexus/build/messages/Messages.java index 400122a..52030d2 100644 --- a/src/main/java/org/codehaus/plexus/build/messages/Messages.java +++ b/src/main/java/org/codehaus/plexus/build/messages/Messages.java @@ -1,6 +1,4 @@ /* -Copyright (c) 2008 Sonatype, Inc. All rights reserved. - This program is licensed to you under the Apache License Version 2.0, and you may not use this file except in compliance with the Apache License Version 2.0. You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. @@ -26,11 +24,10 @@ * Example usage: *

*
- * messages.buildError(Paths.get("/path/to/file.java"))
+ * messages.error(Paths.get("/path/to/file.java"))
  *     .line(42)
  *     .column(10)
- *     .message("Syntax error")
- *     .create();
+ *     .create("Syntax error");
  * 
*/ public interface Messages { @@ -54,7 +51,7 @@ public interface Messages { * @param path the file path for which the error message should be created * @return a MessageBuilder for constructing the error message */ - MessageBuilder buildError(Path path); + MessageBuilder error(Path path); /** * Creates a builder for a warning message. @@ -62,7 +59,7 @@ public interface Messages { * @param path the file path for which the warning message should be created * @return a MessageBuilder for constructing the warning message */ - MessageBuilder buildWarning(Path path); + MessageBuilder warning(Path path); /** * Creates a builder for an informational message. @@ -70,7 +67,7 @@ public interface Messages { * @param path the file path for which the info message should be created * @return a MessageBuilder for constructing the info message */ - MessageBuilder buildInfo(Path path); + MessageBuilder info(Path path); /** * Creates a builder for a message of a specific type. From deb1fe63fcdf19b56fb70bb9f93d51fccb3370d7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Oct 2025 05:23:56 +0000 Subject: [PATCH 5/5] Add @Deprecated annotations and move formatLogMessage to Message.toString() Co-authored-by: laeubi <1331477+laeubi@users.noreply.github.com> --- .../codehaus/plexus/build/BuildContext.java | 2 + .../build/messages/DefaultMessages.java | 40 +------------------ .../plexus/build/messages/Message.java | 39 ++++++++++++++++++ 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/build/BuildContext.java b/src/main/java/org/codehaus/plexus/build/BuildContext.java index 41ddc98..f132223 100644 --- a/src/main/java/org/codehaus/plexus/build/BuildContext.java +++ b/src/main/java/org/codehaus/plexus/build/BuildContext.java @@ -211,6 +211,7 @@ public interface BuildContext { * @param message a {@link java.lang.String} object. * @deprecated Use {@link org.codehaus.plexus.build.messages.Messages} API instead */ + @Deprecated void addMessage(File file, int line, int column, String message, int severity, Throwable cause); /** @@ -221,6 +222,7 @@ public interface BuildContext { * @param file a {@link java.io.File} object. * @deprecated Use {@link org.codehaus.plexus.build.messages.Messages#clear(java.nio.file.Path)} instead */ + @Deprecated void removeMessages(File file); /** diff --git a/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java b/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java index c5d5718..b10d36f 100644 --- a/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java +++ b/src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java @@ -86,7 +86,7 @@ public MessageBuilder build(MessageType type, Path path) { */ private void handleMessage(Message message) { // Log the message - String logMessage = formatLogMessage(message); + String logMessage = message.toString(); switch (message.getType()) { case ERROR: @@ -113,44 +113,6 @@ private void handleMessage(Message message) { } } - /** - * Formats a message for logging. - * - * @param message the message to format - * @return the formatted log message - */ - private String formatLogMessage(Message message) { - StringBuilder sb = new StringBuilder(); - - if (message.getPath() != null) { - sb.append(message.getPath().toAbsolutePath()); - } - - if (message.getLine() > 0 && message.getColumn() > 0) { - sb.append(" ["); - sb.append(message.getLine()); - sb.append(':').append(message.getColumn()); - sb.append("]"); - } else if (message.getLine() > 0) { - sb.append(" ["); - sb.append(message.getLine()); - sb.append("]"); - } else if (message.getColumn() > 0) { - sb.append(" [:"); - sb.append(message.getColumn()); - sb.append("]"); - } - - if (message.getMessage() != null) { - if (sb.length() > 0) { - sb.append(": "); - } - sb.append(message.getMessage()); - } - - return sb.toString(); - } - /** * Maps a MessageType to a BuildContext severity level. * diff --git a/src/main/java/org/codehaus/plexus/build/messages/Message.java b/src/main/java/org/codehaus/plexus/build/messages/Message.java index cdc89b4..3709b27 100644 --- a/src/main/java/org/codehaus/plexus/build/messages/Message.java +++ b/src/main/java/org/codehaus/plexus/build/messages/Message.java @@ -84,4 +84,43 @@ public String getMessage() { public Throwable getCause() { return cause; } + + /** + * Returns a string representation of this message. + * The format is: path [line:column]: message + * + * @return the formatted message string + */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + if (path != null) { + sb.append(path.toAbsolutePath()); + } + + if (line > 0 && column > 0) { + sb.append(" ["); + sb.append(line); + sb.append(':').append(column); + sb.append("]"); + } else if (line > 0) { + sb.append(" ["); + sb.append(line); + sb.append("]"); + } else if (column > 0) { + sb.append(" [:"); + sb.append(column); + sb.append("]"); + } + + if (message != null) { + if (sb.length() > 0) { + sb.append(": "); + } + sb.append(message); + } + + return sb.toString(); + } }