代码之家  ›  专栏  ›  技术社区  ›  alex2k8

命令行以运行任务名称中带有连字符的Ant任务

ant
  •  23
  • alex2k8  · 技术社区  · 15 年前

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

    <?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 回复  |  直到 9 年前
        1
  •  26
  •   Jim    15 年前

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

    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.
    
        2
  •  12
  •   David W.    15 年前

    有些人用破折号启动内部目标,只是为了确保用户不能从命令行运行它们。事实上,我把所有的内部目标从 - 就因为这个原因。

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

    $ ant -f test.xml -- -task1
    

    越来越糟,你可以简单地定义另一个目标 build.xml 依赖于此目标并带有破折号的文件:

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

    那么你应该可以打电话 sneaky :

    $ant -f test.xml sneaky
    
        3
  •  3
  •   Heefan    9 年前

    ANT target doc

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

    因此,用户不能用命令行中的连字符调用目标。

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

    推荐文章