View Javadoc
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 }