views:

58

answers:

1

Hi,

I'm about to write several Maven plugins to ease the life of the R&D people. I'm contemplating between writing the plugin in Groovy or in Java.

The plugin would most likely need to:

  • Use Git commands such as checkout, clone, etc.
  • Download pom.xml files of specific artifacts from a remote repository (our internal Nexus)
  • Parse the pom.xml file to extract several XML elements (such as SCM tags, and specific elements I need for my plugin).
  • Work with the file system (is directory exists, delete a directory, etc).
  • Get metdata information from a pom file such as the dependencies.

Any tips you might have from your vast experience in writing plugins in both languages, will help me greatly!

+6  A: 

I have a lot of experience with both languages and have also written some Maven plugins. I wrote them in Java, because I had no choice, but if I had the choice I would choose Groovy.

In my opinion, unless performance is absolutely critical, Groovy is usually a better choice than Java no matter what you're doing. I say this because it usually takes 30%-50% less code to write something in Groovy than it does in Java. Most of the code you omit when using Groovy is what would be considered boilerplate Java code.

Groovy's compactness is due to two factors

  • More sophisticated language features (closures, properties, operator overloading, default arguments, etc.)
  • High-productivity extensions to the JDK. The GDK adds lots of extremely useful methods to the Java standard libraries. For example, in Java it takes about 10-20 lines of code to read the content of a file (and handle exceptions correctly). Using Groovy you can simply use:

    String fileContent = new File('/foo/bar/baz.txt').text
    

Also, GMaven provides support for writing Maven plugins in Groovy which looks a lot nicer than the AbstractMojo class that one must use when writing a plugin in Java

Aside

You mentioned that your plugin would need to parse pom.xml. If this is the pom.xml of the project in which your plugin is installed, you should not need to parse the content, because all the information about the project is available to the plugin via an instance of MavenProject.

Don
Thanks for the detailed answer (points will be granted of course)!
Asaf Mesika
Nice answer Don. +1
Pascal Thivent