I came across this
This site is very focused and does not clutter my search results with non-
@parameter [alias="someAlias"] [expression="${someExpression}"]So, when the plugin is built, it builds the plugin descriptor using these and the rest of the parameters
[default-value="value"]
You can also add @readOnly attribute, which makes the parameter read only
and not configurable by the user. To deprecate a parameter, use @deprecated
attribute.
@requiresDependencyResolution- Fla gs this goal as requiring the
dependencies in the specified scope i.e all the dependencies in this scope
would first be resolved before executing the goal.
Notice that this goal runs only within any of your maven
projects. If you run it outside, then it will error out,
since @requiresProject is true by default. You can turn
it off in the mojo, if you want to run the goal in any
directory.
If you want to customize/configure the params you can do
so using the -D parameter.
Plugin Prefix:
As you can see typing the above goal is long and cumbersome.
To avoid typing this everytime, maven assigns plugin prefix.
For our plugin, prefix is "myfirst". This is because maven
automatically generates plugin prefix for your plugin if
your plugin name follows the maven syntax of
$name-maven-plugin or maven-$name-plugin. You can see in
maven-metadata-local.xml in your group id directory in M2
Repo to get an idea of how the prefixes are assigned.<plugin><name>myfirst maven PluginMaven Mojo</name><prefix>myfirst</prefix><artifactId>myfirst-maven-plugin</artifactId></plugin>Here myfirst is the prefix for myfirst maven plugin.But by default, maven searches in the
org.apache.maven.plugins (core plugins) and
org.codehaus.mojo (extra plugins) groups to see if the
plugin is assigned a prefix. It looks at
maven-metadata-central.xml to find this.If we want our
group id to be also searched for prefixes, then we can
do so by adding our plugin group in settings.xml.
Add this block to your ~\.m2\settings.xml:<pluginGroups> <pluginGroup>com.mycompany</pluginGroup></pluginGroups><build>
Now, you should be able to run the same above goal like
this: mvn myfirst:touch
If you do not like the maven generated plugin prefix, you
can also set your own by configuring maven plugin plugin in
your pom. For, example you can add the following to your
pom:
<plugins>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>2.3</version>
<configuration>
<goalPrefix>mycustomprefix</goalPrefix>
</configuration>
</plugin>
</plugins>
</build>Now, you should be able to run the same above goal like
this:
mvn mycustomprefix:touchLogging and Exceptions:During execution, the mojo can get the log using getLog and
log messages. This log is very similar to log4j and has
debug, info, warn, error etc., Mojo should log error
messages if it is recoverable and can still continue. But,
if it cannot continue, it should throw exception. There are
two types of exceptions available and a mojo should decide
on which exception to throw at what time:
MojoExecutionException - Throw this for fatal exceptions
and the build cannot continue. This will cause BUILD ERROR
message to be displayed.
MojoFailureException - The goal cannot continue, but still
the build can continue.
Customizing Mojo:
You can customize the mojo using mojo parameters. You can
customize the parameter using the following ways:
1) Using -D option in command line while executing the goal
(-DparamName=paramValue)
2) Using the properties section in settings.xml
<properties>paramValue
</properties>
3) Adding the goal to the build section of the POM in your
project (by using executions and configuration). You can
even bind your goal to multiple phases and configure them
differently while executing in each phase.
settings.xml takes precendence over -D property and the one
in pom takes precedence over settings.xml.
Parameter Types:
The parameter type can be String, StringBuffer, Character,
Boolean (true or false), Integer, Double, Float, Date
(Example format: "yyyy-MM-dd HH:mm:ssa" (a sample date is
"2009-08-15 3:43:23PM"), URL (http:\\www.mycompany.com),
Files and Directories (C:\temp.txt)
We can also have multi-valued parameters.
1) For array - private String[] files;
<files>
<param>c:\test.txt</param>
<param>c:\test2.txt</param>
</files>
2) Collection (Any implementation of collection interface
like LinkedList, HashSet etc.,)
private HashSet mySetOfFiles;
<mySetOfFiles>
<param>c:\test.txt</param>
<param>c:\test2.txt</param>
</mySetOfFiles>
3) Map (Any implementation of Map interface like HashMap,
LinkedHashMap etc.,)
private Map myMap;
<myMap>
<key1>value1</key1>
<key2>value2</key2>
</myMap>
4) Properties - java.util.properties
private Properties myProps;
<myProps>
<property>
<name>propertyName1</name>
<value>propertyValue1</value>
<property>
<property>
<name>propertyName2</name>
<value>propertyValue2</value>
<property>
</myProps>
5) Any other object - private Tag tag;
(name and desc are fields of Tag)<configuration>
...
<tag>
<name>tagname</name>
<desc>tagdesc</desc>
</tag>
</configuration>...
The rules for mapping complex objects are as follows:
... <configuration implementation="com.mycompany.xyz.Tag">
<tag>
<name>tagname</name>
<desc>tagdesc</desc>
</tag>
</configuration>...
Handling pre-requisites:
You can use @executes goal to achieve this:
This annotation will cause Maven to spawn a parallel build
and execute a goal or a lifecycle in a parallel instance of
Maven that isn't going to affect the current build.
Few examples:
@execute phase="package" - Executes a parallel lifecycle
ending in the specified phase. Results of this execution is
available via maven property${executedProperty}.
@execute goal="eclipse:eclipse" - Executes this goal in
parallel. Results of this does not affect the current build.
Conclusion:
We saw how to create a maven plugin, went through plugin
descriptor file (plugin.xml), and mojo's. We then saw how to
install the maven plugin, how plugin prefix mechanism works,
how to customize mojo parameters and different types of
parameters including custom objects. Finally, we also briefly
touched upon how to execute pre-conditions for the goal. Hope
this helps in understanding how to write maven plugins and
gives you a quick start on writing your own maven plugin.