Writing a Beanshell Maven Plugin

You can write a Maven plugin using Beanshell.

Pom configuration

  <dependencies>
    <dependency>
      <groupId>bsh</groupId>
      <artifactId>bsh</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-script-beanshell</artifactId>
      <version>2.2.1</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.3</version>
        <dependencies>
          <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-tools-beanshell</artifactId>
            <version>3.3</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

Beanshell plugin

A Beanshell mojo uses same javadoc tags as a Java mojo with javadoc tags.

Sample script content:

/**
 * Touches a test file.
 *
 * @goal touch
 * @requiresDependencyResolution=test
 * @deprecated Don't use!
 * @since 1.2
 */

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



execute()
{
    logger.info( "Executing beanshell mojo..." );
    FileUtils.fileWrite( outDir + "/" + name, "This is a Beanshell test" );
}

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

/**
 *
 *
 * @parameter expression="${name}"
 * @required
 */
setName( name )
{
    name = name;
}

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