Migrating Ant builds to use the dita command
Although DITA Open Toolkit still supports Ant builds, switching to the dita command offers a simpler command interface, sets all required environment variables and allows you to run DITA-OT without setting up anything beforehand.
About this task
Building output with the dita command is often easier than using Ant. In particular, you can use .properties files to specify sets of DITA-OT parameters for each build.
You can include the dita command in shell scripts to perform multiple builds.
Procedure
Example: Ant build
Prior to DITA-OT 2.0, an Ant build like this was typically used to define the properties for each target.
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-chm-pdf" default="all" basedir=".">
<property name="dita.dir" location="${basedir}/../../.."/>
<target name="all" description="build CHM and PDF" depends="chm,pdf"/>
<target name="chm" description="build CHM">
<ant antfile="${dita.dir}/build.xml">
<property name="args.input" location="../sequence.ditamap"/>
<property name="transtype" value="htmlhelp"/>
<property name="output.dir" location="../out/chm"/>
<property name="args.gen.task.lbl" value="YES"/>
</ant>
</target>
<target name="pdf" description="build PDF">
<ant antfile="${dita.dir}/build.xml">
<property name="args.input" location="../taskbook.ditamap"/>
<property name="transtype" value="pdf"/>
<property name="output.dir" location="../out/pdf"/>
<property name="args.gen.task.lbl" value="YES"/>
<property name="args.rellinks" value="nofamily"/>
</ant>
</target>
</project>
Example: .properties files with dita command
The following .properties files and dita commands are equivalent to the example Ant build.
Sample .properties file: dita-ot-dir/docsrc/samples/properties/chm.properties
output.dir = out/chm
args.gen.task.lbl = YES
Sample .properties file: dita-ot-dir/docsrc/samples/properties/pdf.properties
output.dir = out/pdf
args.gen.task.lbl = YES
args.rellinks = nofamily
Run from dita-ot-dir/docsrc/samples:
dita --input=sequence.ditamap --format=htmlhelp \
--propertyfile=properties/chm.properties
dita --input=taskbook.ditamap --format=pdf \
--propertyfile=properties/pdf.properties
Example: Call the dita command from an Ant build
In some cases, you might still want to use an Ant build to implement some pre- or post-processing steps, but
also want the convenience of using the dita command with .properties
files to define the parameters for each build. This can be accomplished with Antโs <exec>
task.
This example uses a <dita-cmd>
Ant macro defined in the dita-ot-dir/docsrc/samples/ant_sample/dita-cmd.xml file:
<macrodef name="dita-cmd">
<attribute name="input"/>
<attribute name="format"/>
<attribute name="propertyfile"/>
<sequential>
<!-- For Unix run the DITA executable-->
<exec taskname="dita-cmd" executable="${dita.dir}/bin/dita" osfamily="unix" failonerror="true">
<arg value="--input"/>
<arg value="@{input}"/>
<arg value="--format"/>
<arg value="@{format}"/>
<arg value="--propertyfile"/>
<arg value="@{propertyfile}"/>
</exec>
<!-- For Windows run DITA from a DOS command -->
<exec taskname="dita-cmd" dir="${dita.dir}/bin" executable="cmd" osfamily="windows" failonerror="true">
<arg value="/C"/>
<arg value="dita --input @{input} --format @{format} --propertyfile=@{propertyfile}"/>
</exec>
</sequential>
</macrodef>
<dita-cmd input="sample.ditamap" format="pdf" propertyfile="sample.properties"/>
This approach allows you to use Ant builds to perform additional tasks at build time while allowing the dita command to set the classpath and ensure that all necessary JAR libraries are available.
Sample build file: dita-ot-dir/docsrc/samples/ant_sample/build-chm-pdf-hybrid.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-chm-pdf-hybrid" default="all" basedir=".">
<description>An Ant build that calls the dita command</description>
<include file="dita-cmd.xml"/><!-- defines the <dita-cmd> macro -->
<target name="all" depends="pre,main,post"/>
<target name="pre">
<description>Pre-processing steps</description>
</target>
<target name="main">
<description>Build the CHM and PDF with the dita command</description>
<property name="absolute.path.base" location="../"/>
<dita-cmd
input="${absolute.path.base}/sequence.ditamap"
format="htmlhelp"
propertyfile="${absolute.path.base}/properties/chm.properties"
/>
<dita-cmd
input="${absolute.path.base}/taskbook.ditamap"
format="pdf"
propertyfile="${absolute.path.base}/properties/pdf.properties"
/>
</target>
<target name="post">
<description>Postprocessing steps</description>
</target>
</project>