1 package org.apache.maven.plugin.descriptor;
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.artifact.Artifact;
23 import org.apache.maven.artifact.ArtifactUtils;
24 import org.apache.maven.plugin.lifecycle.Lifecycle;
25 import org.apache.maven.plugin.lifecycle.LifecycleConfiguration;
26 import org.apache.maven.plugin.lifecycle.io.xpp3.LifecycleMappingsXpp3Reader;
27 import org.codehaus.classworlds.ClassRealm;
28 import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
29 import org.codehaus.plexus.util.IOUtil;
30 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
31
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Set;
42
43
44
45
46
47 public class PluginDescriptor
48 extends ComponentSetDescriptor
49 {
50 private String groupId;
51
52 private String artifactId;
53
54 private String version;
55
56 private String goalPrefix;
57
58 private String source;
59
60 private boolean inheritedByDefault = true;
61
62 private Artifact pluginArtifact;
63
64 private List artifacts;
65
66 private Map lifecycleMappings;
67
68 private ClassRealm classRealm;
69
70
71 private Map artifactMap;
72
73 private Set introducedDependencyArtifacts;
74
75 private String name;
76
77 private String description;
78
79
80
81
82
83 public List getMojos()
84 {
85 return getComponents();
86 }
87
88 public void addMojo( MojoDescriptor mojoDescriptor )
89 throws DuplicateMojoDescriptorException
90 {
91 MojoDescriptor existing = null;
92
93
94
95 List mojos = getComponents();
96
97 if ( mojos != null && mojos.contains( mojoDescriptor ) )
98 {
99 int indexOf = mojos.indexOf( mojoDescriptor );
100
101 existing = (MojoDescriptor) mojos.get( indexOf );
102 }
103
104 if ( existing != null )
105 {
106 throw new DuplicateMojoDescriptorException( getGoalPrefix(), mojoDescriptor.getGoal(), existing
107 .getImplementation(), mojoDescriptor.getImplementation() );
108 }
109 else
110 {
111 addComponentDescriptor( mojoDescriptor );
112 }
113 }
114
115 public String getGroupId()
116 {
117 return groupId;
118 }
119
120 public void setGroupId( String groupId )
121 {
122 this.groupId = groupId;
123 }
124
125 public String getArtifactId()
126 {
127 return artifactId;
128 }
129
130 public void setArtifactId( String artifactId )
131 {
132 this.artifactId = artifactId;
133 }
134
135
136
137
138
139 public static String constructPluginKey( String groupId, String artifactId, String version )
140 {
141 return groupId + ":" + artifactId + ":" + version;
142 }
143
144 private String lookupKey;
145
146 public String getPluginLookupKey()
147 {
148 if ( lookupKey == null )
149 {
150 lookupKey = ( groupId + ":" + artifactId ).intern();
151 }
152
153 return lookupKey;
154 }
155
156 private String id;
157
158 public String getId()
159 {
160 if ( id == null )
161 {
162 id = constructPluginKey( groupId, artifactId, version ).intern();
163 }
164
165 return id;
166 }
167
168 public static String getDefaultPluginArtifactId( String id )
169 {
170 return "maven-" + id + "-plugin";
171 }
172
173 public static String getDefaultPluginGroupId()
174 {
175 return "org.apache.maven.plugins";
176 }
177
178
179
180
181
182
183 public static String getGoalPrefixFromArtifactId( String artifactId )
184 {
185 if ( "maven-plugin-plugin".equals( artifactId ) )
186 {
187 return "plugin";
188 }
189 else
190 {
191 return artifactId.replaceAll( "-?maven-?", "" ).replaceAll( "-?plugin-?", "" );
192 }
193 }
194
195 public String getGoalPrefix()
196 {
197 return goalPrefix;
198 }
199
200 public void setGoalPrefix( String goalPrefix )
201 {
202 this.goalPrefix = goalPrefix;
203 }
204
205 public void setVersion( String version )
206 {
207 this.version = version;
208 }
209
210 public String getVersion()
211 {
212 return version;
213 }
214
215 public void setSource( String source )
216 {
217 this.source = source;
218 }
219
220 public String getSource()
221 {
222 return source;
223 }
224
225 public boolean isInheritedByDefault()
226 {
227 return inheritedByDefault;
228 }
229
230 public void setInheritedByDefault( boolean inheritedByDefault )
231 {
232 this.inheritedByDefault = inheritedByDefault;
233 }
234
235 public List getArtifacts()
236 {
237 return artifacts;
238 }
239
240 public void setArtifacts( List artifacts )
241 {
242 this.artifacts = artifacts;
243
244
245 artifactMap = null;
246 }
247
248 public Map getArtifactMap()
249 {
250 if ( artifactMap == null )
251 {
252 artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() );
253 }
254
255 return artifactMap;
256 }
257
258 public boolean equals( Object object )
259 {
260 if ( this == object )
261 {
262 return true;
263 }
264
265 return getId().equals( ( (PluginDescriptor) object ).getId() );
266 }
267
268 public int hashCode()
269 {
270 return 10 + getId().hashCode();
271 }
272
273 public MojoDescriptor getMojo( String goal )
274 {
275 if ( getMojos() == null )
276 {
277 return null;
278 }
279
280
281
282
283 MojoDescriptor mojoDescriptor = null;
284 for ( Iterator i = getMojos().iterator(); i.hasNext() && mojoDescriptor == null; )
285 {
286 MojoDescriptor desc = (MojoDescriptor) i.next();
287 if ( goal.equals( desc.getGoal() ) )
288 {
289 mojoDescriptor = desc;
290 }
291 }
292 return mojoDescriptor;
293 }
294
295 public Lifecycle getLifecycleMapping( String lifecycle )
296 throws IOException, XmlPullParserException
297 {
298 if ( lifecycleMappings == null )
299 {
300 LifecycleMappingsXpp3Reader reader = new LifecycleMappingsXpp3Reader();
301 InputStreamReader r = null;
302 LifecycleConfiguration config;
303
304 try
305 {
306 InputStream resourceAsStream = classRealm.getResourceAsStream( "/META-INF/maven/lifecycle.xml" );
307 if ( resourceAsStream == null )
308 {
309 throw new FileNotFoundException( "Unable to find /META-INF/maven/lifecycle.xml in the plugin" );
310 }
311 r = new InputStreamReader( resourceAsStream );
312 config = reader.read( r, true );
313 }
314 finally
315 {
316 IOUtil.close( r );
317 }
318
319 Map map = new HashMap();
320
321 for ( Iterator i = config.getLifecycles().iterator(); i.hasNext(); )
322 {
323 Lifecycle l = (Lifecycle) i.next();
324 map.put( l.getId(), l );
325 }
326
327 lifecycleMappings = map;
328 }
329 return (Lifecycle) lifecycleMappings.get( lifecycle );
330 }
331
332 public void setClassRealm( ClassRealm classRealm )
333 {
334 this.classRealm = classRealm;
335 }
336
337 public ClassRealm getClassRealm()
338 {
339 return classRealm;
340 }
341
342 public void setIntroducedDependencyArtifacts( Set introducedDependencyArtifacts )
343 {
344 this.introducedDependencyArtifacts = introducedDependencyArtifacts;
345 }
346
347 public Set getIntroducedDependencyArtifacts()
348 {
349 return introducedDependencyArtifacts != null ? introducedDependencyArtifacts : Collections.EMPTY_SET;
350 }
351
352 public void setName( String name )
353 {
354 this.name = name;
355 }
356
357 public String getName()
358 {
359 return name;
360 }
361
362 public void setDescription( String description )
363 {
364 this.description = description;
365 }
366
367 public String getDescription()
368 {
369 return description;
370 }
371
372 public Artifact getPluginArtifact()
373 {
374 return pluginArtifact;
375 }
376
377 public void setPluginArtifact( Artifact pluginArtifact )
378 {
379 this.pluginArtifact = pluginArtifact;
380 }
381 }