You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Diane Holt <ho...@yahoo.com> on 2000/09/17 22:52:18 UTC

Using "if" in a target?

Am I using "if" correctly in this target:
  <target name="nsync" if="p4client">
    <exec executable="p4" dir="${workspace}">
      <arg value="sync -n ${spec}"/>
    </exec>
  </target>

I have another target that depends on nsync, which I assumed would mean
"If nsync doesn't get run (or if it runs but fails), don't run this one"
-- and I assumed if the property in the "if" in nsync wasn't set, then
nsync wouldn't get run. The target that depends on nsync is just a little
stubbed-off echo (for now):
  <target name="build" depends="nsync">
    <echo message="Running build target..."/>
  </target>

I explicitly unset the env var that the p4client property is passed in the
ant wrapper-script (ie., "$JAVACMD ... -Dp4client=$P4CLIENT ...") so that
the property wouldn't have a value. The output from the check_properties
target shows that p4client isn't set, as does the fact and type of the
error from the command that nsync went ahead and executed anyway:

[binky] ant check_properties build
Buildfile: build.xml
check_properties:
  p4client is:
nsync:
     [exec] Client 'binky' unknown - use 'client' command to create it.
     [exec] Result: 1
build:
  Running build target...

BUILD SUCCESSFUL
Total time: 2 seconds

Note that not only did nsync get run, even though the p4client property
isn't set, the <exec> in the nsync target resulted in 1, and still the
build target got run.

So am I just doing this all wrong -- or is ant not behaving as it should?
(I'm hoping it's the former -- then I can just correct whatever it is I'm
doing, and get past this.)

I'm running the nightly from 9/16. (But it behaves the same with the
nightly back from 9/9.)

Thanks,
Diane


=====
(holtdl@yahoo.com)



__________________________________________________
Do You Yahoo!?
Yahoo! Mail - Free email you can access from anywhere!
http://mail.yahoo.com/

Re: Using "if" in a target?

Posted by Nico Seessle <ni...@seessle.de>.
----- Original Message -----
From: "Diane Holt" <ho...@yahoo.com>
To: <an...@jakarta.apache.org>
Sent: Sunday, September 17, 2000 10:52 PM
Subject: Using "if" in a target?


> Am I using "if" correctly in this target:
>   <target name="nsync" if="p4client">
>     <exec executable="p4" dir="${workspace}">
>       <arg value="sync -n ${spec}"/>
>     </exec>
>   </target>

Yes.

> I have another target that depends on nsync, which I assumed would mean
> "If nsync doesn't get run (or if it runs but fails), don't run this one"

I'm not totally sure, but I don't think so (for assumption 1.). Each target
checks it's own conditions. I have not seen any source which checks if a
target which the current one depends on executed.

If the target "fails" the build will be completely stopped. Just depends on
your definition of failure :-)

> -- and I assumed if the property in the "if" in nsync wasn't set, then
> nsync wouldn't get run. The target that depends on nsync is just a little

Correct.

> stubbed-off echo (for now):
>   <target name="build" depends="nsync">
>     <echo message="Running build target..."/>
>   </target>
>
> I explicitly unset the env var that the p4client property is passed in the
> ant wrapper-script (ie., "$JAVACMD ... -Dp4client=$P4CLIENT ...") so that
> the property wouldn't have a value. The output from the check_properties
> target shows that p4client isn't set, as does the fact and type of the
> error from the command that nsync went ahead and executed anyway:

And p4client is set to "" (You can verify this in a simple way, just specify
a property you are sure you have never used before as for example
"donaudampfschiffahrtskapitaen" (german :-) ). You will see that ant just
echoes the property. In your case there is nothing which means the property
exists, but it is empty (which in turn is enough to get a target run)).

> [binky] ant check_properties build
> Buildfile: build.xml
> check_properties:
>   p4client is:
> nsync:
>      [exec] Client 'binky' unknown - use 'client' command to create it.
>      [exec] Result: 1
> build:
>   Running build target...
>

You can specify failonerror="yes" to the exec-task and your build will fail
completely.

> I'm running the nightly from 9/16. (But it behaves the same with the
> nightly back from 9/9.)

It will behave the same with the current version in CVS.

Nico



Re: Using "if" in a target?

Posted by Stefan Bodewig <bo...@bost.de>.
Just want to add a little rationale to one thing. Other than that
there is nothing to add to Nico's post.

>>>>> "DH" == Diane Holt <ho...@yahoo.com> writes:

 DH> I have another target that depends on nsync, which I assumed
 DH> would mean "If nsync doesn't get run (or if it runs but fails),
 DH> don't run this one"

No, each target checks its own properties and nothing else. This is
very usefull in cases like this:

<available property="jdk1.2+" classname="java.lang.ThreadLocal" />

<target name="main" depends="jdk1.1main,jdk1.2+main" />

<target name="jdk1.1main" unless="jdk1.2+" ...
<target name="jdk1.2main" if="jdk1.2+" ...

where jdk1.1main and jdk1.2main do the same, just a little bit
different. If you run target main, you can be sure whatever you wanted
to do happened and the "correct" way has been used.

Stefan