Search code examples
antjasper-reports

How to compile each jasper file to its own directory?


I'm trying to create a ant script to compile my jasper files, but I have many "srcdir" and "destdir":

<target name="all"> 
  <jrc
    srcdir="many..."
    destdir="many..."
    tempdir="any"
    xmlvalidation="true">
   <classpath refid="classpath"/>
   <include name="**/*.jrxml"/>
  </jrc>
</target>

...and I would like it to compile each file to it's own dir. For every ".jrxml" file.

Is there a way?


Solution

  • You can use ant-contrib foreach task to loop over each jrxml file and call the jrc task for each of those. If you don't have it, you'll need to install ant-contrib by copying its JAR file to the lib directory of your Ant installation (if you're using Eclipse, you can add it by going to "Window > Preferences > Ant > Runtime" and adding the JAR into "Ant Home Entries").

    The following defines a target "all" that will select all the jrxml files under the current directory. For each of those file, the "jrc" target will be called and the corresponding file will be referenced by the property jrxml.file.

    Inside this task, the directory where the jrxml file is located is retrieved with the dirname task and the name of the jrxml file is retrieved with the basename task. The built .jasper file will be created under a folder having the same name as the jrxml file. (It needs to be created first with the mkdir task).

    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />
    
    <target name="all"> 
      <foreach target="jrc" param="jrxml.file">
        <path>
          <fileset dir=".">
            <include name="**/*.jrxml"/>
          </fileset>
        </path>
      </foreach>
    </target>
    
    <target name="jrc">
      <dirname property="jrxml.dir" file="${jrxml.file}"/>
      <basename property="jrxml.filename" file="${jrxml.file}" suffix="jrxml"/>
      <mkdir dir="${jrxml.dir}/${jrxml.filename}"/>
      <jrc srcdir="${jrxml.dir}"
           destdir="${jrxml.dir}/${jrxml.filename}"
           tempdir="${jrxml.dir}/${jrxml.filename}"
           xmlvalidation="true">
        <classpath refid="classpath"/>
        <include name="${jrxml.filename}.jrxml"/>
      </jrc>
    </target>
    

    As an example, if you have a structure:

    +folder
    +--jrxml
    +----Example1.jrxml
    +----Example2.jrxml
    

    the result will be

    +folder
    +--jrxml
    +----Example1.jrxml
    +----Example1
    +------Example1.jasper
    +----Example2.jrxml
    +----Example2
    +------Example2.jasper