代码之家  ›  专栏  ›  技术社区  ›  Keith Bentrup

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

  •  3
  • Keith Bentrup  · 技术社区  · 15 年前

    我是Ant的新手,如果使用的不是默认目标,我需要一个文件名,所以调用语法如下:

    ant specifictarget -Dfile=myfile
    

    我正在使用 ant contrib package 为了给我更多的功能,我有:

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

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

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

    4 回复  |  直到 15 年前
        1
  •  6
  •   M A    10 年前

    你真的不需要这个控制包。使用内置的Ant功能(如if/except和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>
    
        2
  •  4
  •   Jon W    15 年前

    你的想法是对的。这个

    ant specifictarget -Dfile=myfile
    

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

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

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

        3
  •  2
  •   Paul Schifferer    15 年前

    由于属性在Ant中不可变,可以添加以下内容:

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

    这将设置属性 file 如果尚未在命令行上设置空字符串。那么你的平等测试将按你的意愿进行。

        4
  •  1
  •   seth    15 年前

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

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