migrating-ant-to-dita.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html
  2. SYSTEM "about:legacy-compat">
  3. <html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="UTF-8"><meta name="copyright" content="(C) Copyright 2019"><meta name="DC.rights.owner" content="(C) Copyright 2019"><meta name="DC.type" content="task"><meta name="description" content="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."><meta name="DC.subject" content="Ant, exec, dita-cmd, command, benefits of, dita, command, migrating Ant scripts, classpath"><meta name="keywords" content="Ant, exec, dita-cmd, command, benefits of, dita, command, migrating Ant scripts, classpath"><meta name="DC.relation" scheme="URI" content="../topics/build-using-dita-command.html"><meta name="DC.format" content="HTML5"><meta name="DC.identifier" content="migrating-ant-to-dita"><link rel="stylesheet" type="text/css" href="../css/commonltr.css"><link rel="stylesheet" type="text/css" href="../css/dita-ot-doc.css"><title>Migrating Ant builds to use the dita command</title></head><body id="migrating-ant-to-dita"><header role="banner"><div class="header">
  4. <p>DITA Open Toolkit</p>
  5. <hr>
  6. </div></header><nav role="toc"><ul><li><a href="../index.html">DITA Open Toolkit 3.3</a></li><li><a href="../release-notes/index.html">Release Notes</a></li><li><a href="../topics/installing-client.html">Installing DITA-OT</a></li><li><a href="../topics/alternative-input-formats.html">Authoring formats</a></li><li><a href="../topics/building-output.html">Building output</a><ul><li><a href="../topics/build-using-dita-command.html">Using the dita command</a><ul><li><a href="../topics/using-dita-properties-file.html">Using a properties file</a></li><li class="active"><a href="../topics/migrating-ant-to-dita.html">Migrating Ant builds</a></li></ul></li><li><a href="../topics/publishing-with-ant.html">Using Ant</a></li><li><a href="../reference/java-api.html">Using the Java API</a></li><li><a href="../topics/output-formats.html">Output formats</a></li></ul></li><li><a href="../parameters/index.html">Setting parameters</a></li><li><a href="../topics/customizing.html">Customizing DITA-OT</a></li><li><a href="../topics/troubleshooting-overview.html">Troubleshooting</a></li><li><a href="../reference/index.html">Reference</a></li><li><a href="../topics/dita-and-dita-ot-resources.html">Resources</a></li></ul></nav><main role="main"><article role="article" aria-labelledby="ariaid-title1">
  7. <h1 class="title topictitle1" id="ariaid-title1">Migrating Ant builds to use the <span class="keyword cmdname">dita</span> command</h1>
  8. <div class="body taskbody"><p class="shortdesc">Although DITA Open Toolkit still supports Ant builds, switching to the <span class="keyword cmdname">dita</span> command
  9. offers a simpler command interface, sets all required environment variables and allows you to run DITA-OT without
  10. setting up anything beforehand.</p>
  11. <section class="section context"><div class="tasklabel"><h2 class="sectiontitle tasklabel">About this task</h2></div>
  12. <p class="p">Building output with the <span class="keyword cmdname">dita</span> command is often easier than using Ant. In particular, you can
  13. use <span class="ph filepath">.properties</span> files to specify sets of DITA-OT parameters for each build.</p>
  14. <p class="p">You can include the <span class="keyword cmdname">dita</span> command in shell scripts to perform multiple builds.</p>
  15. <div class="note tip note_tip"><span class="note__title">Tip:</span> Add the absolute path for <span class="ph filepath"><var class="keyword varname">dita-ot-dir</var></span><span class="ph filepath">/bin</span> to the <var class="keyword varname">PATH</var> environment variable to run the
  16. <span class="keyword cmdname">dita</span> command from any location on the file system without typing the path.
  17. </div>
  18. </section>
  19. <section><div class="tasklabel"><h2 class="sectiontitle tasklabel">Procedure</h2></div><ol class="ol steps"><li class="li step stepexpand">
  20. <span class="ph cmd">In your Ant build file, identify the properties set in each build target.</span>
  21. <div class="itemgroup info">
  22. <div class="note note note_note"><span class="note__title">Note:</span> Some build parameters might be specified as properties of the project as a whole. You can refer to a
  23. build log to see a list of all properties that were set for the build.</div>
  24. </div>
  25. </li><li class="li step stepexpand">
  26. <span class="ph cmd">Create a <span class="ph filepath">.properties</span> file for each build and specify the needed build parameters, one
  27. per line, in the format <code class="ph codeph">name = value</code>.</span>
  28. </li><li class="li step stepexpand">
  29. <span class="ph cmd">Use the <span class="keyword cmdname">dita</span> command to perform each build, referencing your
  30. <span class="ph filepath">.properties</span> with the <span class="keyword parmname">--propertyfile</span>=<var class="keyword varname">file</var>
  31. option.</span>
  32. </li></ol></section>
  33. <section class="example"><h2 class="title sectiontitle">Example: Ant build</h2>
  34. <p class="p">Prior to DITA-OT 2.0, an Ant build like this was typically used to define the properties for each target.</p>
  35. <div class="p">Sample build file: <span class="ph filepath" id="migrating-ant-to-dita__samples-dir"><var class="keyword varname">dita-ot-dir</var>/docsrc/samples</span><span class="ph filepath">/ant_sample/build-chm-pdf.xml</span>
  36. <pre class="pre codeblock language-xml normalize-space show-line-numbers show-whitespace"><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  37. &lt;project name="build-chm-pdf" default="all" basedir="."&gt;
  38. &lt;property name="dita.dir" location="${basedir}/../../.."/&gt;
  39. &lt;target name="all" description="build CHM and PDF" depends="chm,pdf"/&gt;
  40. &lt;target name="chm" description="build CHM"&gt;
  41. &lt;ant antfile="${dita.dir}/build.xml"&gt;
  42. &lt;property name="args.input" location="../sequence.ditamap"/&gt;
  43. &lt;property name="transtype" value="htmlhelp"/&gt;
  44. &lt;property name="output.dir" location="../out/chm"/&gt;
  45. &lt;property name="args.gen.task.lbl" value="YES"/&gt;
  46. &lt;/ant&gt;
  47. &lt;/target&gt;
  48. &lt;target name="pdf" description="build PDF"&gt;
  49. &lt;ant antfile="${dita.dir}/build.xml"&gt;
  50. &lt;property name="args.input" location="../taskbook.ditamap"/&gt;
  51. &lt;property name="transtype" value="pdf"/&gt;
  52. &lt;property name="output.dir" location="../out/pdf"/&gt;
  53. &lt;property name="args.gen.task.lbl" value="YES"/&gt;
  54. &lt;property name="args.rellinks" value="nofamily"/&gt;
  55. &lt;/ant&gt;
  56. &lt;/target&gt;
  57. &lt;/project&gt;</code></pre></div></section>
  58. <section class="example"><h2 class="title sectiontitle">Example: <span class="ph filepath">.properties</span> files with <span class="keyword cmdname">dita</span> command</h2>
  59. <p class="p">The following <span class="ph filepath">.properties</span> files and <span class="keyword cmdname">dita</span> commands are equivalent to the
  60. example Ant build.</p>
  61. <p class="p">Sample <span class="ph filepath">.properties</span> file: <span class="ph filepath"><var class="keyword varname">dita-ot-dir</var>/docsrc/samples</span><span class="ph filepath">/properties/chm.properties</span></p>
  62. <div class="p">
  63. <pre class="pre codeblock language-properties normalize-space show-line-numbers show-whitespace"><code>output.dir = out/chm
  64. args.gen.task.lbl = YES
  65. args.breadcrumbs = yes</code></pre></div>
  66. <p class="p">Sample <span class="ph filepath">.properties</span> file: <span class="ph filepath"><var class="keyword varname">dita-ot-dir</var>/docsrc/samples</span><span class="ph filepath">/properties/pdf.properties</span></p>
  67. <div class="p">
  68. <pre class="pre codeblock language-properties normalize-space show-line-numbers show-whitespace"><code>output.dir = out/pdf
  69. args.gen.task.lbl = YES
  70. args.rellinks = nofamily</code></pre></div>
  71. <p class="p">Run from <span class="ph filepath"><var class="keyword varname">dita-ot-dir</var>/docsrc/samples</span>:</p>
  72. <pre class="pre codeblock"><code><span class="keyword cmdname">dita</span> <span class="keyword parmname">--input</span>=<span class="ph filepath">sequence.ditamap</span> <span class="keyword parmname">--format</span>=<span class="keyword option">htmlhelp</span> \
  73. <span class="keyword parmname">--propertyfile</span>=<span class="ph filepath">properties/chm.properties</span>
  74. <span class="keyword cmdname">dita</span> <span class="keyword parmname">--input</span>=<span class="ph filepath">taskbook.ditamap</span> <span class="keyword parmname">--format</span>=<span class="keyword option">pdf</span> \
  75. <span class="keyword parmname">--propertyfile</span>=<span class="ph filepath">properties/pdf.properties</span></code></pre>
  76. </section>
  77. <section class="example"><h2 class="title sectiontitle">Example: Call the <span class="keyword cmdname">dita</span> command from an Ant build</h2>
  78. <p class="p">In some cases, you might still want to use an Ant build to implement some pre- or post-processing steps, but
  79. also want the convenience of using the <span class="keyword cmdname">dita</span> command with <span class="ph filepath">.properties</span>
  80. files to define the parameters for each build. This can be accomplished with Ant’s <code class="keyword markupname xmlelement">&lt;exec&gt;</code>
  81. task.</p>
  82. <p class="p">This example uses a <code class="keyword markupname xmlelement">&lt;dita-cmd&gt;</code> Ant macro defined in the <span class="ph filepath"><var class="keyword varname">dita-ot-dir</var>/docsrc/samples</span><span class="ph filepath">/ant_sample/dita-cmd.xml</span> file:</p>
  83. <div class="p">
  84. <pre class="pre codeblock language-xml normalize-space show-line-numbers show-whitespace"><code>&lt;macrodef name="dita-cmd"&gt;
  85. &lt;attribute name="input"/&gt;
  86. &lt;attribute name="format"/&gt;
  87. &lt;attribute name="propertyfile"/&gt;
  88. &lt;sequential&gt;
  89. &lt;!-- For Unix run the DITA executable--&gt;
  90. &lt;exec taskname="dita-cmd" executable="${dita.dir}/bin/dita" osfamily="unix" failonerror="true"&gt;
  91. &lt;arg value="--input"/&gt;
  92. &lt;arg value="@{input}"/&gt;
  93. &lt;arg value="--format"/&gt;
  94. &lt;arg value="@{format}"/&gt;
  95. &lt;arg value="--propertyfile"/&gt;
  96. &lt;arg value="@{propertyfile}"/&gt;
  97. &lt;/exec&gt;
  98. &lt;!-- For Windows run DITA from a DOS command --&gt;
  99. &lt;exec taskname="dita-cmd" dir="${dita.dir}/bin" executable="cmd" osfamily="windows" failonerror="true"&gt;
  100. &lt;arg value="/C"/&gt;
  101. &lt;arg value="dita --input @{input} --format @{format} --propertyfile=@{propertyfile}"/&gt;
  102. &lt;/exec&gt;
  103. &lt;/sequential&gt;
  104. &lt;/macrodef&gt;</code></pre></div>
  105. <div class="p">You can use this macro in your Ant build to call the <span class="keyword cmdname">dita</span> command and pass the
  106. <span class="keyword parmname">input</span>, <span class="keyword parmname">format</span> and <span class="keyword parmname">propertyfile</span> parameters as
  107. follows:
  108. <pre class="pre codeblock language-xml"><code>&lt;dita-cmd input="sample.ditamap" format="pdf" propertyfile="sample.properties"/&gt;</code></pre>
  109. </div>
  110. <p class="p">This approach allows you to use Ant builds to perform additional tasks at build time while allowing the
  111. <span class="keyword cmdname">dita</span> command to set the classpath and ensure that all necessary JAR libraries are
  112. available.</p>
  113. <div class="note note note_note"><span class="note__title">Note:</span> The attributes defined in the Ant macro are required and must be supplied each time the task is run. To set
  114. optional parameters in one build (but not another), use different <span class="ph filepath">.properties</span> files for
  115. each build.</div>
  116. <p class="p">Sample build file: <span class="ph filepath"><var class="keyword varname">dita-ot-dir</var>/docsrc/samples</span><span class="ph filepath">/ant_sample/build-chm-pdf-hybrid.xml</span></p>
  117. <div class="p">
  118. <pre class="pre codeblock language-xml normalize-space show-line-numbers show-whitespace"><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  119. &lt;project name="build-chm-pdf-hybrid" default="all" basedir="."&gt;
  120. &lt;description&gt;An Ant build that calls the dita command&lt;/description&gt;
  121. &lt;include file="dita-cmd.xml"/&gt;&lt;!-- defines the &lt;dita-cmd&gt; macro --&gt;
  122. &lt;target name="all" depends="pre,main,post"/&gt;
  123. &lt;target name="pre"&gt;
  124. &lt;description&gt;Preprocessing steps&lt;/description&gt;
  125. &lt;/target&gt;
  126. &lt;target name="main"&gt;
  127. &lt;description&gt;Build the CHM and PDF with the dita command&lt;/description&gt;
  128. &lt;property name="absolute.path.base" location="../"/&gt;
  129. &lt;dita-cmd input="${absolute.path.base}/sequence.ditamap" format="htmlhelp"
  130. propertyfile="${absolute.path.base}/properties/chm.properties"/&gt;
  131. &lt;dita-cmd input="${absolute.path.base}/taskbook.ditamap" format="pdf"
  132. propertyfile="${absolute.path.base}/properties/pdf.properties"/&gt;
  133. &lt;/target&gt;
  134. &lt;target name="post"&gt;
  135. &lt;description&gt;Postprocessing steps&lt;/description&gt;
  136. &lt;/target&gt;
  137. &lt;/project&gt;</code></pre>
  138. </div>
  139. </section>
  140. </div>
  141. <nav role="navigation" class="related-links"><div class="familylinks"><div class="parentlink"><strong>Parent topic:</strong> <a class="link" href="../topics/build-using-dita-command.html" title="You can generate output using the dita command-line tool. Build parameters can be specified on the command line or with .properties files.">Building output using the dita command</a></div></div></nav></article></main></body></html>