View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.lifecycle.internal;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.HashSet;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.apache.maven.lifecycle.DefaultLifecycles;
32  import org.apache.maven.lifecycle.LifeCyclePluginAnalyzer;
33  import org.apache.maven.lifecycle.Lifecycle;
34  import org.apache.maven.lifecycle.mapping.LifecycleMapping;
35  import org.apache.maven.lifecycle.mapping.LifecycleMojo;
36  import org.apache.maven.lifecycle.mapping.LifecyclePhase;
37  import org.apache.maven.model.InputLocation;
38  import org.apache.maven.model.InputSource;
39  import org.apache.maven.model.Plugin;
40  import org.apache.maven.model.PluginExecution;
41  import org.codehaus.plexus.util.StringUtils;
42  import org.codehaus.plexus.util.xml.Xpp3Dom;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  /**
47   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
48   *
49   * @since 3.0
50   * @author Benjamin Bentmann
51   * @author Jason van Zyl
52   * @author jdcasey
53   * @author Kristian Rosenvold (extracted class only)
54   */
55  @Singleton
56  @Named
57  public class DefaultLifecyclePluginAnalyzer implements LifeCyclePluginAnalyzer {
58  
59      private final Logger logger = LoggerFactory.getLogger(getClass());
60  
61      @Inject
62      private Map<String, LifecycleMapping> lifecycleMappings;
63  
64      @Inject
65      private DefaultLifecycles defaultLifeCycles;
66  
67      public DefaultLifecyclePluginAnalyzer() {}
68  
69      // These methods deal with construction intact Plugin object that look like they come from a standard
70      // <plugin/> block in a Maven POM. We have to do some wiggling to pull the sources of information
71      // together and this really shows the problem of constructing a sensible default configuration but
72      // it's all encapsulated here so it appears normalized to the POM builder.
73  
74      // We are going to take the project packaging and find all plugins in the default lifecycle and create
75      // fully populated Plugin objects, including executions with goals and default configuration taken
76      // from the plugin.xml inside a plugin.
77      //
78  
79      public Set<Plugin> getPluginsBoundByDefaultToAllLifecycles(String packaging) {
80          if (logger.isDebugEnabled()) {
81              logger.debug("Looking up lifecycle mappings for packaging " + packaging + " from "
82                      + Thread.currentThread().getContextClassLoader());
83          }
84  
85          // look up directly: the injected map filters by visibility and returns the highest-ranked mapping,
86          // so a build extension can override a packaging that core already maps
87          LifecycleMapping lifecycleMappingForPackaging = lifecycleMappings.get(packaging);
88  
89          if (lifecycleMappingForPackaging == null) {
90              return null;
91          }
92  
93          Map<Plugin, Plugin> plugins = new LinkedHashMap<>();
94  
95          for (Lifecycle lifecycle : defaultLifeCycles.getLifeCycles()) {
96              org.apache.maven.lifecycle.mapping.Lifecycle lifecycleConfiguration =
97                      lifecycleMappingForPackaging.getLifecycles().get(lifecycle.getId());
98  
99              Map<String, LifecyclePhase> phaseToGoalMapping = null;
100 
101             if (lifecycleConfiguration != null) {
102                 phaseToGoalMapping = lifecycleConfiguration.getLifecyclePhases();
103             } else if (lifecycle.getDefaultLifecyclePhases() != null) {
104                 phaseToGoalMapping = lifecycle.getDefaultLifecyclePhases();
105             }
106 
107             if (phaseToGoalMapping != null) {
108                 for (Map.Entry<String, LifecyclePhase> goalsForLifecyclePhase : phaseToGoalMapping.entrySet()) {
109                     String phase = goalsForLifecyclePhase.getKey();
110                     LifecyclePhase goals = goalsForLifecyclePhase.getValue();
111                     if (goals != null) {
112                         parseLifecyclePhaseDefinitions(plugins, phase, goals);
113                     }
114                 }
115             }
116         }
117 
118         return plugins.keySet();
119     }
120 
121     private void parseLifecyclePhaseDefinitions(Map<Plugin, Plugin> plugins, String phase, LifecyclePhase goals) {
122         String modelId = "org.apache.maven:maven-core:"
123                 + this.getClass().getPackage().getImplementationVersion() + ":default-lifecycle-bindings";
124         InputSource inputSource = new InputSource();
125         inputSource.setModelId(modelId);
126         InputLocation location = new InputLocation(-1, -1, inputSource);
127         location.setLocation(0, location);
128 
129         List<LifecycleMojo> mojos = goals.getMojos();
130         if (mojos != null) {
131 
132             for (int i = 0; i < mojos.size(); i++) {
133                 LifecycleMojo mojo = mojos.get(i);
134 
135                 GoalSpec gs = parseGoalSpec(mojo.getGoal());
136 
137                 if (gs == null) {
138                     logger.warn("Ignored invalid goal specification '" + mojo.getGoal()
139                             + "' from lifecycle mapping for phase " + phase);
140                     continue;
141                 }
142 
143                 Plugin plugin = new Plugin();
144                 plugin.setGroupId(gs.groupId);
145                 plugin.setArtifactId(gs.artifactId);
146                 plugin.setVersion(gs.version);
147 
148                 plugin.setLocation("", location);
149                 plugin.setLocation("groupId", location);
150                 plugin.setLocation("artifactId", location);
151                 plugin.setLocation("version", location);
152 
153                 Plugin existing = plugins.get(plugin);
154                 if (existing != null) {
155                     if (existing.getVersion() == null) {
156                         existing.setVersion(plugin.getVersion());
157                         existing.setLocation("version", location);
158                     }
159                     plugin = existing;
160                 } else {
161                     plugins.put(plugin, plugin);
162                 }
163 
164                 PluginExecution execution = new PluginExecution();
165                 execution.setId(getExecutionId(plugin, gs.goal));
166                 execution.setPhase(phase);
167                 execution.setPriority(i - mojos.size());
168                 execution.getGoals().add(gs.goal);
169 
170                 execution.setLocation("", location);
171                 execution.setLocation("id", location);
172                 execution.setLocation("phase", location);
173                 execution.setLocation("goals", location);
174 
175                 Xpp3Dom lifecycleConfiguration = mojo.getConfiguration();
176                 if (lifecycleConfiguration != null) {
177                     execution.setConfiguration(new Xpp3Dom(lifecycleConfiguration));
178                 }
179 
180                 plugin.setDependencies(mojo.getDependencies());
181                 plugin.getExecutions().add(execution);
182             }
183         }
184     }
185 
186     private GoalSpec parseGoalSpec(String goalSpec) {
187         GoalSpec gs = new GoalSpec();
188 
189         String[] p = StringUtils.split(goalSpec.trim(), ":");
190 
191         if (p.length == 3) {
192             // <groupId>:<artifactId>:<goal>
193             gs.groupId = p[0];
194             gs.artifactId = p[1];
195             gs.goal = p[2];
196         } else if (p.length == 4) {
197             // <groupId>:<artifactId>:<version>:<goal>
198             gs.groupId = p[0];
199             gs.artifactId = p[1];
200             gs.version = p[2];
201             gs.goal = p[3];
202         } else {
203             // invalid
204             gs = null;
205         }
206 
207         return gs;
208     }
209 
210     private String getExecutionId(Plugin plugin, String goal) {
211         Set<String> existingIds = new HashSet<>();
212         for (PluginExecution execution : plugin.getExecutions()) {
213             existingIds.add(execution.getId());
214         }
215 
216         String base = "default-" + goal;
217         String id = base;
218 
219         for (int index = 1; existingIds.contains(id); index++) {
220             id = base + '-' + index;
221         }
222 
223         return id;
224     }
225 
226     static class GoalSpec {
227 
228         String groupId;
229 
230         String artifactId;
231 
232         String version;
233 
234         String goal;
235     }
236 }