You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Prashant Reddy <pr...@pramati.com> on 2008/02/05 13:06:07 UTC

Fail build when 'replace' task cannot find expected number of tokens

Hello ANT users,

Using ANT 1.6.5, a build XML i am using makes use of <replace> task to
replace certain token in a bunch of Javascript files.

I would like the build to fail when the replace of a token fails, so
that when someone changes the source file such that <replace> does not
find any tokens to replace; the incident is caught straight away.

Right now replace only supports printing of summary containing number of
tokens that were replaced, but this is not good enough for me.

Humm...Sounds like i should look up if ant-unit can help me out.

I would appreciate any suggestions. (Apart from using resourcecount
which is in ANT 1.7)

Thanks for your time.
-Prashant


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: AW: Fail build when 'replace' task cannot find expected number oftokens

Posted by Prashant Reddy <pr...@pramati.com>.
Ok my bad. replace does change the timestamp if the token is replaced.

I was creating the source.txt just before the replace, which also gave
temp.txt same timestamp.

<?xml version="1.0" encoding="UTF-8"?>
<project name="test-replace" default="test">
    <target name="test">
        <!--<echo file="source.txt" message="token"/>-->
        <copy file="source.txt" tofile="temp.txt"
preservelastmodified="true"/>
        <replace file="temp.txt" token="token" value="replaced"/>
        <condition property="not.replaced">
            <not>
                <uptodate srcfile="source.txt"
targetfile="temp.txt"></uptodate>
            </not>
        </condition>
        <echo message=" not.replaced : ${not.replaced}"/>
        <fail if="not.replaced" message="Not replaced"/>
        <delete file="temp.txt"/>
    </target>
</project>


On Tue, 2008-02-05 at 18:24 +0530, Prashant Reddy wrote:
> Thanks for a good idea. nice and simple.
> 
> Quick check with following build.xml reveals that the replace does not
> modify timestamp even when token is replaced. However this approach will
> not catch scenario where 1 instance was replaced when 2 were expected. 
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <project name="test-replace" default="test">
>     <target name="test">
> 	<echo file="source.txt" message="token"/>
>         <copy file="source.txt" tofile="temp.txt"
> preservelastmodified="true"/>
>         <replace file="temp.txt" token="token" value="replaced"/>
>         <condition property="not.replaced">
>             <not>
>                 <uptodate srcfile="source.txt"
> targetfile="temp.txt"></uptodate>
>             </not>
>         </condition>
>         <fail if="not.replaced" message="Not replaced"/>
>     </target>
> </project>
> 
> $ ls -lt
> total 12
> -rw-r--r-- 1 prashant prashant   5 2008-02-05 18:22 source.txt
> -rw-r--r-- 1 prashant prashant   8 2008-02-05 18:22 temp.txt
> -rw-r--r-- 1 prashant prashant 569 2008-02-05 18:22 build.xml
> 
> -Prashant
> 
> On Tue, 2008-02-05 at 13:27 +0100, Knuplesch, Juergen wrote:
> > Maybe you could copy the file without changing of the timestamp (attribute preservelastmodified).
> > Then do the replace on your file.
> > Then test via <uptodate> if a change happened.
> > Delete the copy
> > 
> > This will only work, if replace does not change the timestamp, in case of no changes.
> > 
> > Just an idea...
> > 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: AW: Fail build when 'replace' task cannot find expected number oftokens

Posted by Prashant Reddy <pr...@pramati.com>.
Thanks for a good idea. nice and simple.

Quick check with following build.xml reveals that the replace does not
modify timestamp even when token is replaced. However this approach will
not catch scenario where 1 instance was replaced when 2 were expected. 

<?xml version="1.0" encoding="UTF-8"?>
<project name="test-replace" default="test">
    <target name="test">
	<echo file="source.txt" message="token"/>
        <copy file="source.txt" tofile="temp.txt"
preservelastmodified="true"/>
        <replace file="temp.txt" token="token" value="replaced"/>
        <condition property="not.replaced">
            <not>
                <uptodate srcfile="source.txt"
targetfile="temp.txt"></uptodate>
            </not>
        </condition>
        <fail if="not.replaced" message="Not replaced"/>
    </target>
</project>

$ ls -lt
total 12
-rw-r--r-- 1 prashant prashant   5 2008-02-05 18:22 source.txt
-rw-r--r-- 1 prashant prashant   8 2008-02-05 18:22 temp.txt
-rw-r--r-- 1 prashant prashant 569 2008-02-05 18:22 build.xml

-Prashant

