iReport Designer - Groovy

iReport And Groovy

JasperReports 0.6.6 adds support for scripting languages avoiding the complexity experienced by report writers that don't know Java. A "sample" implementation provided by JasperReports is the support for Groovy, a Java-compatible scripting language.

Groovy simplifies the report designer's life, and it leaves to skilled expressions developers all the power of the Java language. Here are some examples, that show how simple and elegant Groovy ican be relative to the use of pure Java expressions:

Expression Java Groovy
Field $F{field_name} $F{field_name}
Sum of two double fields new Double($F{f1}.doubleValue() + $F{f2}.doubleValue()) $F{f1} + $F{f2}
Comparision of numbers new Boolean($F{f}.intValue() == 1) $F{f} == 1
Comparision of strings new Boolean($F{f} != null && $F{f}.equals("test")) $F{f} == "test"

Groovy is fully Java-compatible. This means that we we can reference pur Java clases in Groovy expressions. The following is a correct Groovy expression:

new JREmptyDataSource($F{num_of_void_recors})

JREmptyDataSource is a class of JasperReports that generates a set of empty records (without all fields set to null) on the fly. Here you see how we can instance this pure Java class in Groovy without any problem.

Groovy is a great example of a scripting language that opens the doors of JasperReports to all people that don't know Java.

Using Groovy in iReport

iReport 0.5.0 provides all developers need to use Groovy to write report expressions. To activate the "Groovy" mode, you have to set the "language" used in the current report. To do this, go to the report properties, tab scriptlet and choose groovy as expressions language.

When the language is set to Groovy, iReport uses the Groovy compiler to generate the Jasper file (the compiler choosen in the options dialog, i.e. "Java compiler" or "Jasperreports default compiler" is ignored). To use Groovy as the default language in iReport, set the option "Default language for expressions".

Deploy Reports that Use Groovy

If you use Groovy as a scripting language for expressions, you have to add the Groovy-all-1.0-beta-10.jar into your application classpath in order to run your reports succesfully. If you need to recompile your reports outside iReport, you'll need jr-groovy-compiler.jar too (this jar is shipped with iReport, but contains classes provided by JasperReports). To compile your "Groovy" reports you can use the following lines of code:

System.setProperty( "jasper.reports.compiler.class", "net.sf.jasperreports.compilers.JRGroovyCompiler" );
JasperCompileManager.compileReportToFile( "myfile.jrxml", "myfile.jasper" );