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>Mojo</code> annotation with the name of the goal:
31   * <pre>
32   *   &#64;Mojo( name = "&lt;goal-name&gt;" )
33   * </pre>
34   * <p>
35   * There are also a number of attributes which can be used to control how and when the
36   * <code>Mojo</code> is executed:
37   * </p>
38   * <table border="1">
39   *  <caption>mojo annotation attributes</caption>
40   *  <tr>
41   *      <th>Descriptor Element</th>
42   *      <th>Annotation</th>
43   *      <th>Required?</th>
44   *      <th>Notes</th>
45   *  </tr>
46   *  <tr>
47   *      <td>goal</td>
48   *      <td>name = "&lt;goal-name&gt;"</td>
49   *      <td>Yes</td>
50   *      <td>The name for the Mojo that users will reference from the command line to execute the Mojo directly,
51   *      or inside a POM in order to provide Mojo-specific configuration.</td>
52   *  </tr>
53   *  <tr>
54   *      <td>implementation</td>
55   *      <td>none (detected)</td>
56   *      <td>Yes</td>
57   *      <td>The Mojo's fully-qualified class name (or script path in the case of non-Java Mojos).</td>
58   *  </tr>
59   *  <tr>
60   *      <td>language</td>
61   *      <td>none (detected)</td>
62   *      <td>No. Default: <code>java</code></td>
63   *      <td>The implementation language for this Mojo (Java, beanshell, etc.).</td>
64   *  </tr>
65   *  <tr>
66   *      <td>configurator</td>
67   *      <td>configurator = "&lt;role-hint&gt;"</td>
68   *      <td>No</td>
69   *      <td>The configurator type to use when injecting parameter values into this Mojo. The value is normally
70   *          deduced from the Mojo's implementation language, but can be specified to allow a custom
71   *          ComponentConfigurator implementation to be used.
72   *          <br>
73   *          <i>NOTE: This will only be used in very special cases, using a highly controlled vocabulary of possible
74   *          values. (Elements like this are why it's a good idea to use the descriptor tools.)</i>
75   *      </td>
76   *   </tr>
77   *   <tr>
78   *      <td>phase</td>
79   *      <td>defaultPhase = LifecyclePhase.&lt;phase&gt;</td>
80   *      <td>No</td>
81   *      <td>Binds this Mojo to a particular phase of the standard build lifecycle, if specified.
82   *          <br>
83   *          <i>NOTE: This is only required if this Mojo is to participate in the standard build process.</i>
84   *      </td>
85   *   </tr>
86   *   <tr>
87   *      <td>execute</td>
88   *      <td>@Execute
89   *       ( phase=LifecyclePhase.&lt;phase&gt;, goal= "&lt;goal-name&gt;", lifecycle="&lt;lifecycle-id&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 = ResolutionScope.&lt;scope&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  * <p>This is only a small set of all the options. A complete list can be found at 
132  * <a href="https://maven.apache.org/components/plugin-tools/maven-plugin-tools-annotations/index.html" target="_blank">
133  * Maven Plugin Tool for Annotations</a>. 
134  *
135  * @see <a href="https://maven.apache.org/guides/plugin/guide-java-plugin-development.html" target="_blank">Guide to Developing Java Plugins</a>
136  * @see <a href="https://maven.apache.org/guides/mini/guide-configuring-plugins.html" target="_blank">Guide to Configuring Plug-ins</a>
137  * @see <a href="https://maven.apache.org/developers/mojo-api-specification.html" target="_blank">Mojo API Specification</a>
138  *
139  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
140  * @author jdcasey
141  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
142  */
143 public abstract class AbstractMojo
144     implements Mojo, ContextEnabled
145 {
146     /** Instance logger */
147     private Log log;
148 
149     /** Plugin container context */
150     private Map pluginContext;
151 
152     @Override
153     public void setLog( Log log )
154     {
155         this.log = log;
156     }
157 
158     /**
159      * <p>
160      * Returns the logger that has been injected into this mojo. If no logger has been setup yet, a
161      * <code>SystemStreamLog</code> logger will be created and returned.
162      * </p>
163      * <strong>Note:</strong>
164      * The logger returned by this method must not be cached in an instance field during the construction of the mojo.
165      * This would cause the mojo to use a wrongly configured default logger when being run by Maven. The proper logger
166      * gets injected by the Plexus container <em>after</em> the mojo has been constructed. Therefore, simply call this
167      * method directly whenever you need the logger, it is fast enough and needs no caching.
168      *
169      * @see org.apache.maven.plugin.Mojo#getLog()
170      */
171     @Override
172     public Log getLog()
173     {
174         if ( log == null )
175         {
176             log = new SystemStreamLog();
177         }
178 
179         return log;
180     }
181 
182     @Override
183     public Map getPluginContext()
184     {
185         return pluginContext;
186     }
187 
188     @Override
189     public void setPluginContext( Map pluginContext )
190     {
191         this.pluginContext = pluginContext;
192     }
193 }