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