Question

I'm trying to compile a jrxml using ant. I've created my jrxml on iReports so I dont have a build.xml. When runing ant command, it asks for a build.xml. I created this file in the same repertory as my jrxml but I dont know what I should put into it to link my jrxml to my scriptlet jar. I'll apreaciate your help, I'm a kind of lost..

Was it helpful?

Solution

You can compile the report template with help of the net.sf.jasperreports.ant.JRAntCompileTask ant task.

The sample taken from here:

<path id="runClasspath">
    <pathelement location="${path_to_jasper_libs}"/>
    <pathelement path="${path_to_scriplet}\scriplet.jar"/>
</path>

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

<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>

<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> 

The quote from the site:

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).

  • The working sample:

    The SampleJRScriptlet class:

    import com.google.common.base.Strings;
    import net.sf.jasperreports.engine.JRDefaultScriptlet;
    
    public class SampleJRScriptlet extends JRDefaultScriptlet {
    
        public String doubleField(String value) {
            return Strings.repeat(value, 2);
        }
    }
    

    The report template to compile (the report_with_scriplet.jrxml file):

    <jasperReport ... scriptletClass="SampleJRScriptlet">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <queryString language="xPath">
            <![CDATA[/Northwind/Customers]]>
        </queryString>
        <field name="CustomerID" class="java.lang.String">
            <fieldDescription><![CDATA[CustomerID]]></fieldDescription>
        </field>
        <field name="CompanyName" class="java.lang.String">
            <fieldDescription><![CDATA[CompanyName]]></fieldDescription>
        </field>
        <field name="ContactName" class="java.lang.String">
            <fieldDescription><![CDATA[ContactName]]></fieldDescription>
        </field>
        <field name="ContactTitle" class="java.lang.String">
            <fieldDescription><![CDATA[ContactTitle]]></fieldDescription>
        </field>
        <detail>
            <band height="20" splitType="Stretch">
                <textField>
                    <reportElement x="0" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$P{REPORT_SCRIPTLET}.doubleField("$F{CustomerID}")]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="100" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{CompanyName}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="200" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{ContactName}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="300" y="0" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{ContactTitle}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>
    

    My ant script (the compile_report.xml file):

    <project default="compile" basedir=".">
        <path id="classpath">
            <fileset dir="./../../target/alternateLocation">
                <include name="jasperreports-4.1.2.jar"/>
                <include name="commons-logging-1.0.2.jar"/>
                <include name="commons-digester-1.7.jar"/>
                <include name="commons-collections-2.1.jar"/>
                <include name="commons-beanutils-1.8.0.jar"/>
                <include name="groovy-all-1.0-jsr-05.jar"/>
            </fileset> 
        </path>
    
        <path id="runClasspath">
            <path refid="classpath"/> 
            <pathelement path="./../../target/myscriplet.jar"/>
        </path>
    
        <taskdef name="jrc" classname="net.sf.jasperreports.ant.JRAntCompileTask"> 
            <classpath refid="classpath"/>
        </taskdef>
    
        <target name="compile">
            <mkdir dir="./compiled_jasper"/> 
            <jrc 
        destdir="./compiled_jasper"
        tempdir="./compiled_jasper"
        keepjava="true"
        xmlvalidation="true">
                <src>
                    <fileset dir="./report">
                        <include name="**/*.jrxml"/>
                    </fileset>
                </src>
                <classpath refid="runClasspath"/>
            </jrc> 
        </target> 
    </project>
    

    The folder structure:

    report
      report_with_scriplet.jrxml
    compile_report.xml
    

    After the running sript folder structure will be:

    report
      report_with_scriplet.jrxml
    compiled_jasper
      report_with_scriplet_1323195663885_780040.groovy
      report_with_scriplet.jasper
    compile_report.xml
    
    Licensed under: CC-BY-SA with attribution
    Not affiliated with StackOverflow
    scroll top