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

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

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:

   //What to do here? 

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 . But for my project, I would like to use pure Ant to implement if else condition. Probably, I should use ?? But I am not sure how to use for my logic. Could some one help me please?

回答1:

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

this target will be called only when property $${do.test} is setthis target will be called only when property $${do.prod} is setthis target will be called only when property $${do.dev} is setthis target will be called only when property $${env} does not equal test/prod/dev

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.

         


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