使用 Ant 1.6.2 版或更新版本,Catalina 任務提供選項,利用屬性或外部文件捕獲輸出。它們直接支持 類型屬性的子集:
屬性 |
屬性說明 |
是否必需 |
output |
輸出文件名。如果錯誤流沒有重定向到一個文件或屬性上,它將出現在輸出中。 |
否 |
error |
命令的標準錯誤應該被重定向到的文件。 |
否 |
logError |
用于在 Ant 日志中顯示錯誤輸出,將輸出重定向至某個文件或屬性。錯誤輸出不會包含在輸出文件或屬性中。如果利用 error 或 errorProperty 屬性重定向錯誤,則沒有任何效果。 |
否 |
append |
輸出和錯誤文件是否應該附加或覆蓋。默認為 false。 |
否 |
createemptyfiles |
是否應該創建輸出和錯誤文件,哪怕是空的文件。默認為 true。 |
否 |
outputproperty |
用于保存命令輸出的屬性名。除非錯誤流被重定向至單獨的文件或流,否則這一屬性將包含錯誤輸出。 |
否 |
errorproperty |
用于保存命令標準錯誤的屬性名。 |
否 |
還可以指定其他一些額外屬性:
屬性 |
屬性說明 |
是否必需 |
alwaysLog |
該屬性用于查看捕獲的輸出,這個輸出也出現在 Ant 日志中。除非捕獲任務輸出,否則千萬不要使用它。默認為 false。Ant 1.6.3 通過 直接支持該屬性。 |
否 |
failonerror |
用于避免因為 manager 命令處理中錯誤而導致 Ant 執行終止情況的發生。默認為 true。如果希望捕獲錯誤輸出,則必須設為false,否則 Ant 執行將有可能在未捕獲任何輸出前就被終止。該屬性只用于 manager 命令的執行上,任何錯誤的或丟失的命令屬性仍然會導致 Ant 執行終止。 |
否 |
它們還支持內嵌的 元素,你可以在這些元素中指定全套的屬性。但對于input、inputstring、inputencoding,即使接收,也無法使用,因為在這種上下文中它們沒有任何意義。詳情可參考 Ant 手冊以了解 元素的各個屬性。
下面這個范例摘錄了一段構建文件,展示了這種對輸出重定向的支持是如何運作的。
<target name="manager.deploy"
depends="context.status"
if="context.notInstalled">
<deploy url="${mgr.url}"
username="${mgr.username}"
password="${mgr.password}"
path="${mgr.context.path}"
config="${mgr.context.descriptor}"/>
</target>
<target name="manager.deploy.war"
depends="context.status"
if="context.deployable">
<deploy url="${mgr.url}"
username="${mgr.username}"
password="${mgr.password}"
update="${mgr.update}"
path="${mgr.context.path}"
war="${mgr.war.file}"/>
</target>
<target name="context.status">
<property name="running" value="${mgr.context.path}:running"/>
<property name="stopped" value="${mgr.context.path}:stopped"/>
<list url="${mgr.url}"
outputproperty="ctx.status"
username="${mgr.username}"
password="${mgr.password}">
</list>
<condition property="context.running">
<contains string="${ctx.status}" substring="${running}"/>
</condition>
<condition property="context.stopped">
<contains string="${ctx.status}" substring="${stopped}"/>
</condition>
<condition property="context.notInstalled">
<and>
<isfalse value="${context.running}"/>
<isfalse value="${context.stopped}"/>
</and>
</condition>
<condition property="context.deployable">
<or>
<istrue value="${context.notInstalled}"/>
<and>
<istrue value="${context.running}"/>
<istrue value="${mgr.update}"/>
</and>
<and>
<istrue value="${context.stopped}"/>
<istrue value="${mgr.update}"/>
</and>
</or>
</condition>
<condition property="context.undeployable">
<or>
<istrue value="${context.running}"/>
<istrue value="${context.stopped}"/>
</or>
</condition>
</target>
警告:多次調用 Catalina 任務往往并不是一個好主意,退一步說這樣做的意義也不是很大。如果 Ant 任務依賴鏈設定糟糕的話,即使本意并非如此,也會導致在一次 Ant 運行中多次運行任務。必須提前對你稍加警告,因為有可能當你從任務中捕獲輸出時,會出現一些意想不到的情況: