Use pure Ant to implement if else condition (check command line input)

有些话、适合烂在心里 提交于 2019-12-17 23:47:57

问题


I am a newbie in Ant. My ant script receives a user input variable named "env" from command line:

e.g. ant doIt -Denv=test

The user input value could be "test","dev" or "prod".

I also have the "doIt" target:

<target name="doIt">
  //What to do here?
</target>

In my target, I would like to create the following if else condition for my ant script:

if(env == "test")
  echo "test"
else if(env == "prod")
  echo "prod"
else if(env == "dev")
  echo "dev"
else
  echo "You have to input env"

That's to check which value user has inputted from command line, then, print a message accordingly.

I know with ant-Contrib, I can write ant script with <if> <else> . But for my project, I would like to use pure Ant to implement if else condition. Probably, I should use <condition> ?? But I am not sure how to use <condition> for my logic. Could some one help me please?


回答1:


You can create few targets and use if/unless tags.

<project name="if.test" default="doIt">

    <target name="doIt" depends="-doIt.init, -test, -prod, -dev, -else"></target>

    <target name="-doIt.init">
        <condition property="do.test">
            <equals arg1="${env}" arg2="test" />
        </condition>
        <condition property="do.prod">
            <equals arg1="${env}" arg2="prod" />
        </condition>
        <condition property="do.dev">
            <equals arg1="${env}" arg2="dev" />
        </condition>
        <condition property="do.else">
            <not>
                <or>
                <equals arg1="${env}" arg2="test" />
                <equals arg1="${env}" arg2="prod" />
                <equals arg1="${env}" arg2="dev" />
                </or>
            </not>
        </condition>
    </target>

    <target name="-test" if="do.test">
        <echo>this target will be called only when property $${do.test} is set</echo>
    </target>

    <target name="-prod" if="do.prod">
        <echo>this target will be called only when property $${do.prod} is set</echo>
    </target>

    <target name="-dev" if="do.dev">
        <echo>this target will be called only when property $${do.dev} is set</echo>
    </target>

    <target name="-else" if="do.else">
        <echo>this target will be called only when property $${env} does not equal test/prod/dev</echo>
    </target>

</project>

Targets with - prefix are private so user won't be able to run them from command line.




回答2:


If any of you require a plain if/else condition (without elseif); then use the below:

Here I am dependent on a env variable DMAPM_BUILD_VER, but it may happen that this variable is not set in the env. So I need to have mechanism to default to a local value.

    <!-- Read build.version value from env variable DMAPM_BUILD_VER. If it is not set, take default.build.version. -->
    <property name="default.build.version" value="0.1.0.0" />
    <condition property="build.version" value="${env.DMAPM_BUILD_VER}" else="${default.build.version}">
        <isset property="env.DMAPM_BUILD_VER"/>
    </condition>


来源:https://stackoverflow.com/questions/15389544/use-pure-ant-to-implement-if-else-condition-check-command-line-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!