------ Guide to Configuring Plug-ins ------ Jason van Zyl Vincent Siveton ------ 2009-08-26 ------ ~~ Licensed to the Apache Software Foundation (ASF) under one ~~ or more contributor license agreements. See the NOTICE file ~~ distributed with this work for additional information ~~ regarding copyright ownership. The ASF licenses this file ~~ to you under the Apache License, Version 2.0 (the ~~ "License"); you may not use this file except in compliance ~~ with the License. You may obtain a copy of the License at ~~ ~~ http://www.apache.org/licenses/LICENSE-2.0 ~~ ~~ Unless required by applicable law or agreed to in writing, ~~ software distributed under the License is distributed on an ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~~ KIND, either express or implied. See the License for the ~~ specific language governing permissions and limitations ~~ under the License. ~~ NOTE: For help with the syntax of this file, see: ~~ http://maven.apache.org/doxia/references/apt-format.html Guide to Configuring Plug-ins [[1]] {{{Generic_Configuration}Generic Configuration}} [[1]] {{{Help_Goal}Help Goal}} [[2]] {{{Configuring_Parameters}Configuring Parameters}} [[1]] {{{Mapping_Simple_Objects}Mapping Simple Objects}} [[2]] {{{Mapping_Complex_Objects}Mapping Complex Objects}} [[3]] {{{Mapping_Collections}Mapping Collections}} [[1]] {{{Mapping_Lists}Mapping Lists}} [[2]] {{{Mapping_Maps}Mapping Maps}} [[3]] {{{Mapping_Properties}Mapping Properties}} [] [] [] [[2]] {{{Configuring_Build_Plugins}Configuring Build Plugins}} [[1]] {{{Using_the_executions_Tag}Using the <<<\>>> Tag}} [[2]] {{{Using_the_dependencies_Tag}Using the <<<\>>> Tag}} [[3]] {{{Using_the_inherited_Tag_In_Build_Plugins}Using the <<<\>>> Tag In Build Plugins}} [] [[3]] {{{Configuring_Reporting_Plugins}Configuring Reporting Plugins}} [[1]] {{{Using_the_reporting_Tag_VS_build_Tag}Using the <<<\>>> Tag VS <<<\>>> Tag}} [[2]] {{{Using_the_reportSets_Tag}Using the <<<\>>> Tag}} [[3]] {{{Using_the_inherited_Tag_In_Reporting_Plugins}Using the <<<\>>> Tag In Reporting Plugins}} [] [] * Introduction In Maven, there are the build and the reporting plugins: * <> will be executed during the build and then, they should be configured in the <<<\>>> element. * <> will be executed during the site generation and they should be configured in the <<<\>>> element. [] All plugins should have minimal required {{{http://maven.apache.org/ref/current/maven-model/maven.html#class_plugin}informations}}: <<>>, <<>> and <<>>. <>: It is recommended to always defined each version of the plugins used by the build to guarantee the build reproducibility. A good practice is to specify them in the <<<\\\>>> elements for <> build plugins (generally, you will define a \ element in a parent POM). For reporting plugins, you should specify each version in the <<<\\\>>> elements (and surely in the <<<\\\>>> elements too). * {Generic Configuration} Maven plugins (build and reporting) are configured by specifying a <<<\>>> element where the child elements of the <<<\>>> element are mapped to fields, or setters, inside your Mojo (remember that a plug-in consists of one or more Mojos where a Mojo maps to a goal). Say, for example, we had a Mojo that performed a query against a particular URL, with a specified timeout and list of options. The Mojo might look like the following: +----+ /** * @goal query */ public class MyQueryMojo extends AbstractMojo { /** * @parameter expression="${query.url}" */ private String url; /** * @parameter default-value="60" */ private int timeout; /** * @parameter */ private String[] options; public void execute() throws MojoExecutionException { ... } } +----+ To configure the Mojo from your POM with the desired URL, timeout and options you might have something like the following: +----+ ... maven-myquery-plugin 1.0 http://www.foobar.com/query 10 ... +----+ As you can see the elements in the configuration match the names of the fields in the Mojo. The configuration mechanism Maven employs is very similar to the way {{{http://xstream.codehaus.org}XStream}} works where elements in XML are mapped to objects. So from the example above you can see that the mapping is pretty straight forward the <<>> element maps to the <<>> field, the <<>> element maps to the <<>> field and the <<>> element maps to the <<>> field. The mapping mechanism can deal with arrays by inspecting the type of the field and determining if a suitable mapping is possible. For mojos that are intended to be executed directly from the CLI, their parameters usually provide a means to be configured via system properties instead of a <<<\>>> section in the POM. The plugin documentation for those parameters will list an that denotes the system properties for the configuration. In the mojo above, the parameter <<>> is associated with the expression <<<$\{query.url\}>>>, meaning its value can be specified by the system property <<>> as shown below: +----+ mvn myquery:query -Dquery.url=http://maven.apache.org +----+ Note that the name of the system property does not necessarily match the name of the mojo parameter. While this is a rather common practice, you will often notice plugins that employ some prefix for the system properties to avoid name clashes with other system properties. Though rarely, there are also plugin parameters that (e.g. for historical reasons) employ system properties which are completely unrelated to the parameter name. So be sure to have a close look at the plugin documentation. ** {Help Goal} Recent Maven plugins have generally an <<>> goal to have in the command line the description of the plugin, with their parameters and types. For instance, to understand the javadoc goal, you need to call: +----+ mvn javadoc:help -Ddetail -Dgoal=javadoc +----+ And you will see all parameters for the javadoc:javadoc goal, similar to this {{{http://maven.apache.org/plugins/maven-javadoc-plugin/javadoc-mojo.html}page}}. ** {Configuring Parameters} *** {Mapping Simple Objects} Mapping simple types, like Boolean or Integer, is very simple. The <<<\>>> element might look like the following: +----+ ... a string true 10 1.0 c:\temp http://maven.apache.org ... +----+ *** {Mapping Complex Objects} Mapping complex types is also fairly straight forward in Maven so let's look at a simple example where we are trying to map a configuration for Person object. The <<<\>>> element might look like the following: +----+ ... Jason van Zyl ... +----+ The rules for mapping complex objects are as follows: * There must be a private field that corresponds to name of the element being mapped. So in our case the <<>> element must map to a <<>> field in the mojo. * The object instantiated must be in the same package as the Mojo itself. So if your mojo is in <<>> then the mapping mechanism will look in that package for an object named <<>>. As you can see the mechanism will capitalize the first letter of the element name and use that to search for the object to instantiate. * If you wish to have the object to be instantiated live in a different package or have a more complicated name then you must specify this using an <<>> attribute like the following: [] +----+ ... Jason van Zyl ... +----+ *** {Mapping Collections} The configuration mapping mechanism can easily deal with most collections so let's go through a few examples to show you how it's done: **** {Mapping Lists} Mapping lists works in much the same way as mapping to arrays where you a list of elements will be mapped to the List. So if you have a mojo like the following: +----+ public class MyAnimalMojo extends AbstractMojo { /** * @parameter */ private List animals; public void execute() throws MojoExecutionException { ... } } +----+ Where you have a field named <<>> then your configuration for the plug-in would look like the following: +----+ ... maven-myanimal-plugin 1.0 cat dog aardvark ... +----+ Where each of the animals listed would be entries in the <<>> field. Unlike arrays, collections have no specific component type. In order to derive the type of a list item, the following strategy is used: [[1]] If the XML element contains an <<>> hint attribute, that is used [[2]] If the XML tag contains a <<<.>>>, try that as a fully qualified class name [[3]] Try the XML tag (with capitalized first letter) as a class in the same package as the mojo/object being configured [[4]] If the element has no children, assume its type is <<>>. Otherwise, the configuration will fail. [] **** {Mapping Maps} In the same way, you could define maps like the following: +-----+ ... /** * My Map. * * @parameter */ private Map myMap; ... +-----+ +-----+ ... value1 value2 ... +-----+ **** {Mapping Properties} Properties should be defined like the following: +-----+ ... /** * My Properties. * * @parameter */ private Properties myProperties; ... +-----+ +-----+ ... propertyName1 propertyValue1 propertyName2 propertyValue2 ... +-----+ * {Configuring Build Plugins} The following is only to configure Build plugins in the <<<\>>> element. ** {Using the <<<\>>> Tag} You can also configure a mojo using the <<<\>>> tag. This is most commonly used for mojos that are intended to participate in some phases of the {{{../introduction/introduction-to-the-lifecycle.html}build lifecycle}}. Using <<>> as an example, you may have something that will look like: +----+ ... maven-myquery-plugin 1.0 execution1 test http://www.foo.com/query 10 query execution2 http://www.bar.com/query 15 query ... +----+ The first execution with id "execution1" binds this configuration to the test phase. The second execution does not have a <<<\>>> tag, how do you think will this execution behave? Well, goals can have a default phase binding as discussed further below. If the goal has a default phase binding then it will execute in that phase. But if the goal is not bound to any lifecycle phase then it simply won't be executed during the build lifecycle. Note that while execution id's have to be unique among all executions of a single plugin within a POM, they don't have to be unique across an inheritance hierarchy of POMs. Executions of the same id from different POMs are merged. The same applies to executions that are defined by profiles. How about if we have a multiple executions with different phases bound to it? How do you think will it behave? Let us use the example POM above again, but this time we shall bind <<>> to a phase. +----+ ... ... execution1 test ... execution2 install http://www.bar.com/query 15 query ... +----+ If there are multiple executions bound to different phases, then the mojo is executed once for each phase indicated. Meaning, <<>> will be executed applying the configuration setup when the phase of the build is test, and <<>> will be executed applying the configuration setup when the build phase is already in install. Now, let us have another mojo example which shows a default lifecycle phase binding. +----+ /** * @goal query * @phase package */ public class MyBindedQueryMojo extends AbstractMojo { /** * @parameter expression="${query.url}" */ private String url; /** * @parameter default-value="60" */ private int timeout; /** * @parameter */ private String[] options; public void execute() throws MojoExecutionException { ... } } +----+ From the above mojo example, <<>> is by default bound to the package phase (see the <<<@phase>>> notation). But if we want to execute this mojo during the install phase and not with package we can rebind this mojo into a new lifecycle phase using the <<<\>>> tag under <<<\>>>. +----+ ... maven-myquery-plugin 1.0 execution1 install http://www.bar.com/query 15 query ... +----+ Now, <<>> default phase which is package has been overrided by install phase. <> Configurations inside the <<<\>>> tag differ from those that are outside <<<\>>> in that they cannot be used from a direct command line invocation. Instead they are only applied when the lifecycle phase they are bound to are invoked. Alternatively, if you move a configuration section outside of the executions section, it will apply globally to all invocations of the plugin. ** {Using the <<<\>>> Tag} You could configure the dependencies of the Build plugins, commonly to use a more recent dependency version. For instance, the Maven Antrun Plugin version 1.2 uses Ant version 1.6.5, if you want to use the latest Ant version when running this plugin, you need to add <<<\>>> element like the following: +----+ ... org.apache.maven.plugins maven-antrun-plugin 1.2 ... org.apache.ant ant 1.7.1 org.apache.ant ant-launcher 1.7.1 ... +----+ ** {Using the <<<\>>> Tag In Build Plugins} By default, plugin configuration should be propagated to child POMs, so to break the inheritance, you could uses the <<<\>>> tag: +----+ ... org.apache.maven.plugins maven-antrun-plugin 1.2 false ... ... +----+ * {Configuring Reporting Plugins} The following is only to configure Reporting plugins in the <<<\>>> element. ** {Using the <<<\>>> Tag VS <<<\>>> Tag} Configuring a reporting plugin in the \ or \ elements in the pom does <> have the same behavior! [<<>>] It uses <> the parameters defined in the \ element of each reporting Plugin specified in the \ element, i.e. <<>> always <> the parameters defined in the \ element of each plugin specified in \. [<<>>] It uses <> the parameters defined in the \ element of each reporting Plugin specified in the \ element; if a parameter is not found, it will look up to a parameter defined in the \ element of each plugin specified in \. [] ** {Using the <<<\>>> Tag} You can configure a reporting plugin using the <<<\>>> tag. This is most commonly used to generate reports selectively when running <<>>. The following will generate only the project team report. +----+ ... org.apache.maven.plugins maven-project-info-reports-plugin 2.1.2 project-team ... +----+ <>: [[1]] To exclude all reports, you need to use: +----+ +----+ [[2]] Refer to each Plugin Documentation (i.e. plugin-info.html) to know the available report goals. [] ** {Using the <<<\>>> Tag In Reporting Plugins} Similar to the build plugins, to break the inheritance, you can use the <<<\>>> tag: +----+ ... org.apache.maven.plugins maven-project-info-reports-plugin 2.1.2 false ... +----+