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

如何为Ant选择性地要求一个命令行参数?

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

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

    ant specifictarget -Dfile=myfile
    

    我在用 ant contrib package 为了给我额外的功能,我有这个:

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

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

    4 回复  |  直到 16 年前
        1
  •  6
  •   M A    11 年前

    你真的不需要contrib包。这更方便地使用内置的蚂蚁功能来完成,比如if/inly和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    16 年前

    你的想法是对的。这

    ant specifictarget -Dfile=myfile
    

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

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

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

        3
  •  2
  •   Paul Schifferer    16 年前

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

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

    这将设置属性 file 如果尚未在命令行上设置,则将其设置为空字符串。那么,你的平等测试将按你的预期进行。

        4
  •  1
  •   seth    16 年前

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

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