如何选择Ant的命令行参数?

时间:2009-09-04 16:31:26

标签: command-line ant build-process

我是ant的新手,如果使用默认目标以外的东西,我想要一个文件名,所以调用语法是这样的:

ant specifictarget -Dfile=myfile

我正在使用ant contrib package为我提供其他功能,所以我有这个:

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

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

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

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分:4)

你有正确的想法。

ant specifictarget -Dfile=myfile

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

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

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

答案 2 :(得分:2)

由于Ant中的属性不可变,您可以添加:

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

如果尚未在命令行上设置属性file,则将其设置为空字符串。然后你的平等测试将按照你的意图运作。

答案 3 :(得分:1)

或者,您可以使用转义值,因为当无法执行属性替换时,ant只会吐出实际文本。

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