View Javadoc

1   package org.apache.maven.plugin.descriptor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.Mojo;
23  import org.codehaus.plexus.component.repository.ComponentDescriptor;
24  import org.codehaus.plexus.configuration.PlexusConfiguration;
25  import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
26  
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.LinkedList;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * The bean containing the Mojo descriptor.
35   * <br/>
36   * For more information about the usage tag, have a look to:
37   * <a href="http://maven.apache.org/developers/mojo-api-specification.html">http://maven.apache.org/developers/mojo-api-specification.html</a>
38   *
39   * @todo is there a need for the delegation of MavenMojoDescriptor to this?
40   * Why not just extend ComponentDescriptor here?
41   * @version $Id: MojoDescriptor.java 640545 2008-03-24 19:50:56Z bentmann $
42   */
43  public class MojoDescriptor
44      extends ComponentDescriptor
45      implements Cloneable
46  {
47      /** The Plexus component type */
48      public static String MAVEN_PLUGIN = "maven-plugin";
49  
50      /** "once-per-session" execution strategy */
51      public static final String SINGLE_PASS_EXEC_STRATEGY = "once-per-session";
52  
53      /** "always" execution strategy */
54      public static final String MULTI_PASS_EXEC_STRATEGY = "always";
55  
56      private static final String DEFAULT_INSTANTIATION_STRATEGY = "per-lookup";
57  
58      private static final String DEFAULT_LANGUAGE = "java";
59  
60      private List parameters;
61  
62      private Map parameterMap;
63  
64      /** By default, the execution strategy is "once-per-session" */
65      private String executionStrategy = SINGLE_PASS_EXEC_STRATEGY;
66  
67      /** The goal name of the Mojo */
68      private String goal;
69  
70      /** Reference the binded phase name of the Mojo */
71      private String phase;
72  
73      /** Specify the version when the Mojo was added to the API. Similar to Javadoc since. */
74      private String since;
75  
76      /** Reference the invocation phase of the Mojo */
77      private String executePhase;
78  
79      /** Reference the invocation goal of the Mojo */
80      private String executeGoal;
81  
82      /** Reference the invocation lifecycle of the Mojo */
83      private String executeLifecycle;
84  
85      /** Specify the version when the Mojo was deprecated to the API. Similar to Javadoc deprecated. */
86      private String deprecated;
87  
88      /** By default, no need to aggregate the Maven project and its child modules */
89      private boolean aggregator = false;
90  
91      // ----------------------------------------------------------------------
92      //
93      // ----------------------------------------------------------------------
94  
95      /** Specify the required dependencies in a specified scope */
96      private String dependencyResolutionRequired = null;
97  
98      /**  By default, the Mojo needs a Maven project to be executed */
99      private boolean projectRequired = true;
100 
101     /**  By default, the Mojo is online */
102     private boolean onlineRequired = false;
103 
104     /**  Plugin configuration */
105     private PlexusConfiguration mojoConfiguration;
106 
107     /**  Plugin descriptor */
108     private PluginDescriptor pluginDescriptor;
109 
110     /**  By default, the Mojo is herited */
111     private boolean inheritedByDefault = true;
112 
113     /**  By default, the Mojo could not be invoke directly */
114     private boolean directInvocationOnly = false;
115 
116     /**  By default, the Mojo don't need reports to run */
117     private boolean requiresReports = false;
118 
119     /**
120      * Default constructor.
121      */
122     public MojoDescriptor()
123     {
124         setInstantiationStrategy( DEFAULT_INSTANTIATION_STRATEGY );
125         setComponentFactory( DEFAULT_LANGUAGE );
126     }
127 
128     // ----------------------------------------------------------------------
129     //
130     // ----------------------------------------------------------------------
131 
132     /**
133      * @return the language of this Mojo, i.e. <code>java</code>
134      */
135     public String getLanguage()
136     {
137         return getComponentFactory();
138     }
139 
140     /**
141      * @param language the new language
142      */
143     public void setLanguage( String language )
144     {
145         setComponentFactory( language );
146     }
147 
148     /**
149      * @return <code>true</code> if the Mojo is deprecated, <code>false</code> otherwise.
150      */
151     public String getDeprecated()
152     {
153         return deprecated;
154     }
155 
156     /**
157      * @param deprecated <code>true</code> to deprecate the Mojo, <code>false</code> otherwise.
158      */
159     public void setDeprecated( String deprecated )
160     {
161         this.deprecated = deprecated;
162     }
163 
164     /**
165      * @return the list of parameters
166      */
167     public List getParameters()
168     {
169         return parameters;
170     }
171 
172     /**
173      * @param parameters the new list of parameters
174      * @throws DuplicateParameterException if any
175      */
176     public void setParameters( List parameters )
177         throws DuplicateParameterException
178     {
179         for ( Iterator it = parameters.iterator(); it.hasNext(); )
180         {
181             Parameter parameter = (Parameter) it.next();
182             addParameter( parameter );
183         }
184     }
185 
186     /**
187      * @param parameter add a new parameter
188      * @throws DuplicateParameterException if any
189      */
190     public void addParameter( Parameter parameter )
191         throws DuplicateParameterException
192     {
193         if ( parameters != null && parameters.contains( parameter ) )
194         {
195             throw new DuplicateParameterException( parameter.getName() +
196                 " has been declared multiple times in mojo with goal: " + getGoal() + " (implementation: " +
197                 getImplementation() + ")" );
198         }
199 
200         if ( parameters == null )
201         {
202             parameters = new LinkedList();
203         }
204 
205         parameters.add( parameter );
206     }
207 
208     /**
209      * @return the list parameters as a Map
210      */
211     public Map getParameterMap()
212     {
213         if ( parameterMap == null )
214         {
215             parameterMap = new HashMap();
216 
217             if ( parameters != null )
218             {
219                 for ( Iterator iterator = parameters.iterator(); iterator.hasNext(); )
220                 {
221                     Parameter pd = (Parameter) iterator.next();
222 
223                     parameterMap.put( pd.getName(), pd );
224                 }
225             }
226         }
227 
228         return parameterMap;
229     }
230 
231     // ----------------------------------------------------------------------
232     // Dependency requirement
233     // ----------------------------------------------------------------------
234 
235     /**
236      * @param requiresDependencyResolution the new required dependencies in a specified scope
237      */
238     public void setDependencyResolutionRequired( String requiresDependencyResolution )
239     {
240         this.dependencyResolutionRequired = requiresDependencyResolution;
241     }
242 
243     /**
244      * @return the required dependencies in a specified scope
245      * @TODO the name is not intelligible
246      */
247     public String isDependencyResolutionRequired()
248     {
249         return dependencyResolutionRequired;
250     }
251 
252     // ----------------------------------------------------------------------
253     // Project requirement
254     // ----------------------------------------------------------------------
255 
256     /**
257      * @param requiresProject <code>true</code> if the Mojo needs a Maven project to be executed, <code>false</code> otherwise.
258      */
259     public void setProjectRequired( boolean requiresProject )
260     {
261         this.projectRequired = requiresProject;
262     }
263 
264     /**
265      * @return <code>true</code> if the Mojo needs a Maven project to be executed, <code>false</code> otherwise.
266      */
267     public boolean isProjectRequired()
268     {
269         return projectRequired;
270     }
271 
272     // ----------------------------------------------------------------------
273     // Online vs. Offline requirement
274     // ----------------------------------------------------------------------
275 
276     /**
277      * @param requiresOnline <code>true</code> if the Mojo is online, <code>false</code> otherwise.
278      */
279     public void setOnlineRequired( boolean requiresOnline )
280     {
281         this.onlineRequired = requiresOnline;
282     }
283 
284     /**
285      * @return <code>true</code> if the Mojo is online, <code>false</code> otherwise.
286      */
287     // blech! this isn't even intelligible as a method name. provided for
288     // consistency...
289     public boolean isOnlineRequired()
290     {
291         return onlineRequired;
292     }
293 
294     /**
295      * @return <code>true</code> if the Mojo is online, <code>false</code> otherwise.
296      */
297     // more english-friendly method...keep the code clean! :)
298     public boolean requiresOnline()
299     {
300         return onlineRequired;
301     }
302 
303     /**
304      * @return the binded phase name of the Mojo
305      */
306     public String getPhase()
307     {
308         return phase;
309     }
310 
311     /**
312      * @param phase the new binded phase name of the Mojo
313      */
314     public void setPhase( String phase )
315     {
316         this.phase = phase;
317     }
318 
319     /**
320      * @return the version when the Mojo was added to the API
321      */
322     public String getSince()
323     {
324         return since;
325     }
326 
327     /**
328      * @param since the new version when the Mojo was added to the API
329      */
330     public void setSince( String since )
331     {
332         this.since = since;
333     }
334 
335     /**
336      * @return The goal name of the Mojo
337      */
338     public String getGoal()
339     {
340         return goal;
341     }
342 
343     /**
344      * @param goal The new goal name of the Mojo
345      */
346     public void setGoal( String goal )
347     {
348         this.goal = goal;
349     }
350 
351     /**
352      * @return the invocation phase of the Mojo
353      */
354     public String getExecutePhase()
355     {
356         return executePhase;
357     }
358 
359     /**
360      * @param executePhase the new invocation phase of the Mojo
361      */
362     public void setExecutePhase( String executePhase )
363     {
364         this.executePhase = executePhase;
365     }
366 
367     /**
368      * @return <code>true</code> if the Mojo uses <code>always</code> for the <code>executionStrategy</code>
369      */
370     public boolean alwaysExecute()
371     {
372         return MULTI_PASS_EXEC_STRATEGY.equals( executionStrategy );
373     }
374 
375     /**
376      * @return the execution strategy
377      */
378     public String getExecutionStrategy()
379     {
380         return executionStrategy;
381     }
382 
383     /**
384      * @param executionStrategy the new execution strategy
385      */
386     public void setExecutionStrategy( String executionStrategy )
387     {
388         this.executionStrategy = executionStrategy;
389     }
390 
391     /**
392      * @return the mojo configuration
393      */
394     public PlexusConfiguration getMojoConfiguration()
395     {
396         if ( mojoConfiguration == null )
397         {
398             mojoConfiguration = new XmlPlexusConfiguration( "configuration" );
399         }
400         return mojoConfiguration;
401     }
402 
403     /**
404      * @param mojoConfiguration a new mojo configuration
405      */
406     public void setMojoConfiguration( PlexusConfiguration mojoConfiguration )
407     {
408         this.mojoConfiguration = mojoConfiguration;
409     }
410 
411     /** {@inheritDoc} */
412     public String getRole()
413     {
414         return Mojo.ROLE;
415     }
416 
417     /** {@inheritDoc} */
418     public String getRoleHint()
419     {
420         return getId();
421     }
422 
423     /**
424      * @return the id of the mojo, based on the goal name
425      */
426     public String getId()
427     {
428         return getPluginDescriptor().getId() + ":" + getGoal();
429     }
430 
431     /**
432      * @return the full goal name
433      * @see PluginDescriptor#getGoalPrefix()
434      * @see #getGoal()
435      */
436     public String getFullGoalName()
437     {
438         return getPluginDescriptor().getGoalPrefix() + ":" + getGoal();
439     }
440 
441     /** {@inheritDoc} */
442     public String getComponentType()
443     {
444         return MAVEN_PLUGIN;
445     }
446 
447     /**
448      * @return the plugin descriptor
449      */
450     public PluginDescriptor getPluginDescriptor()
451     {
452         return pluginDescriptor;
453     }
454 
455     /**
456      * @param pluginDescriptor the new plugin descriptor
457      */
458     public void setPluginDescriptor( PluginDescriptor pluginDescriptor )
459     {
460         this.pluginDescriptor = pluginDescriptor;
461     }
462 
463     /**
464      * @return <code>true</code> if the Mojo is herited, <code>false</code> otherwise.
465      */
466     public boolean isInheritedByDefault()
467     {
468         return inheritedByDefault;
469     }
470 
471     /**
472      * @param inheritedByDefault <code>true</code> if the Mojo is herited, <code>false</code> otherwise.
473      */
474     public void setInheritedByDefault( boolean inheritedByDefault )
475     {
476         this.inheritedByDefault = inheritedByDefault;
477     }
478 
479     /** {@inheritDoc} */
480     public boolean equals( Object object )
481     {
482         if ( this == object )
483         {
484             return true;
485         }
486 
487         if ( object instanceof MojoDescriptor )
488         {
489             MojoDescriptor other = (MojoDescriptor) object;
490 
491             if ( !compareObjects( getPluginDescriptor(), other.getPluginDescriptor() ) )
492             {
493                 return false;
494             }
495 
496             if ( !compareObjects( getGoal(), other.getGoal() ) )
497             {
498                 return false;
499             }
500 
501             return true;
502         }
503 
504         return false;
505     }
506 
507     private boolean compareObjects( Object first, Object second )
508     {
509         if ( ( first == null && second != null ) || ( first != null && second == null ) )
510         {
511             return false;
512         }
513 
514         if ( !first.equals( second ) )
515         {
516             return false;
517         }
518 
519         return true;
520     }
521 
522     /** {@inheritDoc} */
523     public int hashCode()
524     {
525         int result = 1;
526 
527         String goal = getGoal();
528 
529         if ( goal != null )
530         {
531             result += goal.hashCode();
532         }
533 
534         PluginDescriptor pd = getPluginDescriptor();
535 
536         if ( pd != null )
537         {
538             result -= pd.hashCode();
539         }
540 
541         return result;
542     }
543 
544     /**
545      * @return the invocation lifecycle of the Mojo
546      */
547     public String getExecuteLifecycle()
548     {
549         return executeLifecycle;
550     }
551 
552     /**
553      * @param executeLifecycle the new invocation lifecycle of the Mojo
554      */
555     public void setExecuteLifecycle( String executeLifecycle )
556     {
557         this.executeLifecycle = executeLifecycle;
558     }
559 
560     /**
561      * @param aggregator <code>true</code> if the Mojo uses the Maven project and its child modules, <code>false</code> otherwise.
562      */
563     public void setAggregator( boolean aggregator )
564     {
565         this.aggregator = aggregator;
566     }
567 
568     /**
569      * @return <code>true</code> if the Mojo uses the Maven project and its child modules, <code>false</code> otherwise.
570      */
571     public boolean isAggregator()
572     {
573         return aggregator;
574     }
575 
576     /**
577      * @return <code>true</code> if the Mojo could not be invoke directly, <code>false</code> otherwise.
578      */
579     public boolean isDirectInvocationOnly()
580     {
581         return directInvocationOnly;
582     }
583 
584     /**
585      * @param directInvocationOnly <code>true</code> if the Mojo could not be invoke directly, <code>false</code> otherwise.
586      */
587     public void setDirectInvocationOnly( boolean directInvocationOnly )
588     {
589         this.directInvocationOnly = directInvocationOnly;
590     }
591 
592     /**
593      * @return <code>true</code> if the Mojo needs reports to run, <code>false</code> otherwise.
594      */
595     public boolean isRequiresReports()
596     {
597         return requiresReports;
598     }
599 
600     /**
601      * @param requiresReports <code>true</code> if the Mojo needs reports to run, <code>false</code> otherwise.
602      */
603     public void setRequiresReports( boolean requiresReports )
604     {
605         this.requiresReports = requiresReports;
606     }
607 
608     /**
609      * @param executeGoal the new invocation goal of the Mojo
610      */
611     public void setExecuteGoal( String executeGoal )
612     {
613         this.executeGoal = executeGoal;
614     }
615 
616     /**
617      * @return the invocation goal of the Mojo
618      */
619     public String getExecuteGoal()
620     {
621         return executeGoal;
622     }
623 }