Skip to content

Add support for sending mojo execution events #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/main/java/org/codehaus/plexus/build/connect/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -51,11 +49,7 @@ public EventListener(BuildConnection connection) {

@Override
public void init(Context context) throws Exception {
Map<String, String> 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);
}
Expand All @@ -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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, String> settings) {
super(settings);
public InitMessage(Context context) {
super(toMap(context));
}

private static Map<String, String> toMap(Context context) {
Map<String, String> 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<String, String> payload) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> toMap(MojoExecution mojoExecution, Type type) {
Map<String, String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static Map<String, String> toMap(MavenProject mavenProject, Type eventty
/**
* Describe the type of the event
*/
public enum EventType {
public static enum EventType {
/**
* The project was started
*/
Expand Down