Message-ID: <555628172.41052.1371636982691.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_41051_1993550038.1371636982690" ------=_Part_41051_1993550038.1371636982690 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Implementing Maven plugins has never been Groovier!
Groovy Maven plugins are very similar to Java Maven plugins. Actually a= Groovy Maven plugin is compiled into Java byte-code, and once built, Maven= can not tell the difference between a plugin which has been implemented in= Java or Groovy.
For the most part, the existing guide for Developing Maven plugins with Java will also apply= to developing plugins with Groovy. There are some differences though, whi= ch are covered here which can make Maven plugin development with Groovy bet= ter, faster, stronger
Be sure to read over the documentation for Building Groovy Projects. A Groovy Maven plugi= n is a Groovy project, so most of the information there is relevant here as= well.
Just like Java-based Mojo's at its simplest, a Groovy Mojo consists of a= single class. For more complicated plugins you are free to use as many cla= sses as needed of course, just like Java... and you can even write Java cla= sses too if you need too.
Groovy Mojo implementations are denoted by the
c annotation on the class (er, just like Java). Actually, all of the Javadoc annotations which are supported by Java plugins are also=
supported by Groovy plugins. This is because, when building a Groovy proj=
ect, the sub-generator spits out Java sources with Javadocs intact, which t=
he Maven Plugin Plugin then parses for an=
Here is our simple Mojo class which has no parameters and spits out a re= latively meaningless string via logging:
There are a few minor points to note here. First, we are referencing the=
plugins logger via
log.info(), where in Java one would need t=
Second, there is no need to mark the
execute() method as th=
rowing any exceptions, though you can still throw any exceptions you need t=
o. You can of course declare what your methods are throwing if you want to.=
Lastly, this example mojo extends from org.codehaus.groovy=
.maven.mojo.GroovyMojo, which is recommended for most Groovy mojos. Of=
course you can always use
org.apache.maven.plugin.AbstractMojo at the cost of some additional bits of happiness which help make your Moj=
Groovy plugins use the same Java plugin descriptor extractor. The Java e=
xtractor uses the generated stubs to build the descriptor, so be sure to in=
Instead of depending on
maven-plugin-api Groovy plugins dep=
gmaven-mojo, which picks up the required Maven dependen=
cies and the default Groovy runtime provider.
And of course, we need to hook up the
gmaven-plugin to comp=
ile the Groovy sources into class files (as well as a few others).
Below is a POM for the simple sample groovy mojo:
|Dependency and Plugin Versions|
To use the above example you must configure the
Once you have your pom setup then you can build the plugin in the normal= way via:
Mojo parameters work exactly the same as they do for Ja=
va plugins. Simply define a field in your Mojo implementation and annotate =
the field with a
@parameter Javadoc tag.
Configuration of Mojo parameters is... as you might expect, the same as = with Java Mojos:
The main thing to note about Groovy Mojos and parameters, is that fields= should be typed so that Maven (or well, Plexus) can inject objects of the = proper type. Using the def keyword is the same as typing the field as and O= bject which Maven will probably still inject just fine, but it won't perfor= m any conversion.
ant allows your Mojo access to any Ant tasks. Gr=
oovy Mojo's which extend from
GroovyMojo have access to an
This field is lazily initialized when you first reference it.
Below are some small examples of using
ant to perform commo=
n tasks, but really the sky is the limit on what you can do. See the Ant User Manual for more tasks you can execute
The following examples assume that your Mojo implementation has already = defined a project property as in:
Most mojos need to report back some failure status, which is normally do=
ne by throwing a
MojoExecutionException. Groovy Mojos can simp=
ly invoke the
fail() method, which handles the details of thro=
wing for you.
Failing with a simple string:
Failing with an exception detail:
Failing with an exception detail and a message:
To help get Groovy plugins started faster, you can use the
archetype-mojo. This will create a new project with the basic POM co=
nfiguration and an example Groovy-based Mojo class to get you started quick=
To use a specific version of an archetype specify
The Maven Archetype Plugin will ask= a few questions about your new project:
Please ignore any
The above example would have created the following project structure: