ANTを使用してSciptletでJRXMLをコンパイルします
-
28-10-2019 - |
質問
ANTを使用してJRXMLをコンパイルしようとしています。 iReportsでJRXMLを作成したので、build.xmlを持っていません。 Antコマンドを実行するとき、それはbuild.xmlを要求します。 JRXMLと同じレパートリーでこのファイルを作成しましたが、JRXMLをScriptlet JARにリンクするために何を入れるべきかわかりません。私はあなたの助けを求めます、私は一種の失われています。
解決
レポートテンプレートをコンパイルして、 net.sf.jasperreports.ant.jrantcompiletask アリタスク。
撮影したサンプル ここ:
<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>
サイトからの引用:
SRCDIRとDestDIR属性に加えて、JASPERREPORTSに出荷されたJRCカスタムアリタスクは、次の属性をサポートしています。
コンパイラ:JRCompilerインターフェイスを実装するクラスの名前は、レポートをコンパイルするために使用されます(オプション)。 XMLValidation:XML検証をソースレポートテンプレートファイルで実行する必要があるかどうかを示すフラグ(デフォルトでは)。 TempDir:一時的に生成されたファイルを保存する場所(デフォルトで現在の作業ディレクトリ)。 KeepJava:その場で生成された一時的なJavaファイルを保持し、自動的に削除しないかどうかを示すフラグ(デフォルトではFalse)。
作業サンプル:
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>
私のアリのスクリプト( 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
実行中のSRIPTフォルダー構造が次のとおりです。
report
report_with_scriplet.jrxml
compiled_jasper
report_with_scriplet_1323195663885_780040.groovy
report_with_scriplet.jasper
compile_report.xml
所属していません StackOverflow