You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Fenlason, Josh" <jf...@ptc.com> on 2006/12/14 17:43:32 UTC
Replace Question
I want to add a line to a file, but only if the file doesn't already
contain that line. I can add the line fine, but I can't figure out how
to do it conditionally. Is this possible? Here's what I have so far.
Any suggestions would be greatly appreciated. Thanks in advance.
<target name="test">
<!-- this replace works, but it adds the line every time -->
<replace dir="bin" file="bin/start.bat">
<replacetoken><![CDATA[start]]></replacetoken>
<replacevalue><![CDATA[set bar=foo
start]]></replacevalue>
</replace>
<!-- this doesn't replace anything no matter if start.bat contains
set bar=foo or not -->
<replace dir="bin" includes="bin/start.bat">
<not><contains text="set bar=foo" casesensitive="yes"/></not>
<replacetoken><![CDATA[start]]></replacetoken>
<replacevalue><![CDATA[set bar=foo
start]]></replacevalue>
</replace>
</target>
,
Josh.
Re: Replace Question
Posted by Andrew Goktepe <an...@gmail.com>.
If you are running Ant 1.6.3 or later, you can use the <isfileselected>
condition:
<condition property="skip.replacement">
<isfileselected file="bin/start.bat">
<contains text="set bar=foostart" />
</isfileselected>
</condition>
The skip.replacement property will be set if the file already contains the
replacement text. You can then run <replace> conditionally on this property.
You could also use ant-contrib with <if> to combine the check with the
execution:
<if>
<isfileselected file="bin/start.bat">
<contains text="set bar=foostart" />
</isfileselected>
<then/>
<else>
<replace dir="bin" file="bin/start.bat">
<replacetoken><![CDATA[start]]></replacetoken>
<replacevalue><![CDATA[set bar=foostart]]></replacevalue>
</replace>
</else>
</if>
-Andrew
On 12/14/06, Fenlason, Josh <jf...@ptc.com> wrote:
>
> I want to add a line to a file, but only if the file doesn't already
> contain that line. I can add the line fine, but I can't figure out how
> to do it conditionally. Is this possible? Here's what I have so far.
> Any suggestions would be greatly appreciated. Thanks in advance.
> <target name="test">
> <!-- this replace works, but it adds the line every time -->
> <replace dir="bin" file="bin/start.bat">
> <replacetoken><![CDATA[start]]></replacetoken>
> <replacevalue><![CDATA[set bar=foo
> start]]></replacevalue>
> </replace>
>
> <!-- this doesn't replace anything no matter if start.bat contains
> set bar=foo or not -->
> <replace dir="bin" includes="bin/start.bat">
> <not><contains text="set bar=foo" casesensitive="yes"/></not>
> <replacetoken><![CDATA[start]]></replacetoken>
> <replacevalue><![CDATA[set bar=foo
> start]]></replacevalue>
> </replace>
>
> </target>
> ,
> Josh.
>
>