You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Francis GALIEGUE <fg...@one2team.com> on 2009/08/31 10:53:56 UTC

s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Hello everyone,

I have noticed something strange in the if="" and unless="" attributes
of targets.

Let's say that I have:

<target name="t1"/>

<target name="t2" unless="p2"/>

<target name="t" unless="p" depends="t1, t2"/>

If I:

ant -Dp2=1

then, as expected, t1 triggers via t but not t2.

However, if I:

ant -Dp=1

I'd have expected none of t1 and t2 to execute, but both are executed!

Which means that the dependencies of a target are executed before even
any if="" or unless="" attributes are taken into account... This is
rather counterintuitive to my eyes. Is this really the expected
behaviour or is this a bug?


-- 

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

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


Re: s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Posted by David Weintraub <qa...@gmail.com>.
On Tue, Sep 1, 2009 at 3:29 AM, Francis GALIEGUE <fg...@one2team.com> wrote:

> OK, this is a clear enough explanation. I'm not really sure about the
> "programming language vs build system" analogy, though ;)
>
> I think I still don't have "the ant way" of doing things even though
> I've been using it for several years now...
>
> Sorry for the waste of time,
>

No waste of time. It take a while to get use to the difference between a
build system  vs. a program paradigm.

A build system is hard to comprehend because we are so use to looking at the
problem as a programmer. We know what we want done in what order and our
instinct is to tell Ant exactly how to do this. The "if" and "unless"
parameters of the <target> seem to enforce this mode of thinking.

Here's a small example on the difference between looking at Ant as a program
language vs. a build system. I need to build and deploy a jar file. My first
two targets will look like this:

<target name="compile">
    <blah, blah, blah/>
</target>

<target name="jar" depends="compile">
    <blah, blah, blah/>
</target>

The question is what should my deploy target look like?


   1. <target name="deploy" depends="*jar*">
       <blah, blah, blah/>
   </target>
   2. <target name="deploy" depends="*compile,jar*">
       <blah, blah, blah/>
   </target>

The difference is in their dependency listing. The developer in us wants to
do "1" (and even make sure we list "compile" before "jar" in the depends
list too!). Simply stating that we need to deploy the jar makes our little
developer brains think of all the steps from "A" to "Z" that are needed.

However, the correct answer is really "2". Deployment doesn't care about the
steps to build the jar. It only cares that there is a jarfile. In this
simple case, it really doesn't matter because the resulting build process
will be exactly the same. However, as the build gets more and more complex,
trying to determine the exact build order gets more and more difficult.

Take a look at your build process as independent tasks, and some of those
tasks depend upon the output of other tasks. Try not to think of all the
various steps you need to get to a particular task. Just the task and what
that task needs in order to perform that task.

Previously you had three targets:

<target name="converage-report">
<target name="converage-report-gen">
<target name="converage-report-mail">

I take it that you also have some sort of target called "build" or something
similar that depends upon the coverage report:

<target name="build" depends="compile,jar,coverage-report">

However, this is actually not true! Your build isn't dependent upon the
coverage report! That's why you want the option to skip it. What you should
do is remove the dependency on coverage-report from your build target. If
you want a coverage report, you simply call both targets:

$ ant coverage-report build

Don't like the idea of calling two targets? Define a faux target that
depends upon the build and the coverage report.

<target name="build-with-report"
    depends="build,coverage-report">

Now, to build with the coverage report, I simply specify this new target:

$ ant build-with-report

An advantage is that you can use the "description" parameter of the <target>
task to specify what that target does:

<target name="build-with-report"
    depends="build,coverage-report"
    description="Builds system and produces coverage report">

Now your developers can use "*ant -projecthelp*" to see the various build
options:

$ ant -projecthelp
Buildfile: build.xml

Main Targets:

   build:  Builds the system
   clean: Cleans out all built artifacts
   build-with-report: Builds system and produces coverage report
Default target: build-with-report

This way, you're actually documenting the way to build the project without
producing the coverage report inside the build.xml file itself.

-- 
David Weintraub
qazwart@gmail.com

Re: s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Posted by Francis GALIEGUE <fg...@one2team.com>.
On Mon, Aug 31, 2009 at 17:17, David Weintraub<qa...@gmail.com> wrote:
> Ant is not a programming language! Ant is not a programming language! Repeat
> that.
>
> Ant build system. And yes, there is a major difference. In a programming
> language, you set the exact order you want everything executed in. In a
> build system, the build system builds a dependency matrix and then decides
> on the order tp execute the various tasks in.
>
> When you execute Ant, the very first thing it does is look for all the
> targets in your build.xml file, and then builds a dependency matrix based
> upon each target's "depends" statement. It doesn't even look at the contents
> of the target or  the "if" and "unless" statements.
>
> Your issue is that you're treating the "if" and "unless" statements as if it
> was part of a programming language. Normally, the "if" and "unless"
> statements are used to prevent a target from executing because the
> conditions for that target aren't met, and this condition is normally set in
> a previous target. I do this type of stuff all the time:
>
> <target name="mytarget.test">
>     <condition property="mytarget.is.setup">
>         <blah, blah, blah>
>     </condition>
> </target>
>
> <target name="mytarget" depends="mytarget.test"
>     if="mytarget.is.setup">
>     <blah, blah, blah/>
> </target>
>
> In the above, I want to execute "mytarget" but I want to make sure
> everything is actually setup to execute this target. So, I have a
> "mytarget.test" target I depend upon. This sets a property letting me know
> if everything is all setup or not.
>
> I hope this explains why Ant is doing what it is doing and why your
> build.xml isn't quite doing what you expect.. It looks like (and what you
> did) was add the "nocoverage" if check to the other target. That's one way
> to handle this issue.
>

