Writing Beanshell Plugins for Maven 2.0

The Beanshell support for Maven 2.0 landed earlier this week. All in all quite simple to get going (just a few hours, with no prior beanshell expertise), which bodes well for adding other scripting languages later as all the scaffolding is already there.

The following script below is an example:

/**
 * Beanshell mojo integration test.
 * @goal it0020
 */

import org.apache.maven.plugin.Mojo;
import org.apache.maven.script.beanshell.BeanshellMojoAdapter;
import org.codehaus.plexus.util.FileUtils;

execute()
{
    logger.info( "Executing it0020..." );
    print( "info level?" );
    FileUtils.fileWrite( outDir.getAbsolutePath() + "/out.txt",
        "This is a Beanshell test" );
}

/**
 * Output directory for files.
 *
 * @parameter expression="${project.build.directory}" type="java.io.File"
 * @required
 */
setOutDir( file )
{
    outDir = file;
}

return new BeanshellMojoAdapter( (Mojo) this, this.interpreter );

Each script file contains one “mojo” (a term originally coming from Maven POJO used to describe a single goal within a plugin). Several scripts can be included in a single plugin.

The bshdoc comments in here are used to generate a plugin descriptor for Maven, rather than editing the XML by hand. The opening comment gives the description and goal name.

The execute() method is what is called each time the goal is run Any valid beanshell can be used here, and as you can see there is a built in logger variable that can be used for output (as can print). Note that the script is instantiated each time, so any state stored globally is not retained. This may be made configurable in the future.

Any setXXX methods declare the input variables which can be configured from the POM and are used to build the plugin descriptor. Note that at present the methods are not actually called, the variables are just inserted. The expression and type give the default value and what to coerce it to.

Finally, the return statement is required to make sure that Maven gets back the contents with the right interface so it can be executed.

Currently, this all needs to be bundled up in a JAR like other plugins. I think that there would be a strong demand to edit on the fly for scripted plugins, so there may need to be development-only support for editing these in place or even into a running instace.

It is quite similar to a Java plugin, so it may be that people still opt for those unless they are looking for a way to edit on the fly, have some existing code, or particularly want something loosely typed.

Of course, you can also mix and match (eg include Java code, and call that from your beanshell script or vice-versa).

Any thoughts? How would others like to see this work?

Advertisements

One response to “Writing Beanshell Plugins for Maven 2.0

  1. Hi!
    Is there more detailed documentation on Scripting support in M2?
    What scripts are being supported apart from Beanshell?
    cheers,
    Rahul

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s