Maven: The Complete Reference - 11.5. Mojo Parameters |
|
Just as important as the In EchoMojo we declared 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 There are a few ways to populate the message parameter in the
$ mvn first:echo -Decho.message="Hello Everybody" We could 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 could also be configured directly as a configuration value for the plugin. If we wanted to customize the message parameter directly, we could use the following build configuration. The following configuration bypasses the echo.message property and populates the Mojo parameter in plugin configuration. <project> ... <build> <plugins> <plugin> <groupId>org.sonatype.mavenbook.plugins</groupId> <artifactId>first-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <configuration> <message>Hello Everybody!</message> </configuration> </plugin> </plugins> </build> </project> If we wanted to run the <build> <plugins> <plugin> <groupId>org.sonatype.mavenbook.plugins</groupId> <artifactId>first-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <executions> <execution> <id>first-execution</id> <phase>generate-resources</phase> <goals> <goal>echo</goal> </goals> <configuration> <message>The Eagle has Landed!</message> </configuration> </execution> <execution> <id>second-execution</id> <phase>validate</phase> <goals> <goal>echo</goal> </goals> <configuration> <message>${project.version}</message> </configuration> </execution> </executions> </plugin> </plugins> </build> While this last configuration example seems very verbose, it
illustrates the flexibility of Maven. In the previous configuration
example, you’ve bound the Plugins can have parameters which accept more than one value. Take a
look at the A Plugin with Multi-valued Parameters. package org.sonatype.mavenbook.plugins /** * Zips up the output directory. * @goal zip * @phase package */ public class ZipMojo extends AbstractMojo { /** * The Zip archiver. * @parameter \ expression="${component.org.codehaus.plexus.archiver.Archiver#zip}" */ private ZipArchiver zipArchiver; /** * Directory containing the build files. * @parameter expression="${project.build.directory}" */ private File buildDirectory; /** * Base directory of the project. * @parameter expression="${basedir}" */ private File baseDirectory; /** * A set of file patterns to include in the zip. * @parameter alias="includes" */ private String[] mIncludes; /** * A set of file patterns to exclude from the zip. * @parameter alias="excludes" */ private String[] mExcludes; public void setExcludes( String[] excludes ) { mExcludes = excludes; } public void setIncludes( String[] includes ) { mIncludes = includes; } public void execute() throws MojoExecutionException { try { zipArchiver.addDirectory( buildDirectory, includes, excludes ); zipArchiver.setDestFile( new File( baseDirectory, "output.zip" ) ); zipArchiver.createArchive(); } catch( Exception e ) { throw new MojoExecutionException( "Could not zip", e ); } } }
To configure a multi-valued Mojo parameter, you use a series of
elements for each value. If the name of the multi-valued parameter is
<project> ... <build> <plugins> <plugin> <groupId>org.sonatype.mavenbook.plugins</groupId> <artifactId>zip-maven-plugin</artifactId> <configuration> <excludes> <exclude>**/*.txt</exclude> <exclude>**/*~</exclude> </excludes> </configuration> </plugin> </plugins> </build> </project> A Mojo is a component managed by an IoC container called Plexus. A
Mojo can depend on other components managed by Plexus by declaring a
Mojo parameter and using the Depending on a Plexus Component. /** * The Zip archiver. * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="zip" */ private ZipArchiver zipArchiver;
When Maven instantiates this Mojo, it will then attempt to retrieve
the Plexus component with the specified role and role hint. In this
example, the Mojo will be related to a ZipArchiver component which
will allow the Unless you insist on writing your Plugin descriptors by hand, you’ll
never have to write that XML. Instead, the Maven Plugin Plugin has a
@component role="org.codehaus.plexus.archiver.Archiver" roleHint="zip" This would have the effect of retrieving the @parameter expression="'+++${component.org.codehaus.plexus.archiver.Archiver#zip}+++'" While the two annotations are effectively the same, the
|