You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Norman Walsh <nd...@nwalsh.com> on 2003/03/20 14:42:37 UTC

Newbie ant question

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I've dug through the online docs, but perhaps I missed the salient points.
I'm converting some Makefiles into ant. I used to have the rule:

dm-example.xml.cdata: dm-example.xml
	cat $< | sed "s/&/\\&amp;/g" | sed "s/</\\&lt;/g" > $@

My first attempt to convert this to ant was simply:

  <target name="dm-example.xml.cdata">
    <echo message="Building CDATA version of dm-example.xml ..." />
    <copy file="dm-example.xml" tofile="dm-example.xml.cdata" overwrite="yes"/>
    <replace file="dm-example.xml.cdata" token="&amp;" value="&amp;amp;"/>
    <replace file="dm-example.xml.cdata" token="&lt;" value="&amp;lt;"/>
  </target>

But I discovered that this target always rebuilt the file, regardless
of whether or not the dm-example.xml file was actually newer than
dm-example.xml.cdata. I tried to use 'depends', but of course that
requires a target not a filename.

Eventually, I wound up with the following code:

  <target name="chk.dm-example.xml.cdata">
    <uptodate property="dm-example.xml.cdata.exists"
              srcfile="dm-example.xml"
              targetfile="dm-example.xml.cdata"/>
  </target>

  <target name="dm-example.xml.cdata"
          depends="chk.dm-example.xml.cdata"
          unless="dm-example.xml.cdata.exists">
    <echo message="Building CDATA version of dm-example.xml ..." />
    <copy file="dm-example.xml" tofile="dm-example.xml.cdata" overwrite="yes"/>
    <replace file="dm-example.xml.cdata" token="&amp;" value="&amp;amp;"/>
    <replace file="dm-example.xml.cdata" token="&lt;" value="&amp;lt;"/>
  </target>

Is this really the simplest idiom for this expression? By the time I
have a half-dozen or so of these, keeping track of them all is going
to be really tedious.

It seems like there should be a simpler idiom for "build this target if this
file (these files?) are newer than this file". 

                                        Be seeing you,
                                          norm

- -- 
Norman.Walsh@Sun.COM    | Kinship is healing; we are physicians to each
XML Standards Architect | other.--Oliver Sacks
Web Tech. and Standards |
Sun Microsystems, Inc.  | 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.7 <http://mailcrypt.sourceforge.net/>

iD8DBQE+ecVNOyltUcwYWjsRArVyAKCGBewgvt0qiKnmFkeujk0qpK8ONQCfQmqp
bNyImJQGse9gtoXYR40et24=
=mQgY
-----END PGP SIGNATURE-----

Re: Newbie ant question

Posted by peter reilly <pe...@corvil.com>.
Included is a simple stringreplace... (it only takes one param)
(Cut and paste police may cringe at the code....)
<copy file="dm-example.xml" tofile="dm-example.xml.cdata">
   <filterchain>
       <filterreader classname="my.custom.ant.StringReplace"
                         classpath="my.custom.path">
           <param name="&amp;" value="&amp;amp;"/>
       <filterreader>
       <filterreader classname="my.custom.ant.StringReplace"
                         classpath="my.custom.path">
           <param name="&lt;" value="&amp;lt;"/>
       <filterreader>
   </filterchain>
</copy>

one may also use <if/> from ant-contrib.
<if>
    <uptodate
             srcfile="dm-example.xml"
              targetfile="dm-example.xml.cdata"/>
    <else>
    <copy file="dm-example.xml" tofile="dm-example.xml.cdata" 
overwrite="yes"/>
    <replace file="dm-example.xml.cdata" token="&amp;" value="&amp;amp;"/>
    <replace file="dm-example.xml.cdata" token="&lt;" value="&amp;lt;"/>
</if>

included On Thursday 20 March 2003 14:02, Stefan Bodewig wrote:
> On Thu, 20 Mar 2003, Norman Walsh <nd...@nwalsh.com> wrote:
> > Yes, I understand that, I'm just wondering if there's some simpler
> > ant idiom for expressing the conditionality of a target.
>
> In general tasks are supposed to do the dependency checking.  <copy>
> does so and it works for it.  <replace> will not touch your file if
> there was nothing to replace - unfortunately there will always be
> something to replace in your case.
>
> The "simpler idiom" in this case probably would be a filterreader that
> performed the replacements you need while copying the files.  This
> would give you several benefits as you'd get the dependency tracking
> working and the files wouldn't have to be openend, read, written and
> closed three times.
>
> Unfortunately there isn't such a replace-filter built-in into Ant
> (yet), but several people have asked for it, both a simple and a
> regular expression version.
>
> Stefan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org

Re: Newbie ant question

Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 20 Mar 2003, Norman Walsh <nd...@nwalsh.com> wrote:

> Yes, I understand that, I'm just wondering if there's some simpler
> ant idiom for expressing the conditionality of a target.

In general tasks are supposed to do the dependency checking.  <copy>
does so and it works for it.  <replace> will not touch your file if
there was nothing to replace - unfortunately there will always be
something to replace in your case.

