JasperReports plugin for maven2
Published by peter January 11th, 2006 in java.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:
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:
I might contribute the plugin to ibiblio, but till then you can get the sources here!




















0 Responses to “JasperReports plugin for maven2”
Please Wait
Leave a Reply