Derek Lawrence
Derek Lawrence

Reputation: 1608

Ant build.xml only replacing one instance of token

Im using the replace task to replace a token in my html pages but it only appears to be replacing the token in 1 file and skipping the rest.

<replace dir="${basedir}" token="@CDN" value="&quot;${cdnUrl}&quot;">
  <include name="*.html"/>
</replace>

Any ideas why its only replacing this value in one of the html files in this directory?

Upvotes: 0

Views: 387

Answers (1)

David W.
David W.

Reputation: 107040

Maybe you should do something like this?

<replace dir="${basedir}" token="@CDN" value="&quot;${cdnUrl}&quot;">
    <include name="**/*.html"/>
</replace>

It's hard to say. Are the other HTML files in the directory or subdirectories?

I discourage my developers to use <replace> because you end up replacing files that are checked into the source control system. The developers commit their changes, and the files that were modified with <replace> end up getting updated with the parameters gone. We usually end up with multiple commits as developers try to untangle the mess (which usually gets caught in production deployments).

I recommend to copy the files, and use <filterset> and <filter> to do the replacement:

<mkdir dir="${basedir}/target"/>  <!-- Or whatever you call your build directory -->
<copy todir="${basedir}/target">
    <fileset dir="${basedir}">
        <include name="**/*.html"/>
    </fileset>
    <filterset prefix="@" suffix="@">
        <filter token="CDN"
             value="&quot;${cdnUrl}&quot;"/>
    </filterset>
</copy>

This copies your HTML files into the target directory (which is the build directory). When the developer runs the clean target, it merely deletes that target directory.

Upvotes: 1

Related Questions