View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin;
20  
21  import java.util.Map;
22  import org.apache.maven.plugin.logging.Log;
23  import org.apache.maven.plugin.logging.SystemStreamLog;
24  
25  /**
26   * Abstract class to provide most of the infrastructure required to implement a <code>Mojo</code> except for
27   * the execute method.<br>
28   * The implementation should have a <code>Mojo</code> annotation with the name of the goal:
29   * <pre>
30   *   &#64;Mojo( name = "&lt;goal-name&gt;" )
31   * </pre>
32   * <p>
33   * There are also a number of attributes which can be used to control how and when the
34   * <code>Mojo</code> is executed:
35   * </p>
36   * <table border="1">
37   *  <caption>mojo annotation attributes</caption>
38   *  <tr>
39   *      <th>Descriptor Element</th>
40   *      <th>Annotation</th>
41   *      <th>Required?</th>
42   *      <th>Notes</th>
43   *  </tr>
44   *  <tr>
45   *      <td>goal</td>
46   *      <td>name = "&lt;goal-name&gt;"</td>
47   *      <td>Yes</td>
48   *      <td>The name for the Mojo that users will reference from the command line to execute the Mojo directly,
49   *      or inside a POM in order to provide Mojo-specific configuration.</td>
50   *  </tr>
51   *  <tr>
52   *      <td>implementation</td>
53   *      <td>none (detected)</td>
54   *      <td>Yes</td>
55   *      <td>The Mojo's fully-qualified class name (or script path in the case of non-Java Mojos).</td>
56   *  </tr>
57   *  <tr>
58   *      <td>language</td>
59   *      <td>none (detected)</td>
60   *      <td>No. Default: <code>java</code></td>
61   *      <td>The implementation language for this Mojo (Java, beanshell, etc.).</td>
62   *  </tr>
63   *  <tr>
64   *      <td>configurator</td>
65   *      <td>configurator = "&lt;role-hint&gt;"</td>
66   *      <td>No</td>
67   *      <td>The configurator type to use when injecting parameter values into this Mojo. The value is normally
68   *          deduced from the Mojo's implementation language, but can be specified to allow a custom
69   *          ComponentConfigurator implementation to be used.
70   *          <br>
71   *          <i>NOTE: This will only be used in very special cases, using a highly controlled vocabulary of possible
72   *          values. (Elements like this are why it's a good idea to use the descriptor tools.)</i>
73   *      </td>
74   *   </tr>
75   *   <tr>
76   *      <td>phase</td>
77   *      <td>defaultPhase = LifecyclePhase.&lt;phase&gt;</td>
78   *      <td>No</td>
79   *      <td>Binds this Mojo to a particular phase of the standard build lifecycle, if specified.
80   *          <br>
81   *          <i>NOTE: This is only required if this Mojo is to participate in the standard build process.</i>
82   *      </td>
83   *   </tr>
84   *   <tr>
85   *      <td>execute</td>
86   *      <td>@Execute
87   *       ( phase=LifecyclePhase.&lt;phase&gt;, goal= "&lt;goal-name&gt;", lifecycle="&lt;lifecycle-id&gt;" )</td>
88   *      <td>No</td>
89   *      <td>When this goal is invoked, it will first invoke a parallel lifecycle, ending at the given phase.
90   *          If a goal is provided instead of a phase, that goal will be executed in isolation.
91   *          The execution of either will not affect the current project, but instead make available the
92   *          <code>${executedProject}</code> expression if required. An alternate lifecycle can also be provided:
93   *          for more information see the documentation on the
94   *          <a href="https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html"
95   *             target="_blank">build lifecycle</a>.
96   *      </td>
97   *   </tr>
98   *   <tr>
99   *      <td>requiresDependencyResolution</td>
100  *      <td>requiresDependencyResolution = ResolutionScope.&lt;scope&gt;</td>
101  *      <td>No</td>
102  *      <td>Flags this Mojo as requiring the dependencies in the specified scope (or an implied scope) to be
103  *          resolved before it can execute.
104  *          <br>
105  *          <i>NOTE: Currently supports <b>compile</b>, <b>runtime</b>, and <b>test</b> scopes.</i>
106  *      </td>
107  *   </tr>
108  *   <tr>
109  *      <td>description</td>
110  *      <td>none (detected)</td>
111  *      <td>No</td>
112  *      <td>The description of this Mojo's functionality. Using the toolset, this will be the class-level
113  *          Javadoc description provided.<br>
114  *          <i>NOTE: While this is not a required part of the Mojo specification, it <b>SHOULD</b> be provided to
115  *          enable future tool support for browsing, etc. and for clarity.</i>
116  *      </td>
117  *   </tr>
118  *   <tr>
119  *      <td>parameters</td>
120  *      <td>N/A</td>
121  *      <td>No</td>
122  *      <td>Specifications for the parameters which this Mojo uses will be provided in <b>parameter</b> sub-elements
123  *          in this section.
124  *          <br>
125  *          <i>NOTE: Parameters are discussed in more detail below.</i>
126  *      </td>
127  *   </tr>
128  * </table>
129  * <p>This is only a small set of all the options. A complete list can be found at
130  * <a href="https://maven.apache.org/components/plugin-tools/maven-plugin-tools-annotations/index.html" target="_blank">
131  * Maven Plugin Tool for Annotations</a>.
132  *
133  * @see <a href="https://maven.apache.org/guides/plugin/guide-java-plugin-development.html" target="_blank">Guide to Developing Java Plugins</a>
134  * @see <a href="https://maven.apache.org/guides/mini/guide-configuring-plugins.html" target="_blank">Guide to Configuring Plug-ins</a>
135  * @see <a href="https://maven.apache.org/developers/mojo-api-specification.html" target="_blank">Mojo API Specification</a>
136  *
137  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
138  * @author jdcasey
139  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
140  */
141 public abstract class AbstractMojo implements Mojo, ContextEnabled {
142     /** Instance logger */
143     private Log log;
144 
145     /** Plugin container context */
146     private Map pluginContext;
147 
148     /**
149      * @deprecated Use SLF4J directly
150      */
151     @Deprecated
152     @Override
153     public void setLog(Log log) {
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      * @deprecated Use SLF4J directly
170      */
171     @Deprecated
172     @Override
173     public Log getLog() {
174         if (log == null) {
175             log = new SystemStreamLog();
176         }
177 
178         return log;
179     }
180 
181     @Override
182     public Map getPluginContext() {
183         return pluginContext;
184     }
185 
186     @Override
187     public void setPluginContext(Map pluginContext) {
188         this.pluginContext = pluginContext;
189     }
190 }