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.LinkedHashMap;
34 import java.util.Map;
35 import java.util.Set;
36
37
38
39
40
41
42
43
44
45
46 @Component( role = LifeCyclePluginAnalyzer.class )
47 public class DefaultLifecyclePluginAnalyzer
48 implements LifeCyclePluginAnalyzer
49 {
50
51 @Requirement( role = LifecycleMapping.class )
52 private Map<String, LifecycleMapping> lifecycleMappings;
53
54 @Requirement
55 private DefaultLifecycles defaultLifeCycles;
56
57 @Requirement
58 private Logger logger;
59
60 public DefaultLifecyclePluginAnalyzer()
61 {
62 }
63
64
65
66
67
68
69
70
71
72
73
74 public Set<Plugin> getPluginsBoundByDefaultToAllLifecycles( String packaging )
75 {
76 if ( logger.isDebugEnabled() )
77 {
78 logger.debug( "Looking up lifecyle mappings for packaging " + packaging + " from " +
79 Thread.currentThread().getContextClassLoader() );
80 }
81
82 LifecycleMapping lifecycleMappingForPackaging = lifecycleMappings.get( packaging );
83
84 if ( lifecycleMappingForPackaging == null )
85 {
86 return null;
87 }
88
89 Map<Plugin, Plugin> plugins = new LinkedHashMap<Plugin, Plugin>();
90
91 for ( Lifecycle lifecycle : defaultLifeCycles.getLifeCycles() )
92 {
93 org.apache.maven.lifecycle.mapping.Lifecycle lifecycleConfiguration =
94 lifecycleMappingForPackaging.getLifecycles().get( lifecycle.getId() );
95
96 Map<String, String> phaseToGoalMapping = null;
97
98 if ( lifecycleConfiguration != null )
99 {
100 phaseToGoalMapping = lifecycleConfiguration.getPhases();
101 }
102 else if ( lifecycle.getDefaultPhases() != null )
103 {
104 phaseToGoalMapping = lifecycle.getDefaultPhases();
105 }
106
107 if ( phaseToGoalMapping != null )
108 {
109
110
111
112
113 for ( Map.Entry<String, String> goalsForLifecyclePhase : phaseToGoalMapping.entrySet() )
114 {
115 String phase = goalsForLifecyclePhase.getKey();
116 String goals = goalsForLifecyclePhase.getValue();
117 if ( goals != null )
118 {
119 parseLifecyclePhaseDefinitions( plugins, phase, goals );
120 }
121 }
122 }
123 }
124
125 return plugins.keySet();
126 }
127
128 private void parseLifecyclePhaseDefinitions( Map<Plugin, Plugin> plugins, String phase, String goals )
129 {
130 String[] mojos = StringUtils.split( goals, "," );
131
132 for ( int i = 0; i < mojos.length; i++ )
133 {
134
135 String goal = mojos[i].trim();
136 String[] p = StringUtils.split( goal, ":" );
137
138 PluginExecution execution = new PluginExecution();
139 execution.setId( "default-" + p[p.length - 1] );
140 execution.setPhase( phase );
141 execution.setPriority( i - mojos.length );
142 execution.getGoals().add( p[p.length - 1] );
143
144 Plugin plugin = new Plugin();
145 plugin.setGroupId( p[0] );
146 plugin.setArtifactId( p[1] );
147 if ( p.length >= 4 )
148 {
149 plugin.setVersion( p[2] );
150 }
151
152 Plugin existing = plugins.get( plugin );
153 if ( existing != null )
154 {
155 if ( existing.getVersion() == null )
156 {
157 existing.setVersion( plugin.getVersion() );
158 }
159 plugin = existing;
160 }
161 else
162 {
163 plugins.put( plugin, plugin );
164 }
165
166 plugin.getExecutions().add( execution );
167 }
168 }
169
170
171 }