Skip to content

Commit 65e7544

Browse files
Christoph Läubrichlaeubi
authored andcommitted
Add support for sending mojo execution events
Signed-off-by: Christoph Läubrich <[email protected]>
1 parent 490f701 commit 65e7544

File tree

5 files changed

+199
-16
lines changed

5 files changed

+199
-16
lines changed

src/main/java/org/codehaus/plexus/build/connect/EventListener.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
*/
1313
package org.codehaus.plexus.build.connect;
1414

15-
import java.util.LinkedHashMap;
16-
import java.util.Map;
17-
1815
import javax.inject.Inject;
1916
import javax.inject.Named;
2017
import javax.inject.Singleton;
@@ -25,6 +22,7 @@
2522
import org.apache.maven.execution.MavenSession;
2623
import org.codehaus.plexus.build.connect.messages.InitMessage;
2724
import org.codehaus.plexus.build.connect.messages.Message;
25+
import org.codehaus.plexus.build.connect.messages.MojoMessage;
2826
import org.codehaus.plexus.build.connect.messages.ProjectMessage;
2927
import org.codehaus.plexus.build.connect.messages.ProjectsMessage;
3028
import org.codehaus.plexus.build.connect.messages.SessionMessage;
@@ -51,11 +49,7 @@ public EventListener(BuildConnection connection) {
5149

5250
@Override
5351
public void init(Context context) throws Exception {
54-
Map<String, String> data = new LinkedHashMap<>();
55-
context.getData().forEach((k, v) -> {
56-
data.put(k, String.valueOf(v));
57-
});
58-
Message message = connection.send(new InitMessage(data), null);
52+
Message message = connection.send(new InitMessage(context), null);
5953
if (message != null) {
6054
configuration = Configuration.of(message);
6155
}
@@ -71,9 +65,9 @@ public void onEvent(Object event) throws Exception {
7165
}
7266
}
7367

74-
private void handleExecutionEvent(ExecutionEvent executionEvent) {
75-
MavenSession session = executionEvent.getSession();
76-
Type type = executionEvent.getType();
68+
private void handleExecutionEvent(ExecutionEvent event) {
69+
MavenSession session = event.getSession();
70+
Type type = event.getType();
7771
switch (type) {
7872
case SessionStarted:
7973
connection.send(new SessionMessage(session, true), session);
@@ -88,7 +82,13 @@ private void handleExecutionEvent(ExecutionEvent executionEvent) {
8882
case ProjectFailed:
8983
case ProjectSkipped:
9084
case ProjectSucceeded:
91-
connection.send(new ProjectMessage(executionEvent.getProject(), type), session);
85+
connection.send(new ProjectMessage(event.getProject(), type), session);
86+
break;
87+
case MojoStarted:
88+
case MojoFailed:
89+
case MojoSkipped:
90+
case MojoSucceeded:
91+
connection.send(new MojoMessage(event.getMojoExecution(), type), session);
9292
break;
9393
default:
9494
break;

src/main/java/org/codehaus/plexus/build/connect/messages/InitMessage.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
*/
1313
package org.codehaus.plexus.build.connect.messages;
1414

15+
import java.util.LinkedHashMap;
1516
import java.util.Map;
17+
import java.util.Properties;
18+
19+
import org.apache.maven.eventspy.EventSpy.Context;
1620

1721
/**
1822
* Message send to init the inital communication with the endpoints
@@ -22,10 +26,26 @@ public class InitMessage extends Message {
2226
/**
2327
* Creates a message with inital information about the running maven system
2428
*
25-
* @param settings the context settings to send to the endpoint
29+
* @param context the context settings to send to the endpoint
2630
*/
27-
public InitMessage(Map<String, String> settings) {
28-
super(settings);
31+
public InitMessage(Context context) {
32+
super(toMap(context));
33+
}
34+
35+
private static Map<String, String> toMap(Context context) {
36+
Map<String, String> data = new LinkedHashMap<>();
37+
context.getData().forEach((k, v) -> {
38+
if (v instanceof String) {
39+
data.put(k, (String) v);
40+
}
41+
if (v instanceof Properties) {
42+
Properties properties = (Properties) v;
43+
for (String p : properties.stringPropertyNames()) {
44+
data.put(k + "." + p, properties.getProperty(p));
45+
}
46+
}
47+
});
48+
return data;
2949
}
3050

3151
InitMessage(String sessionId, long threadId, Map<String, String> payload) {

src/main/java/org/codehaus/plexus/build/connect/messages/Message.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ public static Message decode(byte[] bytes) {
207207
if ("ProjectMessage".equals(messageType)) {
208208
return new ProjectMessage(sessionId, threadId, payload);
209209
}
210+
if ("MojoMessage".equals(messageType)) {
211+
return new MojoMessage(sessionId, threadId, payload);
212+
}
210213
return new Message(sessionId, threadId, payload);
211214
} catch (IOException e) {
212215
// should never happen, but if it happens something is wrong!
@@ -237,4 +240,26 @@ private static void writeString(String string, DataOutputStream stream) throws I
237240
stream.write(bytes);
238241
}
239242
}
243+
244+
@Override
245+
public int hashCode() {
246+
return Objects.hash(properties, sessionId, threadId);
247+
}
248+
249+
@Override
250+
public boolean equals(Object obj) {
251+
if (this == obj) {
252+
return true;
253+
}
254+
if (obj == null) {
255+
return false;
256+
}
257+
if (getClass() != obj.getClass()) {
258+
return false;
259+
}
260+
Message other = (Message) obj;
261+
return Objects.equals(properties, other.properties)
262+
&& Objects.equals(sessionId, other.sessionId)
263+
&& threadId == other.threadId;
264+
}
240265
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
Copyright (c) 2025 Christoph Läubrich All rights reserved.
3+
4+
This program is licensed to you under the Apache License Version 2.0,
5+
and you may not use this file except in compliance with the Apache License Version 2.0.
6+
You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
8+
Unless required by applicable law or agreed to in writing,
9+
software distributed under the Apache License Version 2.0 is distributed on an
10+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package org.codehaus.plexus.build.connect.messages;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
import org.apache.maven.execution.ExecutionEvent.Type;
19+
import org.apache.maven.plugin.MojoExecution;
20+
21+
/**
22+
* Mesaage generated when a mojo is executed
23+
*/
24+
public class MojoMessage extends Message {
25+
26+
private static final String GOAL = "goal";
27+
private static final String LIFECYCLE_PHASE = "lifecyclePhase";
28+
private static final String EXECUTION_ID = "executionId";
29+
private static final String VERSION = "version";
30+
private static final String ARTIFACT_ID = "artifactId";
31+
private static final String GROUP_ID = "groupId";
32+
private static final String EVENT_TYPE = "eventType";
33+
34+
MojoMessage(String sessionId, long threadId, Map<String, String> payload) {
35+
super(sessionId, threadId, payload);
36+
}
37+
38+
/**
39+
* Creates a Mojo message from execution and type
40+
*
41+
* @param mojoExecution
42+
* @param type
43+
*/
44+
public MojoMessage(MojoExecution mojoExecution, Type type) {
45+
super(toMap(mojoExecution, type));
46+
}
47+
48+
/**
49+
* @return the group id
50+
*/
51+
public String getGroupId() {
52+
return getProperty(GROUP_ID);
53+
}
54+
55+
/**
56+
* @return the artifact id
57+
*/
58+
public String getArtifactId() {
59+
return getProperty(ARTIFACT_ID);
60+
}
61+
62+
/**
63+
* @return the version
64+
*/
65+
public String getVersion() {
66+
return getProperty(VERSION);
67+
}
68+
69+
/**
70+
* @return the execution id
71+
*/
72+
public String getExecutionId() {
73+
return getProperty(EXECUTION_ID);
74+
}
75+
76+
/**
77+
* @return the lifecycle phase
78+
*/
79+
public String getLifecyclePhase() {
80+
return getProperty(LIFECYCLE_PHASE);
81+
}
82+
83+
/**
84+
* @return the lifecycle phase
85+
*/
86+
public String getGoal() {
87+
return getProperty(GOAL);
88+
}
89+
90+
/**
91+
* @return the type of event
92+
*/
93+
public EventType getType() {
94+
try {
95+
return EventType.valueOf(getProperty(EVENT_TYPE));
96+
} catch (RuntimeException e) {
97+
return EventType.Unknown;
98+
}
99+
}
100+
101+
private static Map<String, String> toMap(MojoExecution mojoExecution, Type type) {
102+
Map<String, String> map = new HashMap<>();
103+
map.put(EVENT_TYPE, type.name());
104+
map.put(GROUP_ID, mojoExecution.getGroupId());
105+
map.put(ARTIFACT_ID, mojoExecution.getArtifactId());
106+
map.put(VERSION, mojoExecution.getVersion());
107+
map.put(EXECUTION_ID, mojoExecution.getExecutionId());
108+
map.put(LIFECYCLE_PHASE, mojoExecution.getLifecyclePhase());
109+
map.put(GOAL, mojoExecution.getGoal());
110+
return map;
111+
}
112+
113+
/**
114+
* create the event type
115+
*/
116+
public static enum EventType {
117+
/**
118+
* the mojo was started
119+
*/
120+
MojoStarted,
121+
/**
122+
* The mojo failed
123+
*/
124+
MojoFailed,
125+
/**
126+
* The mojo was skipped
127+
*/
128+
MojoSkipped,
129+
/**
130+
* The mojo succeed
131+
*/
132+
MojoSucceeded,
133+
/**
134+
* the type is unknown
135+
*/
136+
Unknown;
137+
}
138+
}

src/main/java/org/codehaus/plexus/build/connect/messages/ProjectMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static Map<String, String> toMap(MavenProject mavenProject, Type eventty
9797
/**
9898
* Describe the type of the event
9999
*/
100-
public enum EventType {
100+
public static enum EventType {
101101
/**
102102
* The project was started
103103
*/

0 commit comments

Comments
 (0)