Prem
Prem

Reputation: 648

Placeholder for value/version/date replacement in xml using Ant build

I have more than 30 odx-d files (odx-d is just xml file with different extension). All files have common tags:

<DOC-REVISION>
    <REVISION-LABEL>01.02.03-04</REVISION-LABEL>
    <STATE>RELEASE</STATE>
    <DATE>2018-11-14T16:26:00+01:00</DATE>
</DOC-REVISION>

At every release I need to change these values in all files.

Note: Manipulation using Java is not possible as while build just making zip of all these files not using Java to manipulate these files.

Please suggest a way to have one file (any file type you suggest) where I can have these values and place holders for the tags in all these files.

Thanks.!

Upvotes: 1

Views: 1607

Answers (2)

Prem
Prem

Reputation: 648

Solution for multiple files.

  1. Replace values with placeholders @revision@, @state@, @date@ and place into template folder.

  2. Perform the copy operation with filterset from template to dest directory.

Example: Template dir: 'fromDir', destination: 'toDir'

1) Template files:

<DOC-REVISION>
    <REVISION-LABEL>@revision@</REVISION-LABEL>
    <STATE>@state@</STATE>
    <DATE>@date@</DATE>
</DOC-REVISION>

2) Declare properties and perform test target operation.

<!-- Properties -->
<property name="version" value="01.02.03-04" />
<property name="state" value="RELEASE" />
<tstamp>
    <format property="now" pattern="yyyy-MM-dd'T'HH:mm:ss.SSSXXX"/>
</tstamp>

<!-- Target -->
<target name="test">
    <copy todir="${toDir}">
        <fileset dir="${fromDir}" />
        <filterset>
            <filter token="revision" value="${version}" />
            <filter token="state" value="${state}" />
            <filter token="date" value="${now}" />
        </filterset>
    </copy>
</target>

Thanks!

Upvotes: 1

Lolo
Lolo

Reputation: 4357

This is doable with the following steps:

  1. replace the common tag values with placeholders e.g. @revision@, @state@, @date@

  2. copy each file to a temporary location

  3. perform the replacements in the copied files using a <replace file="${dest.file}"> task with nested <replacefilter .../> elements

  4. zip the transformed files in the temporary location

For example, using a template file "template.xml" like this:

<DOC-REVISION>
    <REVISION-LABEL>@revision@</REVISION-LABEL>
    <STATE>@state@</STATE>
    <DATE>@date@</DATE>
</DOC-REVISION>

you can set the real values with this ant target (skipping the zip part):

  <target name="test">
    <property name="my.revision" value="01.02.03-04"/>
    <property name="my.state" value="RELEASE"/>
    <tstamp>
      <format property="my.date" pattern="yyyy-MM-dd hh:mm z"/>
    </tstamp>
    <property name="template.file" value="./template.xml"/>
    <property name="dest.file" value="./doc.odx"/>
    <delete file="${dest.file}" quiet="true"/>
    <copy toFile="${dest.file}" file="${template.file}"/>
    <replace file="${dest.file}">
      <replacefilter token="@revision@" value="${my.revision}"/>
      <replacefilter token="@state@" value="${my.state}"/>
      <replacefilter token="@date@" value="${my.date}"/>
    </replace>
  </target>

Upvotes: 1

Related Questions