Calling Scripts in Maven


Do you use Maven, but don't like programming in Jelly? The idea is to be able to write short scripts in a “real” programming language. Personally, I prefer a Java-like programming language like BeanShell or Groovy, but Jython has many fans. I have been trying to get BeanShell and/or Groovy working in Maven 1.0.2 with limited sucess.

Possible Approaches

There are a variety of possible tags that can be used for scripting, some of which work and some that don't.

Language Specific Jelly Tags

There are a handful of Jelly tags for embedding scripting languages, here is a (partial?) list:

  • beanshell:script (typically used in the bsh namespace)

  • jython:script (I don't remember where I found documentation for this, but see the sample below for an example.)

This is the only approach that I've been able to get working in Maven 1.0.2.

Language-Independent Jelly Tags

There is a bsf:script tag, but I could not get it to work.

Language Specific Ant Tasks

I know of at least one language-specific Ant task for scripting, and am sure there are others that can be added to the list.

Groovy Ant Task

The jsr-02 release of Groovy (not yet available as of this writing) will include the Groovy Ant task that can been used within Maven.

Ant Script Task

There is a Script task in Ant, but it doesn't seem to work from within Maven 1.0.2.

Writing Dynamic Ant Task in a Scripting Language

This is an interesting possibility that I have not had time to investigate. There is at least one webpage that shows how to do it with BeanShell.

To Do List

  • Make it work with the <ant:script> tag, rather than the jelly tags used below.

  • Be able to read and write maven/ant properties from within the script

  • Get Groovy working (probably will have to wait for Maven 1.1)

Sample Files

These sample files document the approach used to get the best results so far with Maven 1.0.2.

Example 1. maven.xml

<?xml version="1.0"?>

  <goal name="test-bsh">
     	System.out.println("Hello from BeanShell, pom.getName() = " + pom.getName());

  <goal name="test-jython">
print "This is a jython script"
print "My Maven POM's name is", pom.getName()
print "If the POM has a name, then the context passing is working"
  <goal name="all" prereqs="test-bsh,  test-jython" />


Example 2. project.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
  <!-- Skipped -->
	<!-- Stuff for Calling Beanshell -->
	<!-- Stuff for Calling Jythhon -->
  <!-- Skipped -->


Creative Commons License
This work is licensed under a Creative Commons License.