2009-09-04 62 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>