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