2010-11-23 50 views
21

任務名稱以連字符「 - 」開頭。命令行用任務名稱中的連字符運行Ant任務

<?xml version="1.0" encoding="UTF-8"?> 
<project name="proj"> 
    <target name="-task1"> 
     <echo>Done!</echo> 
    </target> 
</project> 

如何從命令行運行ant腳本時指定此任務?這是行不通的:

ant -task1 -f test.xml 
+0

爲什麼你需要用`-`啓動任務名? – khachik 2010-11-23 14:31:07

+2

我正在構建Android項目。出於某種原因,任務名稱看起來像「-pre-build」。 – alex2k8 2010-11-23 14:41:52

回答

25

將引用中的任務名稱括起來。

ant "-task1" -f test.xml 

更新: 從Ant docs

Targets beginning with a hyphen such as "-restart" are valid, 
and can be used to name targets that should not be called directly 
from the command line. 
For Ants main class every option starting with hyphen is an option for Ant itself 
and not a target. For that reason calling these target from command line is not 
possible. On the other hand IDEs usually don't use Ants main class as entry 
point and calling them from the IDE is usually possible. 
+3

這不行,至少在Windows上。錯誤信息是:未知的參數:-task1 – alex2k8 2010-11-23 14:52:11

12

有人開始內部目標與破折號只是爲了確保用戶無法在命令行中運行它們。事實上,我只是因爲這個原因纔將所有內部目標都從-開始作爲標準做法。

你可以嘗試舊的雙重短跑技巧。我目前的系統沒有安裝Ant,所以我無法測試它。雙破折號是大多數命令用來幫助結束參數的常用Unix技巧,當你有文件和以短劃線開頭的東西時。順便說一句,這些任務應該是你的命令行上的最後一件事:

$ ant -f test.xml -- -task1 

更糟糕來糟糕的是,你可以簡單地在你的build.xml文件中定義的另一個目標是在這個目標在它與破折號取決於:

<task name="sneaky" 
    depends="-task1"/> 

那麼你應該能夠調用sneaky

$ant -f test.xml sneaky 
1

ANT target doc

以連字符(例如「-restart」)開頭的目標有效,可用於命名不應直接從命令行調用的目標。 對於螞蟻主類,每個以連字符開頭的選項都是Ant本身的選項,而不是目標。因此從命令行調用這些目標是不可能的。

因此,用戶不能從命令行使用連字符調用目標。

2016年4月21日在Windows平臺上測試。