Compile JaspeReports from Maven

| October 25, 2011 | 0 Comments

It´s very common in a project software development that we have the requirement to show information reports. In this case, there are many options and one of this is JasperReport.

This post doesn´t pretend to explain the details of integrating JasperReports inside a J2EE web application. The objective is move the process of compilation reports from runtime to compile time. The process is this:

JRXML file=> compile => file Jasper =>execute

Imagine that you have been designed a report with IReport, so you have a file with extension .jrxml. So, when you arge going to use this report inside a Java application you have two choices.

1.Compile the report in execution time, so from your Java code you need do the next:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Obtain the report
 
nombreReport = nombreReport+".jrxml";
 
InputStream reportStream = this.getClass().getResourceAsStream("/reports/"+nombreReport);
 
// Create one object JasperDesign from the JRXML file
 
JasperDesign jd = JRXmlLoader.load(reportStream);
 
// Compile the report
 
JasperReport jr = JasperCompileManager.compileReport(jd);
 
// Create the object used to print the report
 
JasperPrint jp = JasperFillManager.fillReport(jr, params, dataSource);

2.Compile each report using for example Ireport, so from your Java code you need:

1
2
3
4
5
6
7
8
9
10
// Obtain the jasper file directly
 
nombreReport = nombreReport+".jasper";
 
InputStream reportStream = this.getClass().getResourceAsStream("/reportsCompiled/"+nombreReport);
 
JasperReport jr = (JasperReport) JRLoader.loadObject(reportStream);
 
// Create the object used to print the report
JasperPrint jp = JasperFillManager.fillReport(jr, params, dataSource);

The second option is more powerful and increase performance because in execution  time there is no compilation, is more faster than the first option. Obviusly if you need support in your application hot changes at the design of the reports the option 2 is unbeliaviable.

But what happen when you have a high number of jrxml files? ¿Must you compile file by file using Ireport? No, if you want to compile all the reports at the same time you can do that in compile phase of maven. For that, we can use a plugin in maven, including this fragment in your pom.xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	          org.codehaus.mojo
	          jasperreports-maven-plugin
	          1.0-beta-2  
 
        		org.codehaus.mojo
          		jasperreports-maven-plugin  
 
            		//dirReports//reports
            		//dirReports//reportsCompiled  
 
                compile-reports  
 
              compile  
 
              	net.sf.jasperreports
    			jasperreports
    			3.7.6
    			jar
 
				log4j
				log4j
				1.2.16

 

The  jasperreport version  dependency must be the same that the version you are using in your project for jasperreport. You see that you can specify optionally two directories:

  • //dirReports//reports : directory in that there would stay the jrxml files.
  • //dirReports//reportsCompiled : directory in that the plug in add the jasper files.

In this way, when you compile your project using maven, the jasper files are compiled from the jrxml files. Note that if you change any jrxml files, you need to recompile the project again.

————————————————————————————————————————————————–

More information:

 

Tags: , ,

Category: Others

Leave a Reply