You are previewing Maven: The Definitive Guide.

Maven: The Definitive Guide

Cover of Maven: The Definitive Guide by Sonatype Company Published by O'Reilly Media, Inc.
O'Reilly logo

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:

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

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

The best content for your career. Discover unlimited learning on demand for around $1/day.