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