OK, this is a clear enough explanation. I'm not really sure about the
"programming language vs build system" analogy, though ;)

I think I still don't have "the ant way" of doing things even though
I've been using it for several years now...

Sorry for the waste of time,
-- 

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

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


Re: s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Posted by David Weintraub <qa...@gmail.com>.
Ant is not a programming language! Ant is not a programming language! Repeat
that.

Ant build system. And yes, there is a major difference. In a programming
language, you set the exact order you want everything executed in. In a
build system, the build system builds a dependency matrix and then decides
on the order tp execute the various tasks in.

When you execute Ant, the very first thing it does is look for all the
targets in your build.xml file, and then builds a dependency matrix based
upon each target's "depends" statement. It doesn't even look at the contents
of the target or  the "if" and "unless" statements.

Your issue is that you're treating the "if" and "unless" statements as if it
was part of a programming language. Normally, the "if" and "unless"
statements are used to prevent a target from executing because the
conditions for that target aren't met, and this condition is normally set in
a previous target. I do this type of stuff all the time:

<target name="mytarget.test">
     <condition property="mytarget.is.setup">
         <blah, blah, blah>
     </condition>
</target>

<target name="mytarget" depends="mytarget.test"
     if="mytarget.is.setup">
     <blah, blah, blah/>
</target>

In the above, I want to execute "mytarget" but I want to make sure
everything is actually setup to execute this target. So, I have a
"mytarget.test" target I depend upon. This sets a property letting me know
if everything is all setup or not.

I hope this explains why Ant is doing what it is doing and why your
build.xml isn't quite doing what you expect.. It looks like (and what you
did) was add the "nocoverage" if check to the other target. That's one way
to handle this issue.

On Mon, Aug 31, 2009 at 4:53 AM, Francis GALIEGUE<fg...@one2team.com> wrote:
> Hello everyone,
>
> I have noticed something strange in the if="" and unless="" attributes
> of targets.
>
> Let's say that I have:
>
> <target name="t1"/>
>
> <target name="t2" unless="p2"/>
>
> <target name="t" unless="p" depends="t1, t2"/>
>
> If I:
>
> ant -Dp2=1
>
> then, as expected, t1 triggers via t but not t2.
>
> However, if I:
>
> ant -Dp=1
>
> I'd have expected none of t1 and t2 to execute, but both are executed!
>
> Which means that the dependencies of a target are executed before even
> any if="" or unless="" attributes are taken into account... This is
> rather counterintuitive to my eyes. Is this really the expected
> behaviour or is this a bug?
>
>
> --
>
> Francis Galiegue
> ONE2TEAM
> Ingénieur système
> Mob : +33 (0) 683 877 875
> Tel : +33 (0) 178 945 552
> fge@one2team.com
> 40 avenue Raymond Poincaré
> 75116 Paris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>



-- 
David Weintraub
qazwart@gmail.com

Re: s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Posted by Francis GALIEGUE <fg...@one2team.com>.
On Mon, Aug 31, 2009 at 13:19, Rebhan,
Gilbert<Gi...@huk-coburg.de> wrote:
[...]
>
> f.e. =
>
> <target name="check_state">
>  <condition property="${mname}.done">
>    <available file="${ant.working.dir}/${mname}_script.params"/>
>  </condition>
> </target>
>
> <target name="build" depends="check_state" unless="${mname}.done">
> ...
>
>

Yes, but that's one usage scenario. My scenario is not this at all.

To be precise, I'd have liked to write:

<target name="coverage-gen-report"/>

<target name="coverage-mail-report" unless="nomail"/>

<target name="coverage-report" unless="nocoverage"
depends="coverage-gen-report, coverage-mail-report"/>

This way, if nomail was set, the coverage report would have been
generated but no mail would have been sent. And if nocoverage was
sent, nothing would have been done at all, since I believed that
nocoverage being sent meant that coverage-report wouldn't be executed
at all. But no.

What I had to write to accomplish what I wanted is:

<target name="coverage-gen-report" unless="nocoverage"/>

<target name="coverage-mail-report" depends="coverage-gen-report"
unless="nomail">
    <if>
        <not>
            <isset property="nocoverage"/>
        </not>
        <then>
            <!-- do the stuff -->
        </then>
    </if>
</target>

<target name="coverage-report" depends="coverage-gen-report,
coverage-mail-report"/>

Which is a lot more confusing. And I don't want to write an extra task
to check for the existence of the file I have to send via mail.

