KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > plugin > descriptor > PluginDescriptor


1 package org.apache.maven.plugin.descriptor;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.artifact.ArtifactUtils;
20 import org.apache.maven.plugin.lifecycle.Lifecycle;
21 import org.apache.maven.plugin.lifecycle.LifecycleConfiguration;
22 import org.apache.maven.plugin.lifecycle.io.xpp3.LifecycleMappingsXpp3Reader;
23 import org.codehaus.classworlds.ClassRealm;
24 import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
25 import org.codehaus.plexus.util.IOUtil;
26 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
27
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl</a>
41  * @version $Id: PluginDescriptor.java 355404 2005-12-09 07:58:04Z brett $
42  */

43 public class PluginDescriptor
44     extends ComponentSetDescriptor
45 {
46     private String JavaDoc groupId;
47
48     private String JavaDoc artifactId;
49
50     private String JavaDoc version;
51
52     private String JavaDoc goalPrefix;
53
54     private String JavaDoc source;
55
56     private boolean inheritedByDefault = true;
57
58     private List JavaDoc artifacts;
59     
60     private Map JavaDoc lifecycleMappings;
61
62     private ClassRealm classRealm;
63
64     // calculated on-demand.
65
private Map JavaDoc artifactMap;
66
67     private Set JavaDoc introducedDependencyArtifacts;
68
69     private String JavaDoc name;
70
71     private String JavaDoc description;
72
73     // ----------------------------------------------------------------------
74
//
75
// ----------------------------------------------------------------------
76

77     public List JavaDoc getMojos()
78     {
79         return getComponents();
80     }
81
82     public void addMojo( MojoDescriptor mojoDescriptor )
83         throws DuplicateMojoDescriptorException
84     {
85         MojoDescriptor existing = null;
86         // this relies heavily on the equals() and hashCode() for ComponentDescriptor,
87
// which uses role:roleHint for identity...and roleHint == goalPrefix:goal.
88
// role does not vary for Mojos.
89
List JavaDoc mojos = getComponents();
90
91         if ( mojos != null && mojos.contains( mojoDescriptor ) )
92         {
93             int indexOf = mojos.indexOf( mojoDescriptor );
94
95             existing = (MojoDescriptor) mojos.get( indexOf );
96         }
97
98         if ( existing != null )
99         {
100             throw new DuplicateMojoDescriptorException( getGoalPrefix(), mojoDescriptor.getGoal(), existing
101                 .getImplementation(), mojoDescriptor.getImplementation() );
102         }
103         else
104         {
105             addComponentDescriptor( mojoDescriptor );
106         }
107     }
108
109     public String JavaDoc getGroupId()
110     {
111         return groupId;
112     }
113
114     public void setGroupId( String JavaDoc groupId )
115     {
116         this.groupId = groupId;
117     }
118
119     public String JavaDoc getArtifactId()
120     {
121         return artifactId;
122     }
123
124     public void setArtifactId( String JavaDoc artifactId )
125     {
126         this.artifactId = artifactId;
127     }
128
129     // ----------------------------------------------------------------------
130
// Dependencies
131
// ----------------------------------------------------------------------
132

133     public static String JavaDoc constructPluginKey( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc version )
134     {
135         return groupId + ":" + artifactId + ":" + version;
136     }
137
138     public String JavaDoc getPluginLookupKey()
139     {
140         return groupId + ":" + artifactId;
141     }
142
143     public String JavaDoc getId()
144     {
145         return constructPluginKey( groupId, artifactId, version );
146     }
147
148     public static String JavaDoc getDefaultPluginArtifactId( String JavaDoc id )
149     {
150         return "maven-" + id + "-plugin";
151     }
152
153     public static String JavaDoc getDefaultPluginGroupId()
154     {
155         return "org.apache.maven.plugins";
156     }
157
158     /**
159      * Parse maven-...-plugin.
160      *
161      * @todo move to plugin-tools-api as a default only
162      */

163     public static String JavaDoc getGoalPrefixFromArtifactId( String JavaDoc artifactId )
164     {
165         if ( "maven-plugin-plugin".equals( artifactId ) )
166         {
167             return "plugin";
168         }
169         else
170         {
171             return artifactId.replaceAll( "-?maven-?", "" ).replaceAll( "-?plugin-?", "" );
172         }
173     }
174
175     public String JavaDoc getGoalPrefix()
176     {
177         return goalPrefix;
178     }
179
180     public void setGoalPrefix( String JavaDoc goalPrefix )
181     {
182         this.goalPrefix = goalPrefix;
183     }
184
185     public void setVersion( String JavaDoc version )
186     {
187         this.version = version;
188     }
189
190     public String JavaDoc getVersion()
191     {
192         return version;
193     }
194
195     public void setSource( String JavaDoc source )
196     {
197         this.source = source;
198     }
199
200     public String JavaDoc getSource()
201     {
202         return source;
203     }
204
205     public boolean isInheritedByDefault()
206     {
207         return inheritedByDefault;
208     }
209
210     public void setInheritedByDefault( boolean inheritedByDefault )
211     {
212         this.inheritedByDefault = inheritedByDefault;
213     }
214
215     public List JavaDoc getArtifacts()
216     {
217         return artifacts;
218     }
219
220     public void setArtifacts( List JavaDoc artifacts )
221     {
222         this.artifacts = artifacts;
223
224         // clear the calculated artifactMap
225
artifactMap = null;
226     }
227
228     public Map JavaDoc getArtifactMap()
229     {
230         if ( artifactMap == null )
231         {
232             artifactMap = ArtifactUtils.artifactMapByVersionlessId( getArtifacts() );
233         }
234
235         return artifactMap;
236     }
237
238     public boolean equals( Object JavaDoc object )
239     {
240         if ( this == object )
241         {
242             return true;
243         }
244
245         return getId().equals( ( (PluginDescriptor) object ).getId() );
246     }
247
248     public int hashCode()
249     {
250         return 10 + getId().hashCode();
251     }
252
253     public MojoDescriptor getMojo( String JavaDoc goal )
254     {
255         // TODO: could we use a map? Maybe if the parent did that for components too, as this is too vulnerable to
256
// changes above not being propogated to the map
257

258         MojoDescriptor mojoDescriptor = null;
259         for ( Iterator JavaDoc i = getMojos().iterator(); i.hasNext() && mojoDescriptor == null; )
260         {
261             MojoDescriptor desc = (MojoDescriptor) i.next();
262             if ( goal.equals( desc.getGoal() ) )
263             {
264                 mojoDescriptor = desc;
265             }
266         }
267         return mojoDescriptor;
268     }
269
270     public Lifecycle getLifecycleMapping( String JavaDoc lifecycle )
271         throws IOException JavaDoc, XmlPullParserException
272     {
273         if ( lifecycleMappings == null )
274         {
275             LifecycleMappingsXpp3Reader reader = new LifecycleMappingsXpp3Reader();
276             InputStreamReader JavaDoc r = null;
277             LifecycleConfiguration config;
278
279             try
280             {
281                 InputStream JavaDoc resourceAsStream = classRealm.getResourceAsStream( "/META-INF/maven/lifecycle.xml" );
282                 if ( resourceAsStream == null )
283                 {
284                     throw new FileNotFoundException JavaDoc( "Unable to find /META-INF/maven/lifecycle.xml in the plugin" );
285                 }
286                 r = new InputStreamReader JavaDoc( resourceAsStream );
287                 config = reader.read( r, true );
288             }
289             finally
290             {
291                 IOUtil.close( r );
292             }
293
294             Map JavaDoc map = new HashMap JavaDoc();
295
296             for ( Iterator JavaDoc i = config.getLifecycles().iterator(); i.hasNext(); )
297             {
298                 Lifecycle l = (Lifecycle) i.next();
299                 map.put( l.getId(), l );
300             }
301
302             lifecycleMappings = map;
303         }
304         return (Lifecycle) lifecycleMappings.get( lifecycle );
305     }
306
307     public void setClassRealm( ClassRealm classRealm )
308     {
309         this.classRealm = classRealm;
310     }
311
312     public ClassRealm getClassRealm()
313     {
314         return classRealm;
315     }
316
317     public void setIntroducedDependencyArtifacts( Set JavaDoc introducedDependencyArtifacts )
318     {
319         this.introducedDependencyArtifacts = introducedDependencyArtifacts;
320     }
321
322     public Set JavaDoc getIntroducedDependencyArtifacts()
323     {
324         return introducedDependencyArtifacts != null ? introducedDependencyArtifacts : Collections.EMPTY_SET;
325     }
326     
327     public void setName( String JavaDoc name )
328     {
329         this.name = name;
330     }
331     
332     public String JavaDoc getName()
333     {
334         return name;
335     }
336     
337     public void setDescription( String JavaDoc description )
338     {
339         this.description = description;
340     }
341     
342     public String JavaDoc getDescription()
343     {
344         return description;
345     }
346 }
347
Popular Tags