用于在任务名称中使用连字符运行Ant任务的命令行

时间:2010-11-23 14:27:19

标签: 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

3 个答案:

答案 0 :(得分:24)

将任务名称括在引号中。

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.

答案 1 :(得分:12)

有些人用破折号启动内部目标只是为了确保用户无法从命令行运行它们。事实上,出于这个原因,我将所有内部目标都以-开头是一种标准做法。

你可以尝试旧的双击法技巧。我没有在我当前的系统上安装Ant,所以我无法测试它。双破折号是一种常见的Unix技巧,当你有文件和以破折号开头的东西时,大多数命令都会用它来帮助结束参数。顺便说一句,任务应该是命令行上的最后一件事:

$ ant -f test.xml -- -task1

更糟糕的是,您可以在build.xml文件中定义另一个目标,该目标取决于此目标并带有短划线:

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

然后您应该可以致电sneaky

$ant -f test.xml sneaky

答案 2 :(得分:3)

来自ANT target doc

  

目标以连字符开头,例如&#34; -restart&#34;是有效的,可用于命名不应直接从命令行调用的目标。   对于Ants主类,每个以连字符开头的选项都是Ant本身的选项,而不是目标。因此,无法从命令行调用这些目标。

因此,用户无法使用命令行中的连字符调用目标。

于2016年4月21日在Windows平台上测试。