-- 

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

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


RE: s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 


-----Original Message-----
From: Gilles Scokart [mailto:gscokart@gmail.com] 
Sent: Monday, August 31, 2009 1:00 PM
To: Ant Users List
Subject: Re: <target>s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

/*
2009/8/31 Francis GALIEGUE <fg...@one2team.com>

> Which means that the dependencies of a target are executed before even
> any if="" or unless="" attributes are taken into account... This is
> rather counterintuitive to my eyes. Is this really the expected
> behaviour or is this a bug?


This is indeed as designed, and it is pretty usefull because you can set the
property p in t1 or t2.  This pattern is quiet often used.

Gilles Scokart
*/

f.e. =

<target name="check_state">
  <condition property="${mname}.done">
    <available file="${ant.working.dir}/${mname}_script.params"/>
  </condition>
</target>

<target name="build" depends="check_state" unless="${mname}.done">
...


Regards, Gilbert

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


RE: s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.

-----Original Message-----
From: Francis GALIEGUE [mailto:fge@one2team.com] 
Sent: Monday, August 31, 2009 1:24 PM
To: Ant Users List
Subject: Re: <target>s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

/*

Maybe this pattern is often used, but as I said, it looks pretty much
counterintuitive to my eyes. If a property is dependent on the
(non)existence of a property and this property does/does not exist, I
don't expect this target to be executed at all.
*/

there was another thread recently dealing with depends/unless, see =
http://www.nabble.com/Target-unless-question-td24960662.html

btw. you're not the first one getting confused by that behaviour, see
google => ant+task+depends+unless
but it's working as designed, means  targets listed under depends are
always executed before if / unless. So dependent targets are able to check
certain things, f.e. the <condition> in my example
 
Regards, Gilbert
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Posted by Gilles Scokart <gs...@gmail.com>.
You can write

<target name="t1">
    <property name="p"/>  <!-- Usualy not directly, but conditionaly or as a
potential side effect of some tasks -->
</target>


<target name="t" depends="t1" unless="p">
...
</target>





Gilles Scokart


2009/8/31 Francis GALIEGUE <fg...@one2team.com>

> On Mon, Aug 31, 2009 at 12:59, Gilles Scokart<gs...@gmail.com> wrote:
> > This is indeed as designed, and it is pretty usefull because you can set
> the
> > property p in t1 or t2.  This pattern is quiet often used.
> >
> >
>
> Does this mean that you can:
>
> <target name="t1" unless="p, p1"/>
>
> or:
>
> <target name="t1" unless="p" unless="p1"/>
>
> ?
>
> Maybe this pattern is often used, but as I said, it looks pretty much
> counterintuitive to my eyes. If a property is dependent on the
> (non)existence of a property and this property does/does not exist, I
> don't expect this target to be executed at all.
> --
>
> Francis Galiegue
> ONE2TEAM
> Ingénieur système
> Mob : +33 (0) 683 877 875
> Tel : +33 (0) 178 945 552
> fge@one2team.com
> 40 avenue Raymond Poincaré
> 75116 Paris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>

Re: s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Posted by Francis GALIEGUE <fg...@one2team.com>.
On Mon, Aug 31, 2009 at 12:59, Gilles Scokart<gs...@gmail.com> wrote:
> This is indeed as designed, and it is pretty usefull because you can set the
> property p in t1 or t2.  This pattern is quiet often used.
>
>

Does this mean that you can:

<target name="t1" unless="p, p1"/>

or:

<target name="t1" unless="p" unless="p1"/>

?

Maybe this pattern is often used, but as I said, it looks pretty much
counterintuitive to my eyes. If a property is dependent on the
(non)existence of a property and this property does/does not exist, I
don't expect this target to be executed at all.
-- 

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

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


Re: s unless/if: strangeness in default behaviour (imho) (ant 1.6.5)

Posted by Gilles Scokart <gs...@gmail.com>.
This is indeed as designed, and it is pretty usefull because you can set the
property p in t1 or t2.  This pattern is quiet often used.


Gilles Scokart


2009/8/31 Francis GALIEGUE <fg...@one2team.com>

> Hello everyone,
>
> I have noticed something strange in the if="" and unless="" attributes
> of targets.
>
> Let's say that I have:
>
> <target name="t1"/>
>
> <target name="t2" unless="p2"/>
>
> <target name="t" unless="p" depends="t1, t2"/>
>
> If I:
>
> ant -Dp2=1
>
> then, as expected, t1 triggers via t but not t2.
>
> However, if I:
>
> ant -Dp=1
>
> I'd have expected none of t1 and t2 to execute, but both are executed!
>
> Which means that the dependencies of a target are executed before even
> any if="" or unless="" attributes are taken into account... This is
> rather counterintuitive to my eyes. Is this really the expected
> behaviour or is this a bug?
>
>
> --
>
> Francis Galiegue
> ONE2TEAM
> Ingénieur système
> Mob : +33 (0) 683 877 875
> Tel : +33 (0) 178 945 552
> fge@one2team.com
> 40 avenue Raymond Poincaré
> 75116 Paris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>