[英]ant filtering - fail if property not set
我有一個 ant build.xml
,它使用<copy>
任務來復制各種 xml 文件。 它使用過濾從build.properties
文件合並屬性。 每個環境(dev、stage、prod)都有一個不同的build.properties
來存儲該環境的配置。
有時我們會向 Spring XML 或其他需要更新build.properties
文件的配置文件添加新屬性。
如果build.properties
缺少屬性,我希望 ant 快速失敗。 也就是說,如果任何原始的@...@
令牌進入生成的文件,我希望構建終止,以便用戶知道他們需要向其本地 build.properties 添加一個或多個屬性。
這可以通過內置任務實現嗎? 我在文檔中找不到任何內容。 我即將編寫一個自定義的 ant 任務,但也許我可以省點力氣。
謝謝
如果你正在尋找一個特定的屬性,你可以使用帶有除非屬性的失敗任務,例如:
<fail unless="my.property">Computer says no. You forgot to set 'my.property'!</fail>
有關更多詳細信息,請參閱Ant 的失敗任務的文檔。
您可以在 ant 1.7 中使用LoadFile
任務和match
條件的組合來完成。
<loadfile property="all-build-properties" srcFile="build.properties"/>
<condition property="missing-properties">
<matches pattern="@[^@]*@" string="${all-build-properties}"/>
</condition>
<fail message="Some properties not set!" if="missing-properties"/>
我打算建議您嘗試使用<property file="${filter.file}" prefix="filter">
將屬性實際加載到 Ant 中,如果其中任何一個未設置,則fail
,但我認為我錯誤地解釋了您的問題(如果未在屬性文件中設置指定的屬性,您希望失敗)。
我認為您最好的選擇可能是使用<exec>
來(取決於您的開發平台)對“@”字符執行 grep,然后將屬性設置為找到的出現次數。 不確定確切的語法,但是...
<exec command="grep \"@\" ${build.dir} | wc -l" outputproperty="token.count"/>
<condition property="token.found">
<not>
<equals arg1="${token.count}" arg2="0"/>
</not>
</condition>
<fail if="token.found" message="Found token @ in files"/>
由於 Ant 1.6.2 condition
也可以嵌套在fail
。
以下宏可以輕松地有條件地檢查多個屬性。
<macrodef name="required-property">
<attribute name="name"/>
<attribute name="prop" default="@{name}"/>
<attribute name="if" default="___"/>
<attribute name="unless" default="___"/>
<sequential>
<fail message="You must set property '@{name}'">
<condition>
<and>
<not><isset property="@{prop}"/></not>
<or>
<equals arg1="@{if}" arg2="___"/>
<isset property="@{if}"/>
</or>
<or>
<equals arg1="@{unless}" arg2="___"/>
<not><isset property="@{unless}"/></not>
</or>
</and>
</condition>
</fail>
</sequential>
</macrodef>
<target name="required-property.test">
<property name="prop" value=""/>
<property name="cond" value="set"/>
<required-property name="prop"/>
<required-property name="prop" if="cond"/>
<required-property name="prop" unless="cond"/>
<required-property name="prop" if="cond2"/>
<required-property name="prop" unless="cond2"/>
<required-property name="prop" if="cond" unless="cond"/>
<required-property name="prop" if="cond" unless="cond2"/>
<required-property name="prop" if="cond2" unless="cond"/>
<required-property name="prop" if="cond2" unless="cond2"/>
<required-property name="prop2" unless="cond"/>
<required-property name="prop2" if="cond2"/>
<required-property name="prop2" if="cond2" unless="cond"/>
<required-property name="prop2" if="cond" unless="cond"/>
<required-property name="prop2" if="cond2" unless="cond2"/>
<required-property name="success"/>
</target>
如果 exec 命令在您的 ant 版本中被棄用,您可以使用重定向器,例如:
<exec executable="grep">
<arg line="@ ${build.dir}"/>
<redirector outputproperty="grep.out"/>
</exec>
<exec executable="wc" inputstring="${grep.out}">
<arg line="-l"/>
<redirector outputproperty="token.found"/>
</exec>
創建 token.found 屬性
<condition property="token.found">
<not>
<equals arg1="${token.count}" arg2="0"/>
</not>
</condition>
<fail if="token.found" message="Found token @ in files"/>
對於有條件的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.