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 org.apache.maven.lifecycle.DefaultLifecycles;
23  import org.apache.maven.lifecycle.LifeCyclePluginAnalyzer;
24  import org.apache.maven.lifecycle.Lifecycle;
25  import org.apache.maven.lifecycle.mapping.LifecycleMapping;
26  import org.apache.maven.model.Plugin;
27  import org.apache.maven.model.PluginExecution;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.codehaus.plexus.component.annotations.Requirement;
30  import org.codehaus.plexus.logging.Logger;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  import java.util.ArrayList;
34  import java.util.Collections;
35  import java.util.Comparator;
36  import java.util.HashSet;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Set;
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  @Component( role = LifeCyclePluginAnalyzer.class )
52  public class DefaultLifecyclePluginAnalyzer
53      implements LifeCyclePluginAnalyzer
54  {
55  
56      @Requirement( role = LifecycleMapping.class )
57      private Map<String, LifecycleMapping> lifecycleMappings;
58  
59      @Requirement
60      private DefaultLifecycles defaultLifeCycles;
61  
62      @Requirement
63      private Logger logger;
64  
65      public DefaultLifecyclePluginAnalyzer()
66      {
67      }
68  
69      
70      
71      
72      
73  
74      
75      
76      
77      
78  
79      public Set<Plugin> getPluginsBoundByDefaultToAllLifecycles( String packaging )
80      {
81          if ( logger.isDebugEnabled() )
82          {
83              logger.debug( "Looking up lifecyle mappings for packaging " + packaging + " from "
84                  + Thread.currentThread().getContextClassLoader() );
85          }
86  
87          LifecycleMapping lifecycleMappingForPackaging = lifecycleMappings.get( packaging );
88  
89          if ( lifecycleMappingForPackaging == null )
90          {
91              return null;
92          }
93  
94          Map<Plugin, Plugin> plugins = new LinkedHashMap<Plugin, Plugin>();
95  
96          for ( Lifecycle lifecycle : getOrderedLifecycles() )
97          {
98              org.apache.maven.lifecycle.mapping.Lifecycle lifecycleConfiguration =
99                  lifecycleMappingForPackaging.getLifecycles().get( lifecycle.getId() );
100 
101             Map<String, String> phaseToGoalMapping = null;
102 
103             if ( lifecycleConfiguration != null )
104             {
105                 phaseToGoalMapping = lifecycleConfiguration.getPhases();
106             }
107             else if ( lifecycle.getDefaultPhases() != null )
108             {
109                 phaseToGoalMapping = lifecycle.getDefaultPhases();
110             }
111 
112             if ( phaseToGoalMapping != null )
113             {
114                 
115                 
116                 
117                 
118                 for ( Map.Entry<String, String> goalsForLifecyclePhase : phaseToGoalMapping.entrySet() )
119                 {
120                     String phase = goalsForLifecyclePhase.getKey();
121                     String goals = goalsForLifecyclePhase.getValue();
122                     if ( goals != null )
123                     {
124                         parseLifecyclePhaseDefinitions( plugins, phase, goals );
125                     }
126                 }
127             }
128         }
129 
130         return plugins.keySet();
131     }
132 
133     private List<Lifecycle> getOrderedLifecycles()
134     {
135         
136 
137         List<Lifecycle> lifecycles = new ArrayList<Lifecycle>( defaultLifeCycles.getLifeCycles() );
138 
139         Collections.sort( lifecycles, new Comparator<Lifecycle>()
140         {
141 
142             public int compare( Lifecycle l1, Lifecycle l2 )
143             {
144                 return l1.getId().compareTo( l2.getId() );
145             }
146 
147         } );
148 
149         return lifecycles;
150     }
151 
152     private void parseLifecyclePhaseDefinitions( Map<Plugin, Plugin> plugins, String phase, String goals )
153     {
154         String[] mojos = StringUtils.split( goals, "," );
155 
156         for ( int i = 0; i < mojos.length; i++ )
157         {
158             GoalSpec gs = parseGoalSpec( mojos[i].trim() );
159 
160             if ( gs == null )
161             {
162                 logger.warn( "Ignored invalid goal specification '" + mojos[i] + "' from lifecycle mapping for phase "
163                     + phase );
164                 continue;
165             }
166 
167             Plugin plugin = new Plugin();
168             plugin.setGroupId( gs.groupId );
169             plugin.setArtifactId( gs.artifactId );
170             plugin.setVersion( gs.version );
171 
172             Plugin existing = plugins.get( plugin );
173             if ( existing != null )
174             {
175                 if ( existing.getVersion() == null )
176                 {
177                     existing.setVersion( plugin.getVersion() );
178                 }
179                 plugin = existing;
180             }
181             else
182             {
183                 plugins.put( plugin, plugin );
184             }
185 
186             PluginExecution execution = new PluginExecution();
187             execution.setId( getExecutionId( plugin, gs.goal ) );
188             execution.setPhase( phase );
189             execution.setPriority( i - mojos.length );
190             execution.getGoals().add( gs.goal );
191 
192             plugin.getExecutions().add( execution );
193         }
194     }
195 
196     private GoalSpec parseGoalSpec( String goalSpec )
197     {
198         GoalSpec gs = new GoalSpec();
199 
200         String[] p = StringUtils.split( goalSpec.trim(), ":" );
201 
202         if ( p.length == 3 )
203         {
204             
205             gs.groupId = p[0];
206             gs.artifactId = p[1];
207             gs.goal = p[2];
208         }
209         else if ( p.length == 4 )
210         {
211             
212             gs.groupId = p[0];
213             gs.artifactId = p[1];
214             gs.version = p[2];
215             gs.goal = p[3];
216         }
217         else
218         {
219             
220             gs = null;
221         }
222 
223         return gs;
224     }
225 
226     private String getExecutionId( Plugin plugin, String goal )
227     {
228         Set<String> existingIds = new HashSet<String>();
229         for ( PluginExecution execution : plugin.getExecutions() )
230         {
231             existingIds.add( execution.getId() );
232         }
233 
234         String base = "default-" + goal;
235         String id = base;
236 
237         for ( int index = 1; existingIds.contains( id ); index++ )
238         {
239             id = base + '-' + index;
240         }
241 
242         return id;
243     }
244 
245     static class GoalSpec
246     {
247 
248         String groupId;
249 
250         String artifactId;
251 
252         String version;
253 
254         String goal;
255 
256     }
257 
258 }