【发布时间】:2011-01-17 20:32:30
【问题描述】:
当我们的一位开发人员错误输入了无法识别的 ant 目标名称时,结果是一条不友好的错误消息,例如:
BUILD FAILED
Target "foo" does not exist in the project "bar".
我更希望它运行一个显示可用目标列表的目标。有没有办法捕获 ant 错误消息并运行另一个目标或某种自定义错误消息?
谢谢。
【问题讨论】:
标签: ant
当我们的一位开发人员错误输入了无法识别的 ant 目标名称时,结果是一条不友好的错误消息,例如:
BUILD FAILED
Target "foo" does not exist in the project "bar".
我更希望它运行一个显示可用目标列表的目标。有没有办法捕获 ant 错误消息并运行另一个目标或某种自定义错误消息?
谢谢。
【问题讨论】:
标签: ant
错误消息非常用户友好。它明确指出 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 "foo""/>
<target name="fu"/> <!-- Internal target No description -->
<target name="bar"
description="Runs target "bar""/>
</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,因为它只是一个内部目标。
【讨论】:
ant -p 将在您的build.xml 文件中显示目标列表。够好吗?
【讨论】: