Calling Scripts in Maven

Motivation

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"?>
<project  
        xmlns:j="jelly:core"
        xmlns:util="jelly:util"
        xmlns:ant="jelly:ant"
        xmlns:bsh="jelly:beanshell"
        xmlns:jython="jelly:jython"
        default="all">

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


  <goal name="test-jython">
    <jython:script>
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"
     </jython:script>
  </goal>
  
  <goal name="all" prereqs="test-bsh,  test-jython" />
  
</project>

	

Example 2. project.xml

 
<?xml version="1.0" encoding="ISO-8859-1"?>
<project>
  <!-- Skipped -->
  <dependencies>
	<dependency>
		<groupId>bsf</groupId>
		<artifactId>bsf</artifactId>
		<version>2.3.0</version>
		<type>jar</type>
		<url>http://bsf.apache.org/</url>
	</dependency>
	<!-- Stuff for Calling Beanshell -->
	<dependency>
		<groupId>bsh</groupId>
		<artifactId>bsh</artifactId>
		<version>1.2-b3</version>
		<type>jar</type>
		<url>http://www.beanshell.org/</url>
	</dependency>
	<dependency>
		<groupId>commons-jelly</groupId>
		<artifactId>commons-jelly-tags-beanshell</artifactId>
		<version>1.0</version>
		<type>jar</type>
	</dependency>
	<!-- Stuff for Calling Jythhon -->
	<dependency>
		<groupId>jython</groupId>
		<artifactId>jython</artifactId>
		<version>2.1</version>
		<type>jar</type>
		<url>http://www.jython.org/</url>
	</dependency>
	<dependency>
		<groupId>commons-jelly</groupId>
		<artifactId>commons-jelly-tags-bsf</artifactId>
		<version>1.0</version>
		<type>jar</type>
	</dependency>
  </dependencies>
  
  <!-- Skipped -->
</project>


      

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