2012-02-09 228 views
30

我有一個用於構建的Ant XML文件。如何檢查一個屬性在Ant中是否有價值

我有3個屬性。如果這些屬性不包含任何值,我想打破構建。此外,如果值爲空,我想打破構建。

我該如何在Ant中做到這一點?

我使用Ant而不是Ant-contrib。

+0

可能重複的 - (http://stackoverflow.com/questions/226683/ant-filtering-fail-if-property-not-set) – oers 2012-02-09 08:52:03

回答

52

您可以使用<fail>任務使用條件:

<fail message="Property &quot;foo&quot; needs to be set to a value"> 
    <condition> 
     <or> 
      <equals arg1="${foo}" arg2=""/> 
      <not> 
       <isset property="foo"/> 
      </not> 
     </or> 
    </condition> 

這相當於說if (not set ${foo} or ${foo} = "")是僞代碼。您必須從內部讀取XML條件。

如果您只關心是否設置了該變量,而不是是否具有實際值,那麼您可以在<fail>任務上使用<unless>子句。

<fail message="Property &quot;foo&quot; needs to be set" 
    unless="foo"/> 

但是,如果屬性已設置但沒有值,則不會失敗。


有一個竅門,可以使這個簡單

<!-- Won't change the value of `${foo}` if it's already defined --> 
<property name="foo" value=""/> 
<fail message="Property &quot;foo&quot; has no value"> 
    <condition> 
      <equals arg1="${foo}" arg2=""/> 
    </condition> 
</fail> 

請記住,我不能重置屬性!如果${foo}已經有值,上面的<property>任務將不會執行任何操作。這樣,我可以消除<isset>條件。因爲你有三個屬性這可能是不錯的:

<property name="foo" value=""/> 
<property name="bar" value=""/> 
<property name="fubar" value=""/> 
<fail message="You broke the build, you dufus"> 
    <condition> 
     <or> 
      <equals arg1="${foo}" arg2=""/> 
      <equals arg1="${bar}" arg2=""/> 
      <equals arg1="${fubar}" arg2=""/> 
     </or> 
    </condition> 
</fail> 
+0

坦克[螞蟻濾波如果屬性未設定失敗。對我很好用 – KK99 2012-02-13 15:00:55

+0

它很好地避免嵌套的ifs。也可以設置條件屬性和值。 – user1587504 2013-03-06 08:23:13

+0

很好的回答!我注意到最後一個失敗聲明缺少一個正確的結束標記 - >缺少斜槓,我無法更正它,因爲編輯需要更改至少6個字符:< – AgentKnopf 2014-12-16 14:26:48

0

目標有了Ant addon Flaka你可以使用類似的模式:

<property name="foo" value="bar"/> 
... 
    <fl:unless test="has.property.foo"> 
    ... 
    </fl:unless> 
... 
    <fl:when test="has.property.foo"> 
    ... 
    </fl:when> 

混凝土檢查emptyness:

<fl:when test=" empty '${foo}' "> 
    <fail message="Houston we have a problem!!"/> 
</fl:when> 

一個完整的例子,也使用'eq'的一些等號檢查(與wo相反) ULD是 'NEQ'):

<project xmlns:fl="antlib:it.haefelinger.flaka"> 
    <!-- some if/then/else construct --> 
    <fl:choose> 
    <!-- if --> 
    <when test=" '${buildtype}' eq 'prod' "> 
     <!-- then --> 
     <echo>..starting ProductionBuild</echo> 
    </when> 
    <when test=" '${buildtype}' eq 'test' "> 
     <!-- then --> 
     <echo>..starting TestBuild</echo> 
    </when> 
    <!-- else --> 
    <otherwise> 
     <fl:unless test="has.property.dummybuild"> 
     <fail message="No valid buildtype !, found => '${buildtype}'"/> 
     </fl:unless> 
     <echo>.. is DummyBuild</echo> 
    </otherwise> 
    </fl:choose> 
</project> 

與螞蟻-f的build.xml -Dbuildtype = PROD

螞蟻-f的build.xml -Dbuildtype = PROD -Ddummybuild =任何

[echo] ..starting ProductionBuild 
輸出

輸出與錯字=>螞蟻 - 的build.xml -Dbuildtype = testt

BUILD FAILED 
/home/rosebud/workspace/AntTest/build.xml:21: No valid buildtype !, found => 'testt' 

與螞蟻-f的build.xml -DD輸出ummybuild =無論

[echo] .. is DummyBuild 
12

建立在其他的答案,這是我的首選形式,作爲宏:

<!-- Macro to require a property is not blank --> 
<macrodef name="prop-require"> 
    <attribute name="prop"/> 
    <sequential> 
     <fail message="Property &quot;@{prop}&quot; must be set"> 
      <condition> 
       <not> 
        <isset property="@{prop}"/> 
       </not> 
      </condition> 
     </fail> 

     <fail message="Property &quot;@{prop}&quot; must not be empty"> 
      <condition> 
       <equals arg1="${@{prop}}" arg2=""/> 
      </condition> 
     </fail> 
    </sequential> 
</macrodef> 

用作:

<target name="deploy.war" description="Do the war deployment ;)"> 
    <prop-require prop="target.vm" /> 
    <prop-require prop="target.vip" /> 
    <!-- ... --> 

爲了簡潔起見,您可以通過使用<or>將兩個失敗元素合併爲一個,但我更喜歡我的錯誤消息來對待我,就像我自己無法想象的那樣;)

0

我在一個老版本的Ant中,所以isset不可用。相反,我在等號中使用了以下符號。的

<target name="xxx"> 
     <echo message="${contextRoot}" /> 
     <if> 
      <!-- check if the contextRoot property is defined. --> 
      <equals arg1="${contextRoot}" arg2="$${contextRoot}" /> 
      <then> 
       <!-- it isn't set, set a default --> 
       <property name="contextRoot" value="/WebAppCtx" /> 
      </then> 
     </if> 
     <echo message="${contextRoot}" /> 
    </target> 
相關問題