user3618078
user3618078

Reputation: 55

How do i get the current file name using ant build file?

I recently created an ANT build file to transform a .xml file to .fo using xslt. And the ANT build performs as designed. However, the input file is hard coded into the XSLT task.

How do i dynamically change the input file name without having to edit the build file each time? The following is a snippet of my code.

<target name="createFO"
    description="Transform using XSLT 1.0" depends="clean, copyimg, copysrc">
      <xslt basedir="${srcdir}" 
            destdir="${dstDir}"
            extension=".fo"
            style="${ss}/foobar.xsl"
            in="sample.xml"
            out="${dstDir}/new.fo"/>   

      <echo>The fo file has been created!</echo>
</target>

I failed to mention that i am using OxygenXML to process my ANT file. Sorry.

Upvotes: 0

Views: 857

Answers (1)

David W.
David W.

Reputation: 107040

You don't need the in and out attributes. Instead, you can use a <fileset> that contains the file you want to transform.

<xslt> will strip the suffix from your input file and apply the suffix used in the extension attribute.

<target name="createFO"
    description="Transform using XSLT 1.0" depends="clean, copyimg, copysrc">
    <!-- Don't put "basedir" parameter. It comes from fileset! -->
      <xslt destdir="${dstDir}"
            extension=".fo"
            style="${ss}/foobar.xsl">
            <fileset dir="${xslt.dir}"/>
      </xslt>
      <echo>The fo file has been created!</echo>
</target>

You can also use mappers to transform the input file's name into the output file name too.

The other, of course is to use a property for the input file name, and then have someone pass the name of the file into the Ant script:

<target name="createFO"
    description="Transform using XSLT 1.0" depends="clean, copyimg, copysrc">
      <fail message="You must pass in the parameter &auot;-Dxml.file=...&quot;">
           <condition>
               <not>
                   <available file="${xml.file}">
           </condition>
      </fail>

      <xslt basedir="${srcdir}" 
            destdir="${dstDir}"
            extension=".fo"
            style="${ss}/foobar.xsl"
            in="${xml.file}"
            out="${dstDir}/new.fo"/>   

      <echo>The fo file has been created!</echo>
</target>

Now, to run this, you'd do:

$ ant -Dxml.file=sample.xml createFO

Upvotes: 3

Related Questions