【问题标题】:Command line to run the Ant task with hyphen in the task name在任务名称中使用连字符运行 Ant 任务的命令行
【发布时间】:2010-11-23 14:27:19
【问题描述】:

任务名称以连字符“-”开头。

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

从命令行运行 ant 脚本时如何指定此任务?这行不通:

ant -task1 -f test.xml

【问题讨论】:

  • 为什么任务名要以-开头?
  • 我正在构建 Android 项目。由于某种原因,任务名称看起来像“-pre-build”。

标签: ant


【解决方案1】:

用引号将任务名称括起来。

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.

【讨论】:

  • 这行不通,至少在 Windows 上是这样。错误消息是:未知参数:-task1
  • 对不起,这没有帮助,已在 linux 机器上测试过。
  • 想知道它是否可能。可能是 Android 团队使用“-”使某些任务从外部不可见。
  • 不。引号是shell的权限,执行命令时会将其变为[ant] [-task1],这是同样的问题。如果引用的规则名称适合您,那么未引用的规则名称也适合您。
  • 关于为什么某些任务以 - 开头的好信息,但我认为正确的答案是 David W 提供的答案。我不确定为什么用引号括起来有效 - 我认为那会即使在 UNIX 下也无法使用。
【解决方案2】:

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

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

$ ant -f test.xml -- -task1

更糟糕的是,您可以简单地在您的 build.xml 文件中定义另一个目标,该目标依赖于该目标,其中包含破折号:

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

那你应该可以拨打sneaky

$ant -f test.xml sneaky

【讨论】:

  • 遗憾的是,双破折号的技巧似乎不起作用。我在 Windows 和 Linux 上对其进行了测试,得到:Unknown argument: --
  • @MartinMcNulty 在带有 Ant 1.9.2 的 CentOS 上对我不起作用。 Ant 只是将带有 dash 的目标视为一个选项。
  • 是的,双破折号不被 shell 处理,而是像任何其他参数一样被提供给被调用的命令。如果ant 没有以特定方式处理这个问题,那就无济于事了。
【解决方案3】:

来自ANT target doc

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

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

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

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 2011-04-25
    • 2013-06-24
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2012-09-30
    相关资源
    最近更新 更多