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.io.File;
16  import java.nio.file.Path;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.apache.maven.execution.ExecutionEvent.Type;
21  import org.apache.maven.project.MavenProject;
22  
23  /**
24   * Send to inform about project changes
25   */
26  public class ProjectMessage extends Message {
27  
28      private static final String BASE_DIR = "baseDir";
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      ProjectMessage(String sessionId, long threadId, Map<String, String> payload) {
35          super(sessionId, threadId, payload);
36      }
37  
38      /**
39       * Constructs a new event based on project and type
40       *
41       * @param mavenProject
42       * @param eventtype
43       */
44      public ProjectMessage(MavenProject mavenProject, Type eventtype) {
45          super(toMap(mavenProject, eventtype));
46      }
47  
48      /**
49       * @return the group id of the project
50       */
51      public String getGroupId() {
52          return getProperty(GROUP_ID);
53      }
54  
55      /**
56       * @return the artifact id of the project
57       */
58      public String getArtifactId() {
59          return getProperty(ARTIFACT_ID);
60      }
61  
62      /**
63       * @return the version of the project
64       */
65      public String getVersion() {
66          return getProperty(VERSION);
67      }
68  
69      /**
70       * @return the basedir of the project
71       */
72      public Path getBaseDir() {
73          return new File(getProperty(BASE_DIR)).toPath();
74      }
75  
76      /**
77       * @return the type of the event
78       */
79      public EventType getType() {
80          try {
81              return EventType.valueOf(getProperty(EVENT_TYPE));
82          } catch (RuntimeException e) {
83              return EventType.Unknown;
84          }
85      }
86  
87      private static Map<String, String> toMap(MavenProject mavenProject, Type eventtype) {
88          Map<String, String> map = new HashMap<>();
89          map.put(EVENT_TYPE, eventtype.name());
90          map.put(GROUP_ID, mavenProject.getGroupId());
91          map.put(ARTIFACT_ID, mavenProject.getArtifactId());
92          map.put(VERSION, mavenProject.getVersion());
93          map.put(BASE_DIR, mavenProject.getBasedir().getAbsolutePath());
94          return map;
95      }
96  
97      /**
98       * Describe the type of the event
99       */
100     public static enum EventType {
101         /**
102          * The project was started
103          */
104         ProjectStarted,
105         /**
106          * The project failed
107          */
108         ProjectFailed,
109         /**
110          * The project was skipped
111          */
112         ProjectSkipped,
113         /**
114          * the project succeed
115          */
116         ProjectSucceeded,
117         /**
118          * the type of event is unknown
119          */
120         Unknown;
121     }
122 }