【问题标题】:How can I display a more informative error message on mistyped ant targets?如何在错误输入的 ant 目标上显示更多信息性错误消息?
【发布时间】:2011-01-17 20:32:30
【问题描述】:

当我们的一位开发人员错误输入了无法识别的 ant 目标名称时,结果是一条不友好的错误消息,例如:

BUILD FAILED
Target "foo" does not exist in the project "bar". 

我更希望它运行一个显示可用目标列表的目标。有没有办法捕获 ant 错误消息并运行另一个目标或某种自定义错误消息?

谢谢。

【问题讨论】:

    标签: ant


    【解决方案1】:

    错误消息非常用户友好。它明确指出 build.xml 文件中不存在指定的目标。也许非技术用户会为简洁的 error 消息而烦恼。 (什么是目标?什么是项目“bar”?)但是,程序员应该有足够的技术来阅读消息并意识到他们的错误。

    开发人员可以通过ant --projecthelp 命令显示所有外部目标。这可以缩写为ant -p

    您可以通过将description 参数添加到开发人员可以使用的有效目标来帮助该过程。如果您的build.xml 中的单个目标具有description 参数,则ant --projecthelp 将仅显示具有description 参数的目标。

    您还可以将<description> 任务添加到build.xml 文件的顶部以显示有关项目的信息。这是一个简单的例子:

    <project name="Fubar" default="foo">
    
        <description>
            Project Fubar is a highly secret project that you shouldn't know
            anything about. If you have read this, you've violated national
            security guidelines and must be terminated with extreme finality.
            Hey, it hurts me more than  it hurts you.
        </description>
    
        <target name="foo"
            description="Runs target &quot;foo&quot;"/>
    
        <target name="fu"/>     <!-- Internal target No description -->
    
        <target name="bar"
            description="Runs target &quot;bar&quot;"/>
    </project>
    

    这是我的ant -p 输出:

    $ ant --projecthelp
    Buildfile: build.xml
    
            Project Fubar is a highly secret project that you shouldn't know
            anything about. If you have read this, you've violated national
            security guidelines and must be terminated with extreme finality.
            Hey, it hurts me more than  it hurts you.
    
    Main targets:
    
     bar  Runs target "bar"
     foo  Runs target "foo"
    Default target: foo
    $
    

    请注意,我的 build.xml 中有三个目标,但没有提及目标 fu,因为它只是一个内部目标。

    【讨论】:

      【解决方案2】:
      1. Ant-contrib 的 Trycatch 任务在这种情况下可能很有用
      2. 自定义 Ant API 的 Listener

      【讨论】:

        【解决方案3】:

        ant -p 将在您的build.xml 文件中显示目标列表。够好吗?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多