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