Mojo Parameters

Just as important as the execute() method and the Mojo annotations is the fact that a Mojo is configured via parameters. This section deals with some configuration and topics surrounding Mojo parameters.

Supplying Values for Mojo Parameters

In EchoMojo, we declare the message parameter with the following annotations:

/**
 * Any Object to print out.
 * @parameter
 *       expression="${echo.message}"
 *       default-value="Hello Maven World"
 */
private Object message;

The default expression for this parameter is ${echo.message}. This means that Maven will try to use the value of the echo.message property to set the value for message. If the value of the echo.message property is null, the default-value attribute of the @parameter annotation will be used instead. Instead of using the echo.message property, we can configure a value for the message parameter of the EchoMojo directly in a project’s POM.

We can populate the message parameter in the EchoMojo in a few ways. First, we can pass in a value from the command line like this (assuming that you’ve added org.sonatype.mavenbook.plugins to your pluginGroups):

$ mvn first:echo -Decho.message="Hello Everybody"

We can also specify the value of this message parameter by setting a property in our POM or in our settings.xml:

<project>
  ...
  <properties>
    <echo.message>Hello Everybody</echo.message>
  </properties>
</project>

This parameter can also be configured directly as a configuration value for the plugin. If we wanted to customize the message ...

Get Maven: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.