2010-11-23 54 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平台上测试。