Nik
Nik

Reputation: 9431

Ant Regex Replace pattern with contents of file

I'm trying to figure out how to replace matching text in my files with contents of file. Actually I want to inline javascript sources into HTML. But loadfile doesn't work in replaceregexp.

<replaceregexp flags="gs">
    <regexp pattern="\&lt;!--js--\&gt;(.*?)\&lt;!--/js--\&gt;"/>
    <substitution expression='\&lt;script src="min.js"\&gt;\&lt;/script\&gt;'/>
    <loadfile property="min.js" srcFile="compiled/min.js"/>
</replaceregexp>

Upvotes: 1

Views: 4500

Answers (1)

Chad Nouis
Chad Nouis

Reputation: 7041

Here's an example of using <replaceregexp> to replace every <!--js-->***<!--/js--> with the contents of min.js.

<project name="ant-replaceregexp" default="run" basedir=".">
    <target name="run">
        <property name="js.pattern"
            ><![CDATA[[<]!--js--[>](.*?)[<]!--/js--[>]]]></property>

        <loadfile property="min.js" srcFile="compiled/min.js"/>

        <replaceregexp file="test.html" flags="gs">
            <regexp pattern="${js.pattern}"/>
            <substitution expression="${min.js}"/>
        </replaceregexp>
    </target>
</project>

Upvotes: 3

Related Questions