Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANT "replace" task not expanding properties in "replacetoken" tag

Tags:

replace

ant

After build, I need to modify an HTML file that points the client to download the new app.

I search for a token; replace it with a link and token:

<replace file="index.html" >

    <!-- this searches for literal text ${MyTOKEN} -->
    <!-- does not "expand" ${MyTOKEN} before searching -->
    <replacetoken>${MyTOKEN}</replacetoken>

    <replacevalue>"some link" <br> ${MyTOKEN}</replacevalue>
</replace>

This code CANNOT be moved into a template build script because the replacetoken and replacevalue tags take the text as literals - they do not expandproperties in my version of ANT.

I would like to use properties to define the "some link" and MyTOKEN values.


Workaround for using properties in "some link" is to use a filterchain and copy the file after the replacement:

<copy file="index.html" tofile="index2.html" >
    <filterchain>
        <!-- this converts the ${xxx} properties into their values -->
        <expandproperties />
    </filterchain>
</copy>

But that works AFTER the replace has been done - so it means I still need to hard-code the MyTOKEN values directly into the build script.

  • I want to define my token outside of my build script, and reference it inside the build script.
  • How do I do that?

Update: Should I create my own replace task using copy, filterreader and filterchain? I don't really understand that method properly, but it looks like the way.


Update expanding on accepted answer: I was originally using the <replacetoken> & <replacevalue> method because I needed my value to span multiple lines.

By using token & value, I was unable to find a way to make line breaks.

The solution to putting line breaks is to use ${line.separator} as the line break. See docs on the Echo Task.

As an extra, here is a page of some more useful (off topic) ANT properties: Built-in Ant Properties.

like image 200
Richard Le Mesurier Avatar asked Jun 14 '12 06:06

Richard Le Mesurier


1 Answers

Using the token and value attributes works here. This works for me with Ant 1.7.1:

build.properties

token=FOO
tokval=some ${token}

build.xml

<project>
  <property file="build.properties" />
  <target name="repl">
    <replace file="test.txt" token="${token}" value="${tokval}" />
  </target>
</project>

Hope that helps.

like image 146
beny23 Avatar answered Oct 29 '22 01:10

beny23