JasperReports plugin for maven2

Now we are managing builds using Maven2 we are facing some interesting problems dealing with existing Ant tasks. Maven2 does have support to run Ant tasks directly from the POM but since there was some time on the project I decided to rewrite the existing report compile task from the Jasper Reports project (written by Henri Chen and Kees Kuip).

Writing the Maven2 plugin isn’t to difficult, for once the documentation on the Maven2 website is quite extensive.

It need to be mentioned that Maven2 plugins do not necessarily need to be written in Java, I’ve also come across beanshell and even Ruby implementations. Getting started meant creating a basis project, using the default archetype:

  mvn archetype:create -DgroupId=com.finalist 
	-DartifactId=maven-jasper-compiler 
	-Dpackagename=com.finalist.mvn.plugins 
	-Dpacking=maven-plugin -Dversion=0.0.1 

This generates the basic project structure and a basis pom. Some modifications to the pom are necessary/useful:

  • Change the packing type to ‘maven-plugin’
  • add some descriptive stuff to the pom (like scm, developer info)
  • add the needed dependencies

I added the following dependencies:



jasperreports
jasperreports
1.1.1


commons-digester
commons-digester
1.7


org.apache.maven
maven-project
2.0.1


org.apache.maven
maven-plugin-api
2.0.1

O.k. back to writing the plugin. First of all, I had to extend from ‘AbstractMojo’ which implements most of the infrastructure required to implement a Maven2 plugin. Add some properties, and… that is basically it.

Useful to know is how to map properties from the project file to the plugin, more on this can be found here.

I discovered JasperReports is quite tricky to configure, the report compiler actually executes javac and needs to have a valid classpath passed to it! Brrr… I had a look at the sourcecode for the maven WAR plugin to see how they handled the references to all jars:

Step 1 - Add a MavenProject property (and add a getter and setter)


/**
* The project to create a build for.
*
* @parameter expression=”${project}”
* @required
*/
private MavenProject project;

Step 2 - Iterate over the project artifacts


Set dependencies = project.getArtifacts();
String path = “”;
for (Iterator iter = dependencies.iterator(); iter.hasNext();) {
Artifact artifact = (Artifact) iter.next();
path += artifact.getFile().getAbsolutePath() + System.getProperty(”path.separator”);
}

path += srcDir.getAbsolutePath(); // add in case additional resources need to be loaded from the source path

JRProperties.setProperty(JRProperties.COMPILER_CLASSPATH, path);

The above assumes that all required resources to compile JasperReports are on the classpath.
Now, after coding the entire pluging, just run ‘mvn install’ which installs the plugin in the local repository!
The plugin can be used in a project by adding the following to your pom’s build section:

com.finalist
maven-jasperreports-compiler
0.0.5


src/main/resources/jasperreports/

src/main/resources/jasperreports/


compile
compileReports



I might contribute the plugin to ibiblio, but till then you can get the sources here!


0 Responses to “JasperReports plugin for maven2”

  1. No Comments

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


golden_gate_warning_sign javaone2008 keynote Scribbled Sun Logo nearby hotel Golden Gate Acme Anvile at CommunityOne Keynote javaone 2008 goodybag Stretched Limo Charles Nutter & Guillaume Laforge Community One Keynote alcatraz smashmouth Hotel room Okke en Rudie sea_lion Tim Bray introducing the (J)Ruby panel Rudie Greenland Joshua Bloch at JavaOne2008 Stage being build in the nearby park
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.