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.ArrayList;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.TreeMap;
30  
31  import org.apache.maven.execution.MavenSession;
32  import org.apache.maven.lifecycle.Lifecycle;
33  import org.apache.maven.lifecycle.LifecycleMappingDelegate;
34  import org.apache.maven.model.Plugin;
35  import org.apache.maven.model.PluginExecution;
36  import org.apache.maven.plugin.BuildPluginManager;
37  import org.apache.maven.plugin.InvalidPluginDescriptorException;
38  import org.apache.maven.plugin.MojoExecution;
39  import org.apache.maven.plugin.MojoNotFoundException;
40  import org.apache.maven.plugin.PluginDescriptorParsingException;
41  import org.apache.maven.plugin.PluginNotFoundException;
42  import org.apache.maven.plugin.PluginResolutionException;
43  import org.apache.maven.plugin.descriptor.MojoDescriptor;
44  import org.apache.maven.project.MavenProject;
45  
46  /**
47   * Lifecycle mapping delegate component interface. Calculates project build execution plan given {@link Lifecycle} and
48   * lifecycle phase. Standard lifecycles use plugin execution {@code <phase>} or mojo default lifecycle phase to
49   * calculate the execution plan, but custom lifecycles can use alternative mapping strategies.
50   */
51  @Singleton
52  @Named
53  public class DefaultLifecycleMappingDelegate implements LifecycleMappingDelegate {
54      @Deprecated
55      public static final String HINT = "default";
56  
57      @Inject
58      private BuildPluginManager pluginManager;
59  
60      public Map<String, List<MojoExecution>> calculateLifecycleMappings(
61              MavenSession session, MavenProject project, Lifecycle lifecycle, String lifecyclePhase)
62              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
63                      MojoNotFoundException, InvalidPluginDescriptorException {
64          /*
65           * Initialize mapping from lifecycle phase to bound mojos. The key set of this map denotes the phases the caller
66           * is interested in, i.e. all phases up to and including the specified phase.
67           */
68  
69          Map<String, Map<Integer, List<MojoExecution>>> mappings = new LinkedHashMap<>();
70  
71          for (String phase : lifecycle.getPhases()) {
72              Map<Integer, List<MojoExecution>> phaseBindings = new TreeMap<>();
73  
74              mappings.put(phase, phaseBindings);
75  
76              if (phase.equals(lifecyclePhase)) {
77                  break;
78              }
79          }
80  
81          /*
82           * Grab plugin executions that are bound to the selected lifecycle phases from project. The effective model of
83           * the project already contains the plugin executions induced by the project's packaging type. Remember, all
84           * phases of interest and only those are in the lifecycle mapping, if a phase has no value in the map, we are
85           * not interested in any of the executions bound to it.
86           */
87  
88          for (Plugin plugin : project.getBuild().getPlugins()) {
89              for (PluginExecution execution : plugin.getExecutions()) {
90                  // if the phase is specified then I don't have to go fetch the plugin yet and pull it down
91                  // to examine the phase it is associated to.
92                  if (execution.getPhase() != null) {
93                      Map<Integer, List<MojoExecution>> phaseBindings = mappings.get(execution.getPhase());
94                      if (phaseBindings != null) {
95                          for (String goal : execution.getGoals()) {
96                              MojoExecution mojoExecution = new MojoExecution(plugin, goal, execution.getId());
97                              mojoExecution.setLifecyclePhase(execution.getPhase());
98                              addMojoExecution(phaseBindings, mojoExecution, execution.getPriority());
99                          }
100                     }
101                 }
102                 // if not then i need to grab the mojo descriptor and look at the phase that is specified
103                 else {
104                     for (String goal : execution.getGoals()) {
105                         MojoDescriptor mojoDescriptor = pluginManager.getMojoDescriptor(
106                                 plugin, goal, project.getRemotePluginRepositories(), session.getRepositorySession());
107 
108                         Map<Integer, List<MojoExecution>> phaseBindings = mappings.get(mojoDescriptor.getPhase());
109                         if (phaseBindings != null) {
110                             MojoExecution mojoExecution = new MojoExecution(mojoDescriptor, execution.getId());
111                             mojoExecution.setLifecyclePhase(mojoDescriptor.getPhase());
112                             addMojoExecution(phaseBindings, mojoExecution, execution.getPriority());
113                         }
114                     }
115                 }
116             }
117         }
118 
119         Map<String, List<MojoExecution>> lifecycleMappings = new LinkedHashMap<>();
120 
121         for (Map.Entry<String, Map<Integer, List<MojoExecution>>> entry : mappings.entrySet()) {
122             List<MojoExecution> mojoExecutions = new ArrayList<>();
123 
124             for (List<MojoExecution> executions : entry.getValue().values()) {
125                 mojoExecutions.addAll(executions);
126             }
127 
128             lifecycleMappings.put(entry.getKey(), mojoExecutions);
129         }
130 
131         return lifecycleMappings;
132     }
133 
134     private void addMojoExecution(
135             Map<Integer, List<MojoExecution>> phaseBindings, MojoExecution mojoExecution, int priority) {
136         List<MojoExecution> mojoExecutions = phaseBindings.get(priority);
137 
138         if (mojoExecutions == null) {
139             mojoExecutions = new ArrayList<>();
140             phaseBindings.put(priority, mojoExecutions);
141         }
142 
143         mojoExecutions.add(mojoExecution);
144     }
145 }