questborn
questborn

Reputation: 2855

ant if target failing

I have an ant task which reads environment property from myproject.properties. The environment property value is set to prod and am displaying that "Prod condition is true". I see that ${environment} variable is set to prod, but if condition is never true. Can someone explain why?

myproject.properties:

environment=prod

build.xml:

<project name="my-project" default="run" basedir=".">
  <property file="myproject.properties" />
  <target name="run">
  <echo message="running target run ${environment}"/>
    <if>
      <equals arg1="${environment}" arg2="prod">
        <then>
          <echo message="Prod condition is true"/>
           <!--do prod environment specific task-->
       </then> 
    </if>    
  </target>
</project>

Upvotes: 0

Views: 747

Answers (3)

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77951

The following solution uses core ANT. It avoids using the "if" task provided by the ant-contrib extension.

<project name="my-project" default="run" basedir=".">
    <property file="myproject.properties" />

    <condition property="prod.set">
        <equals arg1="${environment}" arg2="prod"/>
    </condition>

    <target name="run" if="prod.set">
        <echo message="Prod condition is true"/>
    </target>
</project>

Upvotes: 2

Satheesh Cheveri
Satheesh Cheveri

Reputation: 3679

Hopefully you would have had done this, but just to remind, did you include antcontrib jar and placed related taskdef in your project.xml ? if not, please correct the build file, and copy ant contrib jar in to the classpath

<project name="SampleWS" default="run" basedir=".">
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="lib/ant-contrib-0.6.jar" onerror="ignore"/>
 <property file="myproject.properties" />
 <target name="run">
 <echo message="running target run ${environment}"/>
    <if>
      <equals arg1="${environment}" arg2="prod"/>
        <then>
          <echo message="Prod condition is true"/>
           <!--do prod environment specific task-->
       </then> 
   </if>    
</target>
</project>

Upvotes: 0

Isaac
Isaac

Reputation: 16736

Other than the fact that your equals task is missing an end-tag (it should be a self-closing tag, actually), I'm willing to bet that you have a whitespace hiding somewhere. In your echo, surround the printout of the property with apostrophes or something:

<echo message="running target run '${environment}'"/>

And you might see a whitespace at the end of the value. That's the only reasonable explanation I can think of. Alternatively, try running with -Denvironment=prod and see what happens.

Upvotes: 2

Related Questions