On Tue, 2008-02-05 at 13:27 +0100, Knuplesch, Juergen wrote:
> Maybe you could copy the file without changing of the timestamp (attribute preservelastmodified).
> Then do the replace on your file.
> Then test via <uptodate> if a change happened.
> Delete the copy
> 
> This will only work, if replace does not change the timestamp, in case of no changes.
> 
> Just an idea...
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


AW: Fail build when 'replace' task cannot find expected number oftokens

Posted by "Knuplesch, Juergen" <Ju...@icongmbh.de>.
Maybe you could copy the file without changing of the timestamp (attribute preservelastmodified).
Then do the replace on your file.
Then test via <uptodate> if a change happened.
Delete the copy

This will only work, if replace does not change the timestamp, in case of no changes.

Just an idea...

-- 
Jürgen Knuplesch                    www.icongmbh.de
icon Systemhaus GmbH                Tel. +49 711 806098-275
Sophienstraße 40                    
D-70178 Stuttgart                   Fax. +49 711 806098-299

Geschäftsführer: Uwe Seltmann
HRB Stuttgart 17655
USt-IdNr.: DE 811944121 
-----Ursprüngliche Nachricht-----
Von: Prashant Reddy [mailto:prashant@pramati.com] 
Gesendet: Dienstag, 5. Februar 2008 13:06
An: Ant Users List
Betreff: Fail build when 'replace' task cannot find expected number oftokens

Hello ANT users,

Using ANT 1.6.5, a build XML i am using makes use of <replace> task to replace certain token in a bunch of Javascript files.

I would like the build to fail when the replace of a token fails, so that when someone changes the source file such that <replace> does not find any tokens to replace; the incident is caught straight away.

Right now replace only supports printing of summary containing number of tokens that were replaced, but this is not good enough for me.

Humm...Sounds like i should look up if ant-unit can help me out.

I would appreciate any suggestions. (Apart from using resourcecount which is in ANT 1.7)

Thanks for your time.
-Prashant


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Fail build when 'replace' task cannot find expected number of tokens

Posted by Prashant Reddy <pr...@pramati.com>.
I solved this problem like this, on advice of another colleague.

 * <record> the output of the <replace> tasks
 * Look for occurrences of the string "Replaced 0 occurrences" in the
recorded output
 * If above said pattern occurs fail the build.

Of course this approach still does not cover usecase when you are
expecting to find 3 replacements but only found 2.

I guess modifying <replace> task itself, (since it obviously knows the
number of occurrences) to take expected number of occurrences would have
been a clean soln.

-Prashant

On Tue, 2008-02-05 at 17:36 +0530, Prashant Reddy wrote:
> Hello ANT users,
> 
> Using ANT 1.6.5, a build XML i am using makes use of <replace> task to
> replace certain token in a bunch of Javascript files.
> 
> I would like the build to fail when the replace of a token fails, so
> that when someone changes the source file such that <replace> does not
> find any tokens to replace; the incident is caught straight away.
> 
> Right now replace only supports printing of summary containing number of
> tokens that were replaced, but this is not good enough for me.
> 
> Humm...Sounds like i should look up if ant-unit can help me out.
> 
> I would appreciate any suggestions. (Apart from using resourcecount
> which is in ANT 1.7)
> 
> Thanks for your time.
> -Prashant
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


AW: Fail build when 'replace' task cannot find expected number oftokens

Posted by "Knuplesch, Juergen" <Ju...@icongmbh.de>.
Addition:
You could also use the <condition> Task togeteher with <filesmatch>

And of course use the fail-Task to make the Build fail 


-- 
Jürgen Knuplesch                    www.icongmbh.de
icon Systemhaus GmbH                Tel. +49 711 806098-275
Sophienstraße 40                    
D-70178 Stuttgart                   Fax. +49 711 806098-299

Geschäftsführer: Uwe Seltmann
HRB Stuttgart 17655
USt-IdNr.: DE 811944121 
-----Ursprüngliche Nachricht-----
Von: Prashant Reddy [mailto:prashant@pramati.com] 
Gesendet: Dienstag, 5. Februar 2008 13:06
An: Ant Users List
Betreff: Fail build when 'replace' task cannot find expected number oftokens

Hello ANT users,

Using ANT 1.6.5, a build XML i am using makes use of <replace> task to replace certain token in a bunch of Javascript files.

I would like the build to fail when the replace of a token fails, so that when someone changes the source file such that <replace> does not find any tokens to replace; the incident is caught straight away.

Right now replace only supports printing of summary containing number of tokens that were replaced, but this is not good enough for me.

Humm...Sounds like i should look up if ant-unit can help me out.

I would appreciate any suggestions. (Apart from using resourcecount which is in ANT 1.7)

Thanks for your time.
-Prashant


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org