2009-09-04 31 views
3

我是新来的蚂蚁,我想需要一个文件名,如果使用的不是默认目标以外的东西,因此调用语法将是这样的:我如何选择需要Ant的命令行参数?

ant specifictarget -Dfile=myfile 

我使用的ant contrib package给我额外的功能,所以我有这样的:

<if> 
    <equals arg1="${file}" arg2="" /> 
    <then> 
     <!-- fail here --> 
    </then> 
</if> 

我的想法是,如果没有指定文件可能是等于空字符串。显然,这不起作用,我没有在google上找到任何示例或手册中的正确语法。

那么我应该使用什么语法?

回答

6

你并不需要contrib包。这更方便地使用内置的ant功能完成,如if/unless和depends。见下文:

<target name="check" unless="file" description="check that the file property is set" > 
    <fail message="set file property in the command line (e.g. -Dfile=someval)"/> 
</target> 

<target name="specifictarget" if="file" depends="check" description=" " > 
    <echo message="do something ${file}"/> 
</target> 
+3

是会写同样的事情:-)。更短的版本将是“<失败除非='文件'消息='设置文件属性...'/>” – 2009-09-04 16:55:48

+0

+1谢谢大卫和马特。我结束了使用短版本。 – 2009-09-04 17:44:06

+0

伙计们,您是否需要严格在名为_specifictarget_的第二个目标中添加_if_属性? – Victor 2015-06-01 18:46:34

3

你有正确的想法。

ant specifictarget -Dfile=myfile 

从命令行设置Ant属性。所有你真正需要的是

<property name="file" value=""/> 

您的默认值。这样,如果没有指定文件,它将等于空字符串。

2

由于性质是不可变的蚂蚁,你可以补充一点:

<property name="file" value="" />

如果它尚未在命令行上设置则该属性设置file为空字符串。然后,您的平等测试将按照您的预期工作。

1

或者,您可以使用转义值,因为当ant不能进行属性替换时,它只是吐出实际的文本。

 <if> 
     <equals arg1="${file}" arg2="$${file}" /> 
     <then> 
     <echo>BARF!</echo> 
     </then> 
    </if>