1 package org.apache.maven.lifecycle.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.lifecycle.Lifecycle;
30 import org.apache.maven.lifecycle.LifecycleMappingDelegate;
31 import org.apache.maven.model.Plugin;
32 import org.apache.maven.model.PluginExecution;
33 import org.apache.maven.plugin.BuildPluginManager;
34 import org.apache.maven.plugin.InvalidPluginDescriptorException;
35 import org.apache.maven.plugin.MojoExecution;
36 import org.apache.maven.plugin.MojoNotFoundException;
37 import org.apache.maven.plugin.PluginDescriptorParsingException;
38 import org.apache.maven.plugin.PluginNotFoundException;
39 import org.apache.maven.plugin.PluginResolutionException;
40 import org.apache.maven.plugin.descriptor.MojoDescriptor;
41 import org.apache.maven.project.MavenProject;
42 import org.codehaus.plexus.component.annotations.Component;
43 import org.codehaus.plexus.component.annotations.Requirement;
44
45
46
47
48
49
50 @Component( role = LifecycleMappingDelegate.class, hint = DefaultLifecycleMappingDelegate.HINT )
51 public class DefaultLifecycleMappingDelegate
52 implements LifecycleMappingDelegate
53 {
54 public static final String HINT = "default";
55
56 @Requirement
57 private BuildPluginManager pluginManager;
58
59 public Map<String, List<MojoExecution>> calculateLifecycleMappings( MavenSession session, MavenProject project,
60 Lifecycle lifecycle, String lifecyclePhase )
61 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
62 MojoNotFoundException, InvalidPluginDescriptorException
63 {
64
65
66
67
68
69 Map<String, Map<Integer, List<MojoExecution>>> mappings =
70 new LinkedHashMap<>();
71
72 for ( String phase : lifecycle.getPhases() )
73 {
74 Map<Integer, List<MojoExecution>> phaseBindings = new TreeMap<>();
75
76 mappings.put( phase, phaseBindings );
77
78 if ( phase.equals( lifecyclePhase ) )
79 {
80 break;
81 }
82 }
83
84
85
86
87
88
89
90
91 for ( Plugin plugin : project.getBuild().getPlugins() )
92 {
93 for ( PluginExecution execution : plugin.getExecutions() )
94 {
95
96
97 if ( execution.getPhase() != null )
98 {
99 Map<Integer, List<MojoExecution>> phaseBindings = mappings.get( execution.getPhase() );
100 if ( phaseBindings != null )
101 {
102 for ( String goal : execution.getGoals() )
103 {
104 MojoExecution mojoExecution = new MojoExecution( plugin, goal, execution.getId() );
105 mojoExecution.setLifecyclePhase( execution.getPhase() );
106 addMojoExecution( phaseBindings, mojoExecution, execution.getPriority() );
107 }
108 }
109 }
110
111 else
112 {
113 for ( String goal : execution.getGoals() )
114 {
115 MojoDescriptor mojoDescriptor =
116 pluginManager.getMojoDescriptor( plugin, goal, project.getRemotePluginRepositories(),
117 session.getRepositorySession() );
118
119 Map<Integer, List<MojoExecution>> phaseBindings = mappings.get( mojoDescriptor.getPhase() );
120 if ( phaseBindings != null )
121 {
122 MojoExecution mojoExecution = new MojoExecution( mojoDescriptor, execution.getId() );
123 mojoExecution.setLifecyclePhase( mojoDescriptor.getPhase() );
124 addMojoExecution( phaseBindings, mojoExecution, execution.getPriority() );
125 }
126 }
127 }
128 }
129 }
130
131 Map<String, List<MojoExecution>> lifecycleMappings = new LinkedHashMap<>();
132
133 for ( Map.Entry<String, Map<Integer, List<MojoExecution>>> entry : mappings.entrySet() )
134 {
135 List<MojoExecution> mojoExecutions = new ArrayList<>();
136
137 for ( List<MojoExecution> executions : entry.getValue().values() )
138 {
139 mojoExecutions.addAll( executions );
140 }
141
142 lifecycleMappings.put( entry.getKey(), mojoExecutions );
143 }
144
145 return lifecycleMappings;
146
147 }
148
149 private void addMojoExecution( Map<Integer, List<MojoExecution>> phaseBindings, MojoExecution mojoExecution,
150 int priority )
151 {
152 List<MojoExecution> mojoExecutions = phaseBindings.get( priority );
153
154 if ( mojoExecutions == null )
155 {
156 mojoExecutions = new ArrayList<>();
157 phaseBindings.put( priority, mojoExecutions );
158 }
159
160 mojoExecutions.add( mojoExecution );
161 }
162
163 }