The "simpler idiom" in this case probably would be a filterreader that
performed the replacements you need while copying the files.  This
would give you several benefits as you'd get the dependency tracking
working and the files wouldn't have to be openend, read, written and
closed three times.

Unfortunately there isn't such a replace-filter built-in into Ant
(yet), but several people have asked for it, both a simple and a
regular expression version.

Stefan

Re: Newbie ant question

Posted by Norman Walsh <nd...@nwalsh.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

/ Mike McNally <m5...@works.com> was heard to say:
| The "overwrite" flag, according to the ant manual, instructs the <copy>
| task to "overwrite existing files even if the destination files are
| newer".

Yes, without that flag the following replacements occurred every time
which wasn't useful :-)

| Also, note that target names in ant are simply the names of the targets;
| there are no external semantics.  In "make", terget names are implicitly
| file names unless you mark them otherwise.

Yes, I understand that, I'm just wondering if there's some simpler ant
idiom for expressing the conditionality of a target.

                                        Be seeing you,
                                          norm

- -- 
Norman.Walsh@Sun.COM    | To enjoy yourself and make others enjoy
XML Standards Architect | themselves, without harming yourself or any
Web Tech. and Standards | other; that, to my mind, is the whole of
Sun Microsystems, Inc.  | ethics.--Chamfort
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.7 <http://mailcrypt.sourceforge.net/>

iD8DBQE+eccyOyltUcwYWjsRAii2AKCg8e88WK0lV31cfBRWBw70suKThgCeMLvS
Pj4OwEfiZ0h9eK7t3j79R7M=
=R15k
-----END PGP SIGNATURE-----

Re: Newbie ant question

Posted by Mike McNally <m5...@works.com>.
The "overwrite" flag, according to the ant manual, instructs the <copy>
task to "overwrite existing files even if the destination files are
newer".

Also, note that target names in ant are simply the names of the targets;
there are no external semantics.  In "make", terget names are implicitly
file names unless you mark them otherwise.

--
[ you are my main foo ] Mike McNally -- m5@works.com

Re: Newbie ant question

Posted by Mike McNally <m5...@works.com>.
> No, this target
> 
>   <target name="dm-example.xml.cdata">
>     <echo message="Building CDATA version of dm-example.xml ..." />
>     <copy file="dm-example.xml" tofile="dm-example.xml.cdata"/>
>     <replace file="dm-example.xml.cdata" token="&amp;" value="&amp;amp;"/>
>     <replace file="dm-example.xml.cdata" token="&lt;" value="&amp;lt;"/>
>   </target>
> 
> still gets executed every time. 

It's the <copy> task that does the work of dependency checking, so yes,
the target will allways be "executed".

> But the replacements occur over and
> over again. 

Use <uptodate> to set up a relationship between the source of the
copy and the result, and then you can wrap your <replace> tasks
inside another target whose evaluation is predicated on the property
set by your <depends>


--
[ you are my main foo ] Mike McNally -- m5@works.com

Re: Newbie ant question

Posted by Norman Walsh <nd...@nwalsh.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

/ Stefan Bodewig <bo...@apache.org> was heard to say:
| The overwrite="true" is the problem as it disables dependency
| checking.  Set it to false (or somply omit it) and things should work
| as expected.

No, this target

  <target name="dm-example.xml.cdata">
    <echo message="Building CDATA version of dm-example.xml ..." />
    <copy file="dm-example.xml" tofile="dm-example.xml.cdata"/>
    <replace file="dm-example.xml.cdata" token="&amp;" value="&amp;amp;"/>
    <replace file="dm-example.xml.cdata" token="&lt;" value="&amp;lt;"/>
  </target>

still gets executed every time. But the replacements occur over and
over again. The trick is finding some simple way to tell ant that the
target dm-example.xml.cdata should be run if and only if
dm-example.xml is newer than dm-example.xml.cdata.

(I'm aware that the target name is probably irrelevant, it's simply
getting the dependency with a simpler idiom than I gave before that's
stumping me.)

                                        Be seeing you,
                                          norm

- -- 
Norman.Walsh@Sun.COM    | It is not impossibilities which fill us with
XML Standards Architect | the deepest despair, but possibilities which
Web Tech. and Standards | we have failed to realize.--Robert Mallet
Sun Microsystems, Inc.  | 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.7 <http://mailcrypt.sourceforge.net/>

iD8DBQE+ecivOyltUcwYWjsRAp6KAKCTCwkCLgeYtPJUhy1S9lCPqdj02wCfWvPZ
8imbK9BEi9FESQuIelzfXPg=
=Wuru
-----END PGP SIGNATURE-----

Re: Newbie ant question

Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 20 Mar 2003, Norman Walsh <nd...@nwalsh.com> wrote:

>     <copy file="dm-example.xml" tofile="dm-example.xml.cdata"
>           overwrite="yes"/>
> 
> But I discovered that this target always rebuilt the file,
> regardless of whether or not the dm-example.xml file was actually
> newer than dm-example.xml.cdata.

The overwrite="true" is the problem as it disables dependency
checking.  Set it to false (or somply omit it) and things should work
as expected.

Stefan