You are viewing a plain text version of this content. The canonical link for it is here.
Posted to site-commits@maven.apache.org by sv...@apache.org on 2025/01/22 14:05:02 UTC

svn commit: r1923318 - /maven/website/content/guides/plugin/guide-java-plugin-development.html

Author: svn-site-role
Date: Wed Jan 22 14:05:02 2025
New Revision: 1923318

Log:
Site checkin for project Apache Maven Site

Modified:
    maven/website/content/guides/plugin/guide-java-plugin-development.html

Modified: maven/website/content/guides/plugin/guide-java-plugin-development.html
==============================================================================
--- maven/website/content/guides/plugin/guide-java-plugin-development.html (original)
+++ maven/website/content/guides/plugin/guide-java-plugin-development.html Wed Jan 22 14:05:02 2025
@@ -170,10 +170,9 @@
 <p>Calling it <code>maven-&lt;yourplugin&gt;-plugin</code> (note &quot;Maven&quot; is at the beginning of the plugin name) is <b>strongly discouraged</b> since it's a <b>reserved naming pattern for official Apache Maven plugins maintained by the Apache Maven team</b> with groupId <code>org.apache.maven.plugins</code>.</p>
 <p>Using this naming pattern is an infringement of the Apache Maven Trademark.</p></section><section><a id="Your_First_Plugin"></a>
 <h2>Your First Plugin</h2>
-<p>In this section we will build a simple plugin with one goal which takes no parameters and simply displays a message on the screen when run. Along the way, we will cover the basics of setting up a project to create a plugin, the minimal contents of a Java mojo which will define goal code, and a couple ways to execute the mojo.</p><section><a id="Your_First_Mojo"></a>
+<p>In this section we will build a simple plugin with one goal that takes no parameters and displays a message on the screen when run. Along the way, we will cover setting up a project to create a plugin, the minimal contents of a Java mojo which will define goal code, and a couple ways to execute the mojo.</p><section><a id="Your_First_Mojo"></a>
 <h3>Your First Mojo</h3>
-<p>At its simplest, a Java mojo consists simply of a single class representing one plugin's goal. There is no requirement for multiple classes like EJBs, although a plugin which contains a number of similar mojos is likely to use an abstract superclass for the mojos to consolidate code common to all mojos.</p>
-<p>When processing the source tree to find mojos, <a href="/plugin-tools/"> <code>plugin-tools</code></a> looks for classes with <code>@Mojo</code> annotation. Any class with this annotation are included in the plugin configuration file.</p><section><a id="A_Simple_Mojo"></a>
+<p>At its simplest, a Java mojo consists simply of a single class representing one plugin's goal. When processing the source tree to find mojos, <a href="/plugin-tools/"> <code>plugin-tools</code></a> looks for classes with the <code>@Mojo</code> annotation. Any class with this annotation is included in the plugin configuration file.</p><section><a id="A_Simple_Mojo"></a>
 <h4>A Simple Mojo</h4>
 <p>Listed below is a simple mojo class which has no parameters. This is about as simple as a mojo can be. After the listing is a description of the various parts of the source.</p>
 <pre class="prettyprint linenums"><code>package sample.plugin;
