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, which are covered here which can make Maven plugin development with Groovy better, faster, stronger ![]()
Be sure to read over the documentation for Building Groovy Projects. A Groovy Maven plugin is a Groovy project, so most of the information there is relevant here as well.
Putting More Groove into your Mojo
Using ant
Using ant allows your Mojo access to any Ant tasks. Groovy Mojo's which extend from GroovyMojoSupport have access to an AntBuilder instance bound to the ant field.
This field is lazily initialized when you first reference it.
Below are some small examples of using ant to perform common tasks, but really the sky is the limit on what you can do. See the Ant User Manual for more tasks you can execute ![]()
| NOTE The following examples assume that your Mojo implementation has already defined a project field as in: |
Touching a File
ant.touch(file: "${project.build.directory}/stamp")
Copying Files
def dir = "${project.build.directory}/backup"
ant.mkdir(dir: dir)
ant.copy(todir: dir) {
fileset(dir: "${project.build.outputDirectoy}") {
include(name: '**/*')
}
}
Execute Something
def propname = 'lsout' ant.exec(executable: '/bin/ls', outputproperty: propname) def value = ant.antProject.properties[propname]
Using fail()
Most mojos need to report back some failure status, which is normally done by throwing a MojoExecutionException. Groovy Mojos can simply invoke the fail() method, which handles the details of throwing for you.
Failing with a simple string:
fail("That ain't no woman! It's a man, man!")
Failing with an exception detail:
try {
....
}
catch (Exception e) {
fail(e)
}
Failing with an exception detail and a message:
try {
....
}
catch (Exception e) {
fail("She's the village bicycle! Everybody's had a ride.", e)
}
gmaven-archetype-mojo Archetype
To help get Groovy plugins started faster, you can use the gmaven-archetype-mojo. This will create a new project with the basic POM configuration and an example Groovy-based Mojo class to get you started quickly:
mvn archetype:generate -DarchetypeGroupId=org.codehaus.groovy.maven.archetypes -DarchetypeArtifactId=gmaven-archetype-mojo
|
To use a specific version of an archetype specify |
The Maven Archetype Plugin will ask a few questions about your new project:
[INFO] [archetype:generate] ... Define value for groupId: : org.mycompany.myproject Define value for artifactId: : example-maven-plugin Define value for version: : 1.0-SNAPSHOT Define value for package: : org.mycompany.myproject.example Confirm properties configuration: name: Example Maven Plugin groupId: org.mycompany.myproject artifactId: example-maven-plugin version: 1.0-SNAPSHOT package: org.mycompany.myproject.example Y: : y ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ ...
|
Please ignore any |
The above example would have created the following project structure:
example-maven-plugin example-maven-plugin/pom.xml example-maven-plugin/src example-maven-plugin/src/main example-maven-plugin/src/main/groovy example-maven-plugin/src/main/groovy/org example-maven-plugin/src/main/groovy/org/mycompany example-maven-plugin/src/main/groovy/org/mycompany/myproject example-maven-plugin/src/main/groovy/org/mycompany/myproject/example example-maven-plugin/src/main/groovy/org/mycompany/myproject/example/HelloMojo.groovy
gmaven-plugin Packaging
TODO |
