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 * @Mojo( name = "<goal-name>" ) 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" summary="mojo annotation attributes"> 39 * <tr bgcolor="#CCCCCC"> 40 * <th>Descriptor Element</th> 41 * <th>Annotation</th> 42 * <th>Required?</th> 43 * <th>Notes</th> 44 * </tr> 45 * <tr> 46 * <td>goal</td> 47 * <td>name = "<goal-name>"</td> 48 * <td>Yes</td> 49 * <td>The name for the Mojo that users will reference from the command line to execute the Mojo directly, 50 * or inside a POM in order to provide Mojo-specific configuration.</td> 51 * </tr> 52 * <tr> 53 * <td>implementation</td> 54 * <td>none (detected)</td> 55 * <td>Yes</td> 56 * <td>The Mojo's fully-qualified class name (or script path in the case of non-Java Mojos).</td> 57 * </tr> 58 * <tr> 59 * <td>language</td> 60 * <td>none (detected)</td> 61 * <td>No. Default: <code>java</code></td> 62 * <td>The implementation language for this Mojo (Java, beanshell, etc.).</td> 63 * </tr> 64 * <tr> 65 * <td>configurator</td> 66 * <td>configurator = "<role-hint>"</td> 67 * <td>No</td> 68 * <td>The configurator type to use when injecting parameter values into this Mojo. The value is normally 69 * deduced from the Mojo's implementation language, but can be specified to allow a custom 70 * ComponentConfigurator implementation to be used. 71 * <br> 72 * <i>NOTE: This will only be used in very special cases, using a highly controlled vocabulary of possible 73 * values. (Elements like this are why it's a good idea to use the descriptor tools.)</i> 74 * </td> 75 * </tr> 76 * <tr> 77 * <td>phase</td> 78 * <td>defaultPhase = LifecyclePhase.<phase></td> 79 * <td>No</td> 80 * <td>Binds this Mojo to a particular phase of the standard build lifecycle, if specified. 81 * <br> 82 * <i>NOTE: This is only required if this Mojo is to participate in the standard build process.</i> 83 * </td> 84 * </tr> 85 * <tr> 86 * <td>execute</td> 87 * <td>@Execute 88 * ( phase=LifecyclePhase.<phase>, goal= "<goal-name>", lifecycle="<lifecycle-id>" )</td> 89 * <td>No</td> 90 * <td>When this goal is invoked, it will first invoke a parallel lifecycle, ending at the given phase. 91 * If a goal is provided instead of a phase, that goal will be executed in isolation. 92 * The execution of either will not affect the current project, but instead make available the 93 * <code>${executedProject}</code> expression if required. An alternate lifecycle can also be provided: 94 * for more information see the documentation on the 95 * <a href="https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html" 96 * target="_blank">build lifecycle</a>. 97 * </td> 98 * </tr> 99 * <tr> 100 * <td>requiresDependencyResolution</td> 101 * <td>requiresDependencyResolution = ResolutionScope.<scope></td> 102 * <td>No</td> 103 * <td>Flags this Mojo as requiring the dependencies in the specified scope (or an implied scope) to be 104 * resolved before it can execute. 105 * <br> 106 * <i>NOTE: Currently supports <b>compile</b>, <b>runtime</b>, and <b>test</b> scopes.</i> 107 * </td> 108 * </tr> 109 * <tr> 110 * <td>description</td> 111 * <td>none (detected)</td> 112 * <td>No</td> 113 * <td>The description of this Mojo's functionality. Using the toolset, this will be the class-level 114 * Javadoc description provided.<br> 115 * <i>NOTE: While this is not a required part of the Mojo specification, it <b>SHOULD</b> be provided to 116 * enable future tool support for browsing, etc. and for clarity.</i> 117 * </td> 118 * </tr> 119 * <tr> 120 * <td>parameters</td> 121 * <td>N/A</td> 122 * <td>No</td> 123 * <td>Specifications for the parameters which this Mojo uses will be provided in <b>parameter</b> sub-elements 124 * in this section. 125 * <br> 126 * <i>NOTE: Parameters are discussed in more detail below.</i> 127 * </td> 128 * </tr> 129 * </table> 130 * <p>This is only a small set of all the options. A complete list can be found at 131 * <a href="https://maven.apache.org/components/plugin-tools/maven-plugin-tools-annotations/index.html" target="_blank"> 132 * Maven Plugin Tool for Annotations</a>. 133 * 134 * @see <a href="https://maven.apache.org/guides/plugin/guide-java-plugin-development.html" target="_blank">Guide to Developing Java Plugins</a> 135 * @see <a href="https://maven.apache.org/guides/mini/guide-configuring-plugins.html" target="_blank">Guide to Configuring Plug-ins</a> 136 * @see <a href="https://maven.apache.org/developers/mojo-api-specification.html" target="_blank">Mojo API Specification</a> 137 * 138 * @author <a href="mailto:brett@apache.org">Brett Porter</a> 139 * @author jdcasey 140 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 141 */ 142 public abstract class AbstractMojo 143 implements Mojo, ContextEnabled 144 { 145 /** Instance logger */ 146 private Log log; 147 148 /** Plugin container context */ 149 private Map pluginContext; 150 151 @Override 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 @Override 171 public Log getLog() 172 { 173 if ( log == null ) 174 { 175 log = new SystemStreamLog(); 176 } 177 178 return log; 179 } 180 181 @Override 182 public Map getPluginContext() 183 { 184 return pluginContext; 185 } 186 187 @Override 188 public void setPluginContext( Map pluginContext ) 189 { 190 this.pluginContext = pluginContext; 191 } 192 }