@@ -189,6 +188,7 @@ import org.apache.maven.plugins.annotati
 @Mojo(name = &quot;sayhi&quot;)
 public class GreetingMojo extends AbstractMojo
 {
+    @Override
     public void execute() throws MojoExecutionException
     {
         getLog().info(&quot;Hello, world.&quot;);
@@ -196,28 +196,28 @@ public class GreetingMojo extends Abstra
 }</code></pre>
 <ul>
 <li>The class <code>org.apache.maven.plugin.AbstractMojo</code> provides most of the infrastructure required to implement a mojo except for the <code>execute</code> method.</li>
-<li>The annotation &quot;<code>@Mojo</code>&quot; is required and control how and when the mojo is executed.</li>
+<li>The annotation &quot;<code>@Mojo</code>&quot; is required and controls how and when the mojo is executed.</li>
 <li>The <code>execute</code> method can throw <code>org.apache.maven.plugin.MojoExecutionException</code> if a problem occurs. Throwing this exception causes a <code>BUILD FAILURE</code> message to be displayed.</li>
-<li>The <code>getLog</code> method (defined in <code>AbstractMojo</code>) returns a log4j-like logger object which allows plugins to create messages at levels of &quot;debug&quot;, &quot;info&quot;, &quot;warn&quot;, and &quot;error&quot;. This logger is the accepted means to display information to the user. Please have a look at the section <a href="../../plugin-developers/common-bugs.html#Retrieving_the_Mojo_Logger">Retrieving the Mojo Logger</a> for a hint on its proper usage.</li></ul>
+<li>The <code>getLog</code> method (defined in <code>AbstractMojo</code>) returns a log4j-like logger object which allows plugins to create messages at levels of &quot;debug&quot;, &quot;info&quot;, &quot;warn&quot;, and &quot;error&quot;. This logger is the accepted means to display information to the user. See <a href="../../plugin-developers/common-bugs.html#Retrieving_the_Mojo_Logger">Retrieving the Mojo Logger</a> for a hint on its proper usage.</li></ul>
 <p>All Mojo annotations are described by the <a href="../../developers/mojo-api-specification.html#The_Descriptor_and_Annotations">Mojo API Specification</a>.</p></section></section><section><a id="Project_Definition"></a>
 <h3>Project Definition</h3>
-<p>Once the mojos have been written for the plugin, it is time to build the plugin. To do this properly, the project's descriptor needs to have a number of settings set properly:</p>
+<p>Once the mojos have been written for the plugin, it is time to build the plugin. To do this properly, the project's pom.xml must set these things:</p>
 <table class="table table-bordered table-striped">
 <tr class="a">
 <td style="text-align: left;"><code>groupId</code></td>
-<td style="text-align: left;">This is the group ID for the plugin, and should match the common prefix to the packages used by the mojos</td></tr>
+<td style="text-align: left;">This is the group ID for the plugin, and should match the common prefix to the packages used by the mojos.</td></tr>
 <tr class="b">
 <td style="text-align: left;"><code>artifactId</code></td>
-<td style="text-align: left;">This is the name of the plugin</td></tr>
+<td style="text-align: left;">This is the name of the plugin.</td></tr>
 <tr class="a">
 <td style="text-align: left;"><code>version</code></td>
-<td style="text-align: left;">This is the version of the plugin</td></tr>
+<td style="text-align: left;">This is the version of the plugin.</td></tr>
 <tr class="b">
 <td style="text-align: left;"><code>packaging</code></td>
 <td style="text-align: left;">This must be set to &quot;<code>maven-plugin</code>&quot;</td></tr>
 <tr class="a">
 <td style="text-align: left;"><code>dependencies</code></td>
-<td style="text-align: left;">A dependency must be declared to the Maven Plugin Tools API to resolve &quot;<code>AbstractMojo</code>&quot; and related classes</td></tr></table>
+<td style="text-align: left;">A dependency must be declared on the Maven Plugin Tools API to resolve &quot;<code>AbstractMojo</code>&quot; and related classes</td></tr></table>
 <p>Listed below is an illustration of the sample mojo project's pom with the parameters set as described in the above table:</p>
 <pre class="prettyprint linenums"><code>&lt;project&gt;
   &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
@@ -237,11 +237,11 @@ public class GreetingMojo extends Abstra
     &lt;dependency&gt;
       &lt;groupId&gt;org.apache.maven&lt;/groupId&gt;
       &lt;artifactId&gt;maven-plugin-api&lt;/artifactId&gt;
-      &lt;version&gt;3.0&lt;/version&gt;
+      &lt;version&gt;3.9.9&lt;/version&gt;
       &lt;scope&gt;provided&lt;/scope&gt;
     &lt;/dependency&gt;
 
-    &lt;!-- dependencies to annotations --&gt;
+    &lt;!-- dependency on annotations --&gt;
     &lt;dependency&gt;
       &lt;groupId&gt;org.apache.maven.plugin-tools&lt;/groupId&gt;
       &lt;artifactId&gt;maven-plugin-annotations&lt;/artifactId&gt;
@@ -294,7 +294,7 @@ public class GreetingMojo extends Abstra
 <td style="text-align: left;">Deploys the plugin jar to the remote repository</td></tr></table>
 <p>For more details, you can look at <a href="/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_maven-plugin_packaging"> detailed bindings for <code>maven-plugin</code> packaging</a>.</p></section><section><a id="Executing_Your_First_Mojo"></a>
 <h3>Executing Your First Mojo</h3>
-<p>The most direct means of executing your new plugin is to specify the plugin goal directly on the command line. To do this, you need to configure the <code>hello-maven-plugin</code> plugin in you project:</p>
+<p>The most direct means of executing your new plugin is to specify the plugin goal directly on the command line. To do this, configure the <code>hello-maven-plugin</code> plugin in the project:</p>
 <pre class="prettyprint linenums"><code>&lt;project&gt;
 ...
   &lt;build&gt;
@@ -310,22 +310,22 @@ public class GreetingMojo extends Abstra
   &lt;/build&gt;
 ...
 &lt;/project&gt;</code></pre>
-<p>And, you need to specify a fully-qualified goal in the form of:</p>
+<p>Then on the command line specify a fully-qualified goal in the form of:</p>
 <pre>mvn groupId:artifactId:version:goal</pre>
-<p>For example, to run the simple mojo in the sample plugin, you would enter &quot;<code>mvn sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi</code>&quot; on the command line.</p>
+<p>For example, to run the simple mojo in the sample plugin, enter &quot;<code>mvn sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi</code>&quot; on the command line.</p>
 <p><b>Tips</b>: <code>version</code> is not required to run a standalone goal.</p><section><a id="Shortening_the_Command_Line"></a>
 <h4>Shortening the Command Line</h4>
 <p>There are several ways to reduce the amount of required typing:</p>
 <ul>
-<li>If you need to run the latest version of a plugin installed in your local repository, you can omit its version number. So just use &quot;<code>mvn sample.plugin:hello-maven-plugin:sayhi</code>&quot; to run your plugin.</li>
-<li>You can assign a shortened prefix to your plugin, such as <code>mvn hello:sayhi</code>. This is done automatically if you follow the convention of using <code>${prefix}-maven-plugin</code> (or <code>maven-${prefix}-plugin</code> if the plugin is part of the Apache Maven project). You may also assign one through additional configuration - for more information see <a href="../introduction/introduction-to-plugin-prefix-mapping.html"> Introduction to Plugin Prefix Mapping</a>.</li>
-<li>Finally, you can also add your plugin's groupId to the list of groupIds searched by default. To do this, you need to add the following to your <code>${user.home}/.m2/settings.xml</code> file:
+<li>To run the latest version of a plugin installed in the local repository, you can omit its version number. So just use &quot;<code>mvn sample.plugin:hello-maven-plugin:sayhi</code>&quot; to run your plugin.</li>
+<li>You can assign a shortened prefix to your plugin, such as <code>mvn hello:sayhi</code>. This is done automatically if you follow the convention of using <code>${prefix}-maven-plugin</code> (or <code>maven-${prefix}-plugin</code> if the plugin is part of the Apache Maven project). You may also assign one through additional configuration. For more information, see <a href="../introduction/introduction-to-plugin-prefix-mapping.html"> Introduction to Plugin Prefix Mapping</a>.</li>
+<li>Finally, you can also add your plugin's group ID to the list of group IDs searched by default. To do this, add the following to your <code>${user.home}/.m2/settings.xml</code> file:
 <pre class="prettyprint linenums"><code>&lt;pluginGroups&gt;
   &lt;pluginGroup&gt;sample.plugin&lt;/pluginGroup&gt;
 &lt;/pluginGroups&gt;</code></pre></li></ul>
 <p>At this point, you can run the mojo with &quot;<code>mvn hello:sayhi</code>&quot;.</p></section><section><a id="Attaching_the_Mojo_to_the_Build_Lifecycle"></a>
 <h4>Attaching the Mojo to the Build Lifecycle</h4>
-<p>You can also configure your plugin to attach specific goals to a particular phase of the build lifecycle. Here is an example:</p>
+<p>You can also configure the plugin to attach specific goals to a particular phase of the build lifecycle. Here is an example:</p>
 <pre class="prettyprint linenums"><code>  &lt;build&gt;
     &lt;pluginManagement&gt;
       &lt;plugins&gt;
@@ -351,9 +351,9 @@ public class GreetingMojo extends Abstra
       &lt;/plugin&gt;
     &lt;/plugins&gt;
   &lt;/build&gt;</code></pre>
-<p>This causes the simple mojo to be executed whenever Java code is compiled. For more information on binding a mojo to phases in the lifecycle, please refer to the <a href="../introduction/introduction-to-the-lifecycle.html">Build Lifecycle</a> document.</p></section></section></section><section><a id="Mojo_archetype"></a>
+<p>This causes the simple mojo to be executed whenever Java code is compiled. For more information on binding a mojo to phases in the lifecycle, see the <a href="../introduction/introduction-to-the-lifecycle.html">Build Lifecycle</a> document.</p></section></section></section><section><a id="Mojo_archetype"></a>
 <h2>Mojo archetype</h2>
-<p>To create a new plugin project, you could using the Mojo <a href="../introduction/introduction-to-archetypes.html">archetype</a> with the following command line:</p>
+<p>To create a new plugin project, you can use the Mojo <a href="../introduction/introduction-to-archetypes.html">archetype</a> with the following command line:</p>
 <pre>mvn archetype:generate \
   -DgroupId=sample.plugin \
   -DartifactId=hello-maven-plugin \
@@ -362,8 +362,8 @@ public class GreetingMojo extends Abstra
 <h2>Parameters</h2>
 <p>It is unlikely that a mojo will be very useful without parameters. Parameters provide a few very important functions:</p>
 <ul>
-<li>It provides hooks to allow the user to adjust the operation of the plugin to suit their needs.</li>
-<li>It provides a means to easily extract the value of elements from the POM without the need to navigate the objects.</li></ul><section><a id="Defining_Parameters_Within_a_Mojo"></a>
+<li>They provide hooks to allow the user to adjust the operation of the plugin to suit their needs.</li>
+<li>They provide a means to easily extract the value of elements from the POM without the need to navigate the objects.</li></ul><section><a id="Defining_Parameters_Within_a_Mojo"></a>
 <h3>Defining Parameters Within a Mojo</h3>
 <p>Defining a parameter is as simple as creating an instance variable in the mojo and adding the proper annotations. Listed below is an example of a parameter for the simple mojo:</p>
 <pre class="prettyprint linenums"><code>    /**
@@ -371,9 +371,9 @@ public class GreetingMojo extends Abstra
      */
     @Parameter(property = &quot;sayhi.greeting&quot;, defaultValue = &quot;Hello World!&quot; )
     private String greeting;</code></pre>
-<p>The portion before the annotations is the description of the parameter. The <code>@Parameter</code> annotation identifies the variable as a mojo parameter. The <code>defaultValue</code> parameter of the annotation defines the default value for the variable. This value can include expressions which reference the project, such as &quot;<code>${project.version}</code>&quot; (more can be found in the <a href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">&quot;Parameter Expressions&quot; document</a>). The <code>property</code> parameter can be used to allow configuration of the mojo parameter from the command line by referencing a system property that the user sets via the <code>-D</code> option.</p></section><section><a id="Configuring_Parameters_in_a_Project"></a>
+<p>The Javadoc comment is the description of the parameter. The <code>@Parameter</code> annotation identifies the variable as a mojo parameter. The <code>defaultValue</code> element defines the default value for the parameter. This value can include expressions which reference the project, such as &quot;<code>${project.version}</code>&quot;. (More can be found in the <a href="/ref/current/maven-core/apidocs/org/apache/maven/plugin/PluginParameterExpressionEvaluator.html">&quot;Parameter Expressions&quot; document.</a>) The <code>property</code> element enables configuration of the mojo parameter from the command line by referencing a system property that the user sets via the <code>-D</code> option.</p></section><section><a id="Configuring_Parameters_in_a_Project"></a>
 <h3>Configuring Parameters in a Project</h3>
-<p>Configuring the parameter values for a plugin is done in a Maven project within the <code>pom.xml</code> file as part of defining the plugin in the project. An example of configuring a plugin:</p>
+<p>Each Maven project can configrue the plugins it uses in the <code>pluginManagement</code> section of the pom.xml. For example:</p>
 <pre class="prettyprint linenums"><code>&lt;plugin&gt;
   &lt;groupId&gt;sample.plugin&lt;/groupId&gt;
   &lt;artifactId&gt;hello-maven-plugin&lt;/artifactId&gt;
@@ -382,17 +382,17 @@ public class GreetingMojo extends Abstra
     &lt;greeting&gt;Welcome&lt;/greeting&gt;
   &lt;/configuration&gt;
 &lt;/plugin&gt;</code></pre>
-<p>In the configuration section, the element name (&quot;<code>greeting</code>&quot;) is the parameter name and the contents of the element (&quot;<code>Welcome</code>&quot;) is the value to be assigned to the parameter.</p>
+<p>In the configuration section, the element name (&quot;<code>greeting</code>&quot;) is the parameter name and the content of the element (&quot;<code>Welcome</code>&quot;) is the value to be assigned to the parameter.</p>
 <p><b>Note</b>: More details can be found in the <a href="../mini/guide-configuring-plugins.html">Guide to Configuring Plugins</a>.</p></section></section><section><a id="Using_Setters"></a>
 <h2>Using Setters</h2>
-<p>You are not restricted to using private field mapping which is good if you are trying to make your Mojos resuable outside the context of Maven. Using the example above, we could define public setter methods that the configuration mapping mechanism can use. You can also add an <code>@Parameter</code> annotation on the setter method (from version 3.7.0 of `plugin-tools`)</p>
+<p>You are not restricted to using private field mapping which is helpful if you are trying to make your Mojos reusable outside the context of Maven. In the example above, we could define public setter methods that the configuration mapping mechanism can use. You can also add an <code>@Parameter</code> annotation on the setter method (from version 3.7.0 of `plugin-tools`)</p>
 <p>So the Mojo would look like the following:</p>
 <pre class="prettyprint linenums"><code>
 public class MyQueryMojo extends AbstractMojo {
 
     // provide name for non matching field and setter name
     @Parameter(name = &quot;url&quot;, property = &quot;url&quot;)
-    private String _url;
+    private String url;
 
     @Parameter(property = &quot;timeout&quot;)
     private int timeout;
@@ -401,7 +401,7 @@ public class MyQueryMojo extends Abstrac
     private String option1;
 
     public void setUrl(String url) {
-        _url = url;
+        this.url = url;
     }
 
     public void setTimeout(int timeout) {
@@ -415,20 +415,19 @@ public class MyQueryMojo extends Abstrac
         this.option1 = options[1];
     }
 
+    @Override
     public void execute() throws MojoExecutionException {
         ...
     }
 }
 </code></pre>
-<p>Note the specification of the property name for each parameter which tells Maven which setter and getter to use when the field's name does not match the name of the parameter in the plugin configuration.</p></section><section><a id="Resources"></a>
+<p>The property annotation for each parameter tells Maven which setter and getter to use when the field's name does not match the name of the parameter in the plugin configuration.</p></section><section><a id="Resources"></a>
 <h2>Resources</h2>
 <ol style="list-style-type: decimal;">
 <li><a href="../../developers/mojo-api-specification.html">Mojo Documentation</a>: Mojo API, Mojo annotations</li>
 <li><a href="/shared/maven-plugin-testing-harness/">Maven Plugin Testing Harness</a>: Testing framework for your Mojos.</li>
 <li><a class="externalLink" href="https://eclipse.dev/sisu/index.html">Eclipse Sisu</a>: The IoC container used by Maven 3.</li>
 <li><a href="../../maven-jsr330.html">Maven &amp; JSR 330</a>: How to use JSR 330 in Plugins</li>
-<li><a class="externalLink" href="https://codehaus-plexus.github.io/plexus-utils/">Plexus Common Utilities</a>: Set of utilities classes useful for Mojo development.</li>
-<li><a class="externalLink" href="http://commons.apache.org/io/">Commons IO</a>: Set of utilities classes useful for file/path handling.</li>
 <li><a href="../../plugin-developers/common-bugs.html">Common Bugs and Pitfalls</a>: Overview of problematic coding patterns.</li></ol></section></section>        </main>
       </div>
     </div>