Summary: An Ant multiline text replacement example.
I'm working on a Java project where we recently started using the JNDI capabilities in Glassfish, but our development environment has been to use Tomcat as our application server (without JNDI lookups). To keep my Apache Ant build process working in both environments I quickly learned that I was going to need to perform multiline text replacements with Ant.
I could tell you what didn't work, but I'll just skip to what did work. For my purposes I had to perform replacements in two files, my Spring applicationContext.xml
file and my web.xml
file. Here's the abridged version of what I had to do to make the replacements in the web.xml
file. First, here's the important part of the web.xml
file:
<session-config> <session-timeout>30</session-timeout> </session-config> ... @GLASSFISH@ ...
I skipped a lot of lines there, but what I'm going to do in my Ant build script is replace that @GLASSFISH@
token with multiple lines of text during the Ant build process.
Given that file, here's a portion of my Ant build.xml
file that performed the replacement on that file when my build process was run:
<copy file="${resources.dir}/web.xml" toFile="${web-inf.dir}/web.xml" overwrite="true"> <filterchain> <replacetokens> <token key="GLASSFISH" value="${web_xml_glassfish}"/> </replacetokens> </filterchain> </copy>
This Ant command can be read as "When you copy the web.xml
file from the resources
directory to the web-inf
directory, replace the token GLASSFISH
with the value of the variable ${web_xml_glassfish}
as you perform the copy."
Now, in this case the variable is a multi-line string of text, but you can't just type multiple lines, you have to use a syntax like this:
web_xml_glassfish=${line.separator}LINE 1${line.separator}LINE 2${line.separator}LINE 3${line.separator}
With a few modifications, that's my line from my build properties file. That line.separator
part is very important to know; the replacement process doesn't work without it.