6

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.

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
  • possible duplicate of [How do I use properties in ants replace task?](http://stackoverflow.com/questions/5216462/how-do-i-use-properties-in-ants-replace-task) but the answers don't work for me – Richard Le Mesurier Jun 14 '12 at 06:57
  • trying to get these solutions to work, but don't understand them properly: [Ant replace token from properties file](http://stackoverflow.com/questions/3680362/ant-replace-token-from-properties-file) & [Simulating the Maven2 filter mechanism using Ant](http://stackoverflow.com/a/1233820/383414) – Richard Le Mesurier Jun 14 '12 at 07:01

2 Answers2

8

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.

beny23
  • 34,390
  • 5
  • 82
  • 85
0

You can do a multiline replacement with markers where the properties need to go: @__relative_url_to_doc_root__@

     <replace dir="${dir_build_docs_javadoc}">
        <replacetoken><![CDATA[</head>]]></replacetoken>
       <replacevalue><![CDATA[<meta name="viewport" content="width=device-width"/>
<!-- Required for syntax highlighting (1/2)...START -->
  <script type="text/javascript" src="@__relative_url_to_doc_root__@resources/shCore.js"></script>
  <link href="@__relative_url_to_doc_root__@resources/shCore.css" rel="stylesheet" type="text/css"/>
  <link href="@__relative_url_to_doc_root__@resources/shThemeDefault.css" rel="stylesheet" type="text/css"/>
  <script type="text/javascript" src="@__relative_url_to_doc_root__@resources/shBrushJava.js"></script>
<!-- Required for syntax highlighting (1/2)...END -->
</HEAD>]]></replacevalue>
     </replace>

And then run another single line replacement for the properties:

<target name="-replace_all_javadoc_headers">
   <antcall target="-javadoc_replace_headers_in_one_dir">
     <param name="directory_to_replace" value="${dir_build_docs_javadoc}"/>
     <param name="relative_url_to_doc_root" value=""/>
   </antcall>
   <antcall target="-javadoc_replace_headers_in_one_dir">
     <param name="directory_to_replace" value="${dir_build_docs_javadoc}${cg_xbn_codelet}"/>
     <param name="relative_url_to_doc_root" value="../../../../"/>
   </antcall>
   <antcall target="-javadoc_replace_headers_in_one_dir">
     <param name="directory_to_replace" value="${dir_build_docs_javadoc}${cg_xbn_codelet}type${fs}"/>
     <param name="relative_url_to_doc_root" value="../../../../../"/>
   </antcall>
   <antcall target="-javadoc_replace_headers_in_one_dir">
     <param name="directory_to_replace" value="${dir_build_docs_javadoc}${cg_xbn_codelet}taglet${fs}"/>
     <param name="relative_url_to_doc_root" value="../../../../../"/>
   </antcall>
</target>

<target name="-javadoc_replace_headers_in_one_dir">
  <replace dir="${directory_to_replace}" 
        token="@__relative_url_to_doc_root__@" 
        value="${relative_url_to_doc_root}">
     <include name="*.html"/>
  </replace>

aliteralmind
  • 19,847
  • 17
  • 77
  • 108