Compiling sub-reports in Jasper reports

12 / Mar / 2010 by Vishal Sahu 11 comments

In my project we needed to generate pdf reports. The tool we used to design reports is IReport 2.0.5 & the plugin used is grails jasper plugin.The plugin works fine with simple reports but fails when it come to complex report which uses subreport.The problem is that the plugin compiles .jrxml file to .jasper file upto 1 level.It do not compiles subreport.

When we compile.jrxml files using iReport, it takes the hardcoded path of the local machine & hence throws an exception in other environment.
So to overcome this problem i used the following script by putting it in CompileReports.groovy file under scripts folder.

  import org.codehaus.groovy.grails.commons.GrailsClassUtils as GCU
grailsHome ="environment.GRAILS_HOME"
includeTargets << new File("${grailsHome}/scripts/Init.groovy")
target('default': "Compile all Jasper Reports") {
target(compileAll: "Compile all *.jrxml files as *.jasper") {
    def sourceDir = (config.'jasper.dir.reports'?:'reports')
    Ant.echo "sourceDir = ${sourceDir}"
    Ant.taskdef(classpathref:"grails.classpath",name: "jrc", classname: "net.sf.jasperreports.ant.JRAntCompileTask")
    jrc(srcdir:"${basedir}/web-app/${sourceDir}", tempdir: "~/.grails/.jasper") {include(name:"**/*.jrxml")

To compile reports use the command:

grails compile-reports

Helpful links:-

Hope it helps.

Vishal Sahu


comments (11)

  1. vivek yadav

    Hi Vishal
    I need to swap two sub report dynamically . Is it possible from controller to swap 2 sub report .

  2. Richard Olsen

    Using coupons isn’t old-fashioned; it’s a great way for you to find out how you can save a few dollars here and there when you need to buy things. It doesn’t matter what you are buying, there is usually a coupon to help you out. Use the tips in this article to help you get smart about coupons.

  3. Ahmed Pentecost

    Hello there, just became aware of your blog through Google, and found that it’s truly informative. I’m going to watch out for brussels. I’ll be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers! It is the best time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I wish to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I wish to read even more things about it! Excellent post. I was checking constantly this blog and I’m impressed! Very helpful information specially the last part 🙂 I care for such information much.

  4. Sachin Anand

    Thanx Vishal. It was real nice script, but it didn’t work for me in the first go. I had to add setDefaultTarget (‘default’) to make it work, but other than that it works like charm. 🙂 Thnx again.

  5. Vishal

    To define a subreport expression, i used..


    where the params ‘SUBREPORT_DIR’ contains the absolute path of subreport.jasper file

  6. Hitesh

    Hi Enrique ..this script compiles all the jrxml files in the directory … path of the subreport is specified in the main(parent) report


    here DataSource is the Collection that is supplied to sub report
    and SUBREPORT_DIR holds the path of subreport

    To define SUBREPORT_DIR variable in controller (I use )

    import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH
    def  abcAction = {
    String path = "${SCH.servletContext.getRealPath("/")}"              
    params.SUBREPORT_DIR = path + "/reports" 
    //where path of directory where I store reports is ~/appName/web-app/reports
    chain(controller: 'jasper', action: 'index', model: [data: anyVOs], params: params)

    hope it helps

  7. Enrique Medina

    Question 1 was actually:

    Which is the value that you need to define for <subreportExpression>?

  8. Enrique Medina

    A couple of questions:

    1) Which is the value that you need to use for the subreport?

    2) How do you tell the subreport to use fields defined in the parent report? (I’m using the Grails plugin, so no DataSource defined)



Leave a Reply

Your email address will not be published. Required fields are marked *