Chapter 23. Split Task

This task splits a property or file into pieces. This is similar to the "split" utility found on most Unix and Linux distributions.

To use this task in your build files, include a task definition like this:


    <taskdef name="split" classname="ise.antelope.tasks.SplitTask"/>
   

Once a file has been split into smaller pieces, it can be rejoined with the "concat" task that is part of the standard Ant core tasks.

Table 23.1. Split Task Attributes

AttributeDescriptionDefaultRequired
prefixThe start of the filename(s) to write the parts to.xNo
bytesHow big, in bytes, to make the individual pieces. In general, use lines for text files, bytes or size for binary files.NoneNo
sizeHow big to make the individual pieces. Like the Unix/Linux split utility, this attribute allows modifiers: b for 512, k for 1K, m for 1 Meg, so 100k is the same as passing 102400 in the bytes attribute. In general, use lines for text files, bytes or size for binary files.NoneNo
linesHow big to make the individual pieces in lines. In general, use lines for text files, bytes or size for binary files.1000No
propertySplit the value of the property into several files.NoneNo
stringSplit this string into several files.NoneNo
fileSplit this file into several files.NoneNo
outputdirDestination for the output files.NoneMaybe. If file is given and output dir is not, will write to the same directory as file, otherwise, this is a required attribute.
failonerrorShould the build stop if this task fails?YesNo

Examples

Split the value of a property into several files:


   <target name="test1" depends="clean">
      <property name="prop1" value="ABCDE"/>
      <a:split property="prop1" bytes="1" outputdir="${out_dir}"/>
      <a:fileutil file="${out_dir}" property="file_count">
         <a:filecount/>
      </a:fileutil>
      <a:assert message="Expected 5 files, got ${file_count}">
         <bool>
            <a:mathequals arg1="5" arg2="${file_count}"/>
         </bool>
      </a:assert>
   </target>

This will result in 5 files named x.0, x.1, x.2, x.3, and x.4, each containing a single character.

This more involved example shows how to split ant.jar into several files each 100000 bytes in size. The files will be names ant.jar.0, ant.jar.1, ..., ant.jar.10. Then the parts are put back together with concat.


   <target name="test2" depends="clean">
      <!-- make sure ant.jar is available -->
      <property name="ant.jar" value="${ant.library.dir}/ant.jar"/>
      <available property="ant.jar.available" file="${ant.jar}"/>

      <a:if name="ant.jar.available" value="true">
         <!-- ant.jar generally runs around 1MB in size, so split into 100000 byte pieces -->
         <a:unset name="piece_size"/>
         <property name="piece_size" value="100000"/>
         <a:split file="${ant.jar}" bytes="${piece_size}" outputdir="${out_dir}" prefix="ant.jar"/>

         <!-- count the parts -->
         <a:fileutil file="${out_dir}" property="file_count">
            <a:filecount/>
         </a:fileutil>

         <!-- calculate how many parts there should be -->
         <a:fileutil file="${ant.jar}" property="ant_size">
            <a:filelength/>
         </a:fileutil>
         <a:math result="split_size">
            <a:op op="ceil">
               <a:op op="/">
                  <a:num value="${ant_size}"/>
                  <a:num value="${piece_size}"/>
               </a:op>
            </a:op>
         </a:math>

         <!-- make sure there are the right number of parts -->
         <a:assert message="Expected ${split_size} files, got ${file_count}">
            <bool>
               <a:mathequals arg1="${split_size}" arg2="${file_count}"/>
            </bool>
         </a:assert>

         <!-- sort the filenames of the parts so concat puts them together in
         the right order-->
         <a:fileutil file="${out_dir}" property="file_list">
            <a:listfiles includepath="no"/>
         </a:fileutil>
         <a:stringutil string="${file_list}" property="file_list">
            <a:sort/>
         </a:stringutil>

         <!-- put them back together -->
         <concat destfile="${out_dir}/ant.jar" binary="true">
            <filelist dir="${out_dir}" files="${file_list}"/>
         </concat>

         <!-- make sure the new file is the identical to the original -->
         <a:assert message="concat did not produce identical file">
            <bool>
               <filesmatch file1="${ant.jar}" file2="${out_dir}/ant.jar"/>
            </bool>
         </a:assert>
      </a:if>
   </target>