From fb998fd0f7548e6fa02f79f154b13a4cc6a94c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sat, 22 Feb 2025 17:04:51 +0100 Subject: [PATCH] Add support for sending mojo execution events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christoph Läubrich --- .../plexus/build/connect/EventListener.java | 24 +-- .../build/connect/messages/InitMessage.java | 26 +++- .../build/connect/messages/Message.java | 25 ++++ .../build/connect/messages/MojoMessage.java | 138 ++++++++++++++++++ .../connect/messages/ProjectMessage.java | 2 +- 5 files changed, 199 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/codehaus/plexus/build/connect/messages/MojoMessage.java diff --git a/src/main/java/org/codehaus/plexus/build/connect/EventListener.java b/src/main/java/org/codehaus/plexus/build/connect/EventListener.java index 5806c08..1eb5e8d 100644 --- a/src/main/java/org/codehaus/plexus/build/connect/EventListener.java +++ b/src/main/java/org/codehaus/plexus/build/connect/EventListener.java @@ -12,9 +12,6 @@ */ package org.codehaus.plexus.build.connect; -import java.util.LinkedHashMap; -import java.util.Map; - import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; @@ -25,6 +22,7 @@ import org.apache.maven.execution.MavenSession; import org.codehaus.plexus.build.connect.messages.InitMessage; import org.codehaus.plexus.build.connect.messages.Message; +import org.codehaus.plexus.build.connect.messages.MojoMessage; import org.codehaus.plexus.build.connect.messages.ProjectMessage; import org.codehaus.plexus.build.connect.messages.ProjectsMessage; import org.codehaus.plexus.build.connect.messages.SessionMessage; @@ -51,11 +49,7 @@ public EventListener(BuildConnection connection) { @Override public void init(Context context) throws Exception { - Map data = new LinkedHashMap<>(); - context.getData().forEach((k, v) -> { - data.put(k, String.valueOf(v)); - }); - Message message = connection.send(new InitMessage(data), null); + Message message = connection.send(new InitMessage(context), null); if (message != null) { configuration = Configuration.of(message); } @@ -71,9 +65,9 @@ public void onEvent(Object event) throws Exception { } } - private void handleExecutionEvent(ExecutionEvent executionEvent) { - MavenSession session = executionEvent.getSession(); - Type type = executionEvent.getType(); + private void handleExecutionEvent(ExecutionEvent event) { + MavenSession session = event.getSession(); + Type type = event.getType(); switch (type) { case SessionStarted: connection.send(new SessionMessage(session, true), session); @@ -88,7 +82,13 @@ private void handleExecutionEvent(ExecutionEvent executionEvent) { case ProjectFailed: case ProjectSkipped: case ProjectSucceeded: - connection.send(new ProjectMessage(executionEvent.getProject(), type), session); + connection.send(new ProjectMessage(event.getProject(), type), session); + break; + case MojoStarted: + case MojoFailed: + case MojoSkipped: + case MojoSucceeded: + connection.send(new MojoMessage(event.getMojoExecution(), type), session); break; default: break; diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/InitMessage.java b/src/main/java/org/codehaus/plexus/build/connect/messages/InitMessage.java index 7239b92..e8cabcb 100644 --- a/src/main/java/org/codehaus/plexus/build/connect/messages/InitMessage.java +++ b/src/main/java/org/codehaus/plexus/build/connect/messages/InitMessage.java @@ -12,7 +12,11 @@ */ package org.codehaus.plexus.build.connect.messages; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.Properties; + +import org.apache.maven.eventspy.EventSpy.Context; /** * Message send to init the inital communication with the endpoints @@ -22,10 +26,26 @@ public class InitMessage extends Message { /** * Creates a message with inital information about the running maven system * - * @param settings the context settings to send to the endpoint + * @param context the context settings to send to the endpoint */ - public InitMessage(Map settings) { - super(settings); + public InitMessage(Context context) { + super(toMap(context)); + } + + private static Map toMap(Context context) { + Map data = new LinkedHashMap<>(); + context.getData().forEach((k, v) -> { + if (v instanceof String) { + data.put(k, (String) v); + } + if (v instanceof Properties) { + Properties properties = (Properties) v; + for (String p : properties.stringPropertyNames()) { + data.put(k + "." + p, properties.getProperty(p)); + } + } + }); + return data; } InitMessage(String sessionId, long threadId, Map payload) { diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/Message.java b/src/main/java/org/codehaus/plexus/build/connect/messages/Message.java index 4400f22..ddc1430 100644 --- a/src/main/java/org/codehaus/plexus/build/connect/messages/Message.java +++ b/src/main/java/org/codehaus/plexus/build/connect/messages/Message.java @@ -207,6 +207,9 @@ public static Message decode(byte[] bytes) { if ("ProjectMessage".equals(messageType)) { return new ProjectMessage(sessionId, threadId, payload); } + if ("MojoMessage".equals(messageType)) { + return new MojoMessage(sessionId, threadId, payload); + } return new Message(sessionId, threadId, payload); } catch (IOException e) { // should never happen, but if it happens something is wrong! @@ -237,4 +240,26 @@ private static void writeString(String string, DataOutputStream stream) throws I stream.write(bytes); } } + + @Override + public int hashCode() { + return Objects.hash(properties, sessionId, threadId); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Message other = (Message) obj; + return Objects.equals(properties, other.properties) + && Objects.equals(sessionId, other.sessionId) + && threadId == other.threadId; + } } diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/MojoMessage.java b/src/main/java/org/codehaus/plexus/build/connect/messages/MojoMessage.java new file mode 100644 index 0000000..e1beb34 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/build/connect/messages/MojoMessage.java @@ -0,0 +1,138 @@ +/* +Copyright (c) 2025 Christoph Läubrich 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.connect.messages; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.maven.execution.ExecutionEvent.Type; +import org.apache.maven.plugin.MojoExecution; + +/** + * Mesaage generated when a mojo is executed + */ +public class MojoMessage extends Message { + + private static final String GOAL = "goal"; + private static final String LIFECYCLE_PHASE = "lifecyclePhase"; + private static final String EXECUTION_ID = "executionId"; + private static final String VERSION = "version"; + private static final String ARTIFACT_ID = "artifactId"; + private static final String GROUP_ID = "groupId"; + private static final String EVENT_TYPE = "eventType"; + + MojoMessage(String sessionId, long threadId, Map payload) { + super(sessionId, threadId, payload); + } + + /** + * Creates a Mojo message from execution and type + * + * @param mojoExecution + * @param type + */ + public MojoMessage(MojoExecution mojoExecution, Type type) { + super(toMap(mojoExecution, type)); + } + + /** + * @return the group id + */ + public String getGroupId() { + return getProperty(GROUP_ID); + } + + /** + * @return the artifact id + */ + public String getArtifactId() { + return getProperty(ARTIFACT_ID); + } + + /** + * @return the version + */ + public String getVersion() { + return getProperty(VERSION); + } + + /** + * @return the execution id + */ + public String getExecutionId() { + return getProperty(EXECUTION_ID); + } + + /** + * @return the lifecycle phase + */ + public String getLifecyclePhase() { + return getProperty(LIFECYCLE_PHASE); + } + + /** + * @return the lifecycle phase + */ + public String getGoal() { + return getProperty(GOAL); + } + + /** + * @return the type of event + */ + public EventType getType() { + try { + return EventType.valueOf(getProperty(EVENT_TYPE)); + } catch (RuntimeException e) { + return EventType.Unknown; + } + } + + private static Map toMap(MojoExecution mojoExecution, Type type) { + Map map = new HashMap<>(); + map.put(EVENT_TYPE, type.name()); + map.put(GROUP_ID, mojoExecution.getGroupId()); + map.put(ARTIFACT_ID, mojoExecution.getArtifactId()); + map.put(VERSION, mojoExecution.getVersion()); + map.put(EXECUTION_ID, mojoExecution.getExecutionId()); + map.put(LIFECYCLE_PHASE, mojoExecution.getLifecyclePhase()); + map.put(GOAL, mojoExecution.getGoal()); + return map; + } + + /** + * create the event type + */ + public static enum EventType { + /** + * the mojo was started + */ + MojoStarted, + /** + * The mojo failed + */ + MojoFailed, + /** + * The mojo was skipped + */ + MojoSkipped, + /** + * The mojo succeed + */ + MojoSucceeded, + /** + * the type is unknown + */ + Unknown; + } +} diff --git a/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectMessage.java b/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectMessage.java index 68a30fd..3180263 100644 --- a/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectMessage.java +++ b/src/main/java/org/codehaus/plexus/build/connect/messages/ProjectMessage.java @@ -97,7 +97,7 @@ private static Map toMap(MavenProject mavenProject, Type eventty /** * Describe the type of the event */ - public enum EventType { + public static enum EventType { /** * The project was started */