Sample Reference - Schema Reference - Configuration Reference - API (Javadoc)

JasperReports - Ant Compile Sample (version 4.0.1)


Shows how multiple JRXML files can be compiled in batch mode using the ANT build tool.

Download All Sample Source Files
Browse Sample Source Files on SVN


Main Features in This Sample

Compiling Multiple Report Template Files Using the Ant Build Tool

Generating the JRXML Source Files for Multiple Compiled Report Template Files Using the Ant Build Tool (Decompiling)

Secondary Features
Report Compilers


top

Compiling Multiple Report Template Files Using the Ant Build ToolDocumented by Teodor Danciu


Description / Goal
How to compile all your JRXML report source files at application build time using the Ant build tool.

Since
0.4.6

Other Samples
/demo/samples/beanshell
/demo/samples/groovy
/demo/samples/java1.5
/demo/samples/javascript


The JRXML files represent the source files for static report templates. These report templates need to be prepared for use at runtime by compiling them into *.jasper files, which are basically serialized JasperReport objects, ready for filling with data.

Report template source files having the *.jrxml file extensions are compiled into serialized object files having the *.jasper file extension, just like Java source files having the *.java file extension are transformed into Java bytecode binary files with the *.class file extension.
The transformation of *.jrxml files into *.jasper files should be part of the application build process, just as the compilation of *.java files into *.class files is. In the majority of cases, when the report templates are static and do not change at runtime (only data feed into them changes), there is no point in deploying source JRXML files with the application.
After all, when deploying a Java application, you deploy *.class files, packaged up in JARs, not source *.java files. The same technique is applicable to JR report template files, where compiled *.jasper files should be created at application built time and then deployed as part of the application classpath as resources.

The JasperReports library provides a built-in Ant task for compiling source JRXML report template files into *.jasper files. This task is represented by the JRAntCompilerTask task and works very similar to the Ant built-in Javac task.

This task scans source folders and looks up for report template files (usually having the JRXML file extension) and compiles them into *.jasper files which are placed into a destination folder hierarchy.
The destination folder tree is similar to the source folder tree, meaning that the relative location of source files from the root source folder is preserved for the resulting *.jasper files.

Similar to the Javac task, the source folders can be specified using either the srcdir attribute of the task (when there is only one root source folder) or using a nested <src> tag (when source files are scattered across multiple paths).

Open the build.xml file in the current sample and notice how the custom Ant task called jrc is defined:

<taskdef name="jrc" classname="net.sf.jasperreports.ant.JRAntCompileTask"> 
  <classpath refid="classpath"/>
</taskdef>

This task definition uses a <classpath> element which contains the JasperReports JAR and all its required dependencies.

Then, the custom jrc task is used to compile report templates having the *.jrxml file extension found under the sample's reports folder. This source folder is specified using the srcdir attribute of the jrc task in the compile1 target of the build.xml file:

<target name="compile1"> 
  <mkdir dir="./build/reports"/> 
  <jrc 
    srcdir="./reports"
    destdir="./build/reports"
    tempdir="./build/reports"
    keepjava="true"
    xmlvalidation="true">
   <classpath refid="runClasspath"/>
   <include name="**/*.jrxml"/>
  </jrc>
</target> 

The compile2 target in the same build.xml file performs the same report compilation process, but the source folder is specified using a nested <src> tag with filesets. The nested source tag allows compiling report templates that are scattered through many different locations and are not grouped under a single root report source folder.

<target name="compile2">
  <mkdir dir="./build/reports"/> 
  <jrc 
    destdir="./build/reports"
    tempdir="./build/reports"
    keepjava="true"
    xmlvalidation="true">
   <src>
    <fileset dir="./reports">
     <include name="**/*.jrxml"/>
    </fileset>
   </src>
   <classpath refid="runClasspath"/>
  </jrc> 
</target> 

Notice that both report compilation targets have a <classpath> nested element, used to specify the classpath used by the report compiler. This so called run-classpath contains classes that are referenced inside the report templates themselves, such as scriptlet classes, chart customizers and so forth.
In this particular sample, both source JRXML files make use of such helper classes found in the src folder of the samples. These helper classes have to be compiled before the report templates are compiled, using the javac target of the build.xml file.

In addition to the srcdir and the destdir attributes, the jrc custom Ant task shipped with JasperReports supports the following attributes:

  • compiler : Name of the class that implements the JRCompiler interface to be used for compiling the reports (optional).
  • xmlvalidation : Flag to indicate whether the XML validation should be performed on the source report template files (true by default).
  • tempdir : Location to store the temporarily generated files (the current working directory by default).
  • keepjava : Flag to indicate if the temporary Java files generated on the fly should be kept and not deleted automatically (false by default).


In our sample, we use the default report compiler, which is the JDT-based compiler, because the JDT JAR is found in the classpath. This compiler works on the assumption that report expressions are Java expressions and thus it produces a Java class file dynamically containing all the report expressions and compiles it using the JDT Java compiler. Normally, this report compiler does all the Java class file generation and compilation in-memory and does not work with actual files on disk, which makes it very flexible and easy to deploy in all environments. However, if the keepjava flag is turned to true, it will save the report's temporary Java source file on disk, in the specified tempdir. This is useful for debugging report expressions in certain cases.

Depending on the report expression language they are mapped to, other report compiler implementations might produce script files instead of Java files, for report expression evaluation. The keepjava and the tempdir attributes will still work for them, except that the files that will be kept into the temporary location will not be Java source files but script files, also helpful for debugging.

top

Generating the JRXML Source Files for Multiple Compiled Report Template Files Using the Ant Build Tool (Decompiling)Documented by


Description / Goal
How to re-create the JRXML source files for multiple compiled report templates using the Ant build tool. This is useful in cases where only the compiled *.jasper files of older reports are available, the initial *.jrxml source files being lost.

Since
3.7.1


[Under Construction]



© 2001-2010 Jaspersoft Corporation www.jaspersoft.com