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.io.IOException;
17  import java.io.StringWriter;
18  import java.nio.file.Path;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Objects;
23  import java.util.stream.Stream;
24  
25  import org.apache.maven.model.Model;
26  import org.apache.maven.model.io.DefaultModelWriter;
27  import org.apache.maven.project.MavenProject;
28  
29  /**
30   * Message send to inform about reactor project in the build and their effective
31   * model
32   */
33  public class ProjectsMessage extends Message {
34  
35      private static final DefaultModelWriter MODEL_WRITER = new DefaultModelWriter();
36  
37      ProjectsMessage(String sessionId, long threadId, Map<String, String> payload) {
38          super(sessionId, threadId, payload);
39      }
40  
41      /**
42       * @param projects the projects to send
43       */
44      public ProjectsMessage(Collection<MavenProject> projects) {
45          super(buildMap(projects));
46      }
47  
48      /**
49       * @return a stream of project infos
50       */
51      public Stream<ProjectInfo> projects() {
52          return keys().map(key -> {
53                      String[] gav = key.split("\t");
54                      if (gav.length == 5 && "p".equals(gav[0])) {
55                          ProjectInfo pi = new ProjectInfo() {
56  
57                              @Override
58                              public String getGroupId() {
59                                  return gav[1];
60                              }
61  
62                              @Override
63                              public String getArtifactId() {
64                                  return gav[2];
65                              }
66  
67                              @Override
68                              public String getVersion() {
69                                  return gav[3];
70                              }
71  
72                              @Override
73                              public String getModel() {
74                                  return getProperty(key);
75                              }
76  
77                              @Override
78                              public Path getBaseDir() {
79                                  return new File(gav[4]).toPath();
80                              }
81                          };
82                          return pi;
83                      }
84                      return null;
85                  })
86                  .filter(Objects::nonNull);
87      }
88  
89      private static Map<String, String> buildMap(Collection<MavenProject> projects) {
90          Map<String, String> map = new HashMap<>();
91          for (MavenProject project : projects) {
92              String key = String.format(
93                      "p\t%s\t%s\t%s\t%s",
94                      project.getGroupId(),
95                      project.getArtifactId(),
96                      project.getVersion(),
97                      project.getBasedir().getAbsolutePath());
98              map.put(key, getEffectiveModel(project));
99          }
100         return map;
101     }
102 
103     private static String getEffectiveModel(MavenProject project) {
104         Model model = project.getModel();
105         StringWriter writer = new StringWriter();
106         try {
107             MODEL_WRITER.write(writer, null, model);
108         } catch (IOException e) {
109         }
110         String string = writer.toString();
111         return string;
112     }
113 
114     /**
115      * Holds basic project info
116      */
117     public static interface ProjectInfo {
118         /**
119          * @return the group id of the reactor project
120          */
121         String getGroupId();
122 
123         /**
124          * @return the artifact id of the reactor project
125          */
126         String getArtifactId();
127 
128         /**
129          * @return the version of the reactor project
130          */
131         String getVersion();
132 
133         /**
134          * @return the effective model of the project
135          */
136         String getModel();
137 
138         /**
139          * @return the basedir of the project
140          */
141         Path getBaseDir();
142     }
143 }