gpt4 book ai didi

java - 使用 ant 通过 sciptlet 编译 jrxml

转载 作者:行者123 更新时间:2023-12-01 15:38:54 29 4
gpt4 key购买 nike

我正在尝试使用 ant 编译 jrxml。我已经在 iReports 上创建了 jrxml,所以我没有 build.xml。运行 ant 命令时,它会要求提供 build.xml。我在与 jrxml 相同的目录中创建了此文件,但我不知道应该在其中放入什么内容以将我的 jrxml 链接到我的 scriptlet jar。我会感谢你的帮助,我有点迷失了..

最佳答案

您可以在 net.sf.jasperreports.ant.JRAntCompileTask ant 任务的帮助下编译报告模板。

样本取自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>

来自网站的引用:

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

  • 工作示例:

    SampleJRScriptlet 类:

    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);
    }
    }

    要编译的报告模板(report_with_scriplet.jrxml 文件):

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

    我的ant脚本(compile_report.xml文件):

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

    文件夹结构:

    report
    report_with_scriplet.jrxml
    compile_report.xml

    运行脚本后文件夹结构将是:

    report
    report_with_scriplet.jrxml
    compiled_jasper
    report_with_scriplet_1323195663885_780040.groovy
    report_with_scriplet.jasper
    compile_report.xml

    关于java - 使用 ant 通过 sciptlet 编译 jrxml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8400775/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com