View Javadoc
1   package org.apache.maven.plugin;
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 java.util.Map;
23  
24  import org.apache.maven.plugin.logging.Log;
25  import org.apache.maven.plugin.logging.SystemStreamLog;
26  
27  /**
28   * Abstract class to provide most of the infrastructure required to implement a <code>Mojo</code> except for
29   * the execute method.<br>
30   * The implementation should have a <code>goal</code> annotation in the class-level javadoc annotation:
31   * <pre>
32   * &#47;&#42;&#42;
33   *  &#42; &#64;goal goalName
34   *  &#42;&#47;
35   * </pre>
36   * <p>
37   * There are also a number of class-level javadoc annotations which can be used to control how and when the
38   * <code>Mojo</code> is executed:
39   * </p>
40   * <table border="1" summary="class-level doclettags">
41   *  <tr bgcolor="#CCCCCC">
42   *      <th>Descriptor Element</th>
43   *      <th>Annotation</th>
44   *      <th>Required?</th>
45   *      <th>Notes</th>
46   *  </tr>
47   *  <tr>
48   *      <td>goal</td>
49   *      <td>@goal &lt;goalName&gt;</td>
50   *      <td>Yes</td>
51   *      <td>The name for the Mojo that users will reference from the command line to execute the Mojo directly,
52   *      or inside a POM in order to provide Mojo-specific configuration.</td>
53   *  </tr>
54   *  <tr>
55   *      <td>implementation</td>
56   *      <td>none (detected)</td>
57   *      <td>Yes</td>
58   *      <td>The Mojo's fully-qualified class name (or script path in the case of non-Java Mojos).</td>
59   *  </tr>
60   *  <tr>
61   *      <td>language</td>
62   *      <td>none (detected)</td>
63   *      <td>No. Default: <code>java</code></td>
64   *      <td>The implementation language for this Mojo (Java, beanshell, etc.).</td>
65   *  </tr>
66   *  <tr>
67   *      <td>configurator</td>
68   *      <td>@configurator &lt;roleHint&gt;</td>
69   *      <td>No</td>
70   *      <td>The configurator type to use when injecting parameter values into this Mojo. The value is normally
71   *          deduced from the Mojo's implementation language, but can be specified to allow a custom
72   *          ComponentConfigurator implementation to be used.
73   *          <br>
74   *          <i>NOTE: This will only be used in very special cases, using a highly controlled vocabulary of possible
75   *          values. (Elements like this are why it's a good idea to use the descriptor tools.)</i>
76   *      </td>
77   *   </tr>
78   *   <tr>
79   *      <td>phase</td>
80   *      <td>@phase &lt;phaseName&gt;</td>
81   *      <td>No</td>
82   *      <td>Binds this Mojo to a particular phase of the standard build lifecycle, if specified.
83   *          <br>
84   *          <i>NOTE: This is only required if this Mojo is to participate in the standard build process.</i>
85   *      </td>
86   *   </tr>
87   *   <tr>
88   *      <td>execute</td>
89   *      <td>@execute [phase=&lt;phaseName&gt;|goal=&lt;goalName&gt;] [lifecycle=&lt;lifecycleId&gt;]</td>
90   *      <td>No</td>
91   *      <td>When this goal is invoked, it will first invoke a parallel lifecycle, ending at the given phase.
92   *          If a goal is provided instead of a phase, that goal will be executed in isolation.
93   *          The execution of either will not affect the current project, but instead make available the
94   *          <code>${executedProject}</code> expression if required. An alternate lifecycle can also be provided:
95   *          for more information see the documentation on the
96   *          <a href="https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html"
97   *             target="_blank">build lifecycle</a>.
98   *      </td>
99   *   </tr>
100  *   <tr>
101  *      <td>requiresDependencyResolution</td>
102  *      <td>@requiresDependencyResolution &lt;requiredScope&gt;</td>
103  *      <td>No</td>
104  *      <td>Flags this Mojo as requiring the dependencies in the specified scope (or an implied scope) to be
105  *          resolved before it can execute.
106  *          <br>
107  *          <i>NOTE: Currently supports <b>compile</b>, <b>runtime</b>, and <b>test</b> scopes.</i>
108  *      </td>
109  *   </tr>
110  *   <tr>
111  *      <td>description</td>
112  *      <td>none (detected)</td>
113  *      <td>No</td>
114  *      <td>The description of this Mojo's functionality. Using the toolset, this will be the class-level
115  *          Javadoc description provided.<br>
116  *          <i>NOTE: While this is not a required part of the Mojo specification, it <b>SHOULD</b> be provided to
117  *          enable future tool support for browsing, etc. and for clarity.</i>
118  *      </td>
119  *   </tr>
120  *   <tr>
121  *      <td>parameters</td>
122  *      <td>N/A</td>
123  *      <td>No</td>
124  *      <td>Specifications for the parameters which this Mojo uses will be provided in <b>parameter</b> sub-elements
125  *          in this section.
126  *          <br>
127  *          <i>NOTE: Parameters are discussed in more detail below.</i>
128  *      </td>
129  *   </tr>
130  * </table>
131  *
132  * @see <a href="https://maven.apache.org/guides/plugin/guide-java-plugin-development.html" target="_blank">Guide to Developing Java Plugins</a>
133  * @see <a href="https://maven.apache.org/guides/mini/guide-configuring-plugins.html" target="_blank">Guide to Configuring Plug-ins</a>
134  * @see <a href="https://maven.apache.org/developers/mojo-api-specification.html" target="_blank">Mojo API Specification</a>
135  *
136  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
137  * @author jdcasey
138  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
139  */
140 public abstract class AbstractMojo
141     implements Mojo, ContextEnabled
142 {
143     /** Instance logger */
144     private Log log;
145 
146     /** Plugin container context */
147     private Map pluginContext;
148 
149     /**
150      * @see org.apache.maven.plugin.Mojo#setLog(org.apache.maven.plugin.logging.Log)
151      */
152     public void setLog( Log log )
153     {
154         this.log = log;
155     }
156 
157     /**
158      * <p>
159      * Returns the logger that has been injected into this mojo. If no logger has been setup yet, a
160      * <code>SystemStreamLog</code> logger will be created and returned.
161      * </p>
162      * <strong>Note:</strong>
163      * The logger returned by this method must not be cached in an instance field during the construction of the mojo.
164      * This would cause the mojo to use a wrongly configured default logger when being run by Maven. The proper logger
165      * gets injected by the Plexus container <em>after</em> the mojo has been constructed. Therefore, simply call this
166      * method directly whenever you need the logger, it is fast enough and needs no caching.
167      *
168      * @see org.apache.maven.plugin.Mojo#getLog()
169      */
170     public Log getLog()
171     {
172         if ( log == null )
173         {
174             log = new SystemStreamLog();
175         }
176 
177         return log;
178     }
179 
180     /**
181      * @see org.apache.maven.plugin.ContextEnabled#getPluginContext()
182      */
183     public Map getPluginContext()
184     {
185         return pluginContext;
186     }
187 
188     /**
189      * @see org.apache.maven.plugin.ContextEnabled#setPluginContext(java.util.Map)
190      */
191     public void setPluginContext( Map pluginContext )
192     {
193         this.pluginContext = pluginContext;
194     }
195 }