简体   繁体   English

如何检查属性在 Ant 中是否有价值

[英]How to check if a property has value in Ant

I have an Ant XML file which I use for build.我有一个用于构建的 Ant XML 文件。

I have 3 properties.我有3个属性。 I want to break the build if these properties does not contain any value.如果这些属性不包含任何值,我想中断构建。 Also I want to break the build if the value is empty.如果值为空,我也想中断构建。

How can I do this in Ant?我怎样才能在 Ant 中做到这一点?

I a using Ant and not Ant-contrib.我使用 Ant 而不是 Ant-contrib。

You can use conditions using the <fail> task:您可以使用<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>

This is equivalent to saying if (not set ${foo} or ${foo} = "") is pseudocode.这相当于说if (not set ${foo} or ${foo} = "")是伪代码。 You have to read the XML conditions from the inside out.您必须从内到外读取 XML 条件。

You could have used the <unless> clause on the <fail> task if you only cared whether or not the variable was set, and not whether it has an actual value.如果您只关心是否设置了变量,而不关心它是否具有实际值,则可以在<fail>任务上使用<unless>子句。

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

However, this won't fail if the property is set, but has no value.但是,如果设置了属性但没有值,这不会失败。


There's a trick that can make this simpler有一个技巧可以使这更简单

 <!-- 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>

Remember that I can't reset a property!请记住,我无法重置属性! If ${foo} already has a value, the <property> task above won't do anything.如果${foo}已经有一个值,上面的<property>任务将不会做任何事情。 This way, I can eliminate the <isset> condition.这样,我可以消除<isset>条件。 It might be nice since you have three properties:这可能会很好,因为您拥有三个属性:

<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>

Building on the other answers, this is my preferred form, as a Macro:基于其他答案,这是我的首选形式,作为宏:

<!-- 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>

To Be used as:用作:

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

For brevity you can collapse the two fail elements into one by using an <or> , but I prefer my error messages to treat me like I cannot think for myself ;)为简洁起见,您可以使用<or>将两个失败元素合并为一个元素,但我更喜欢我的错误消息将我视为我自己无法思考;)

I'm on an older version of Ant, so isset wasn't available.我使用的是旧版本的 Ant,所以 isset 不可用。 Instead I used the following notation with the double $ in the equals.相反,我在等号中使用了带有双 $ 的以下符号。

  <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>

Try this.试试这个。

<condition property="isPropertySet" value="true" else="false">
    <and>
        <isset property="my_property"/>
        <length string="${my_property}" trim="true" when="greater" length="0"/>
    </and>
</condition>
<fail unless="isPropertySet" message="The property my_property is not set."/>

Since Ant 1.9.1, it is possible to add if and unless attributes on all tasks and nested elements using special namespaces.从 Ant 1.9.1 开始,可以使用特殊命名空间在所有任务和嵌套元素上添加ifunless属性。

In order to use this feature you need to add the following namespace declarations为了使用此功能,您需要添加以下命名空间声明

xmlns:if="ant:if"
xmlns:unless="ant:unless"

The if and unless namespaces support the following conditions: ifunless命名空间支持以下条件:

  • true - true if the value of the attribute evaluates to true true - 如果属性的值计算为 true,则为 true
  • blank - true if the value of the attribute is null or empty blank - 如果属性值为 null 或为空,则为 true
  • set - true if the specified property is set set - 如果set了指定的属性,则为 true

Sample:样本:

<project name="tryit"
 xmlns:if="ant:if"
 xmlns:unless="ant:unless">
 <exec executable="java">
   <arg line="-X" if:true="${showextendedparams}"/>
   <arg line="-version" unless:true="${showextendedparams}"/>
 </exec>
 <condition property="onmac">
   <os family="mac"/>
 </condition>
 <echo if:set="onmac">running on MacOS</echo>
 <echo unless:set="onmac">not running on MacOS</echo>
</project>

From Ant manual "If And Unless"来自 Ant 手册“如果和除非”

您可以尝试使用条件...或创建一个目标,除非

With Ant addon Flaka you may use patterns like :使用Ant 插件 Flaka,您可以使用以下模式:

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

Concrete check for emptyness :具体检查空虚:

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

A complete example, also using some equals check with 'eq' (opposite would be 'neq'):一个完整的例子,还使用了一些带有 'eq' 的相等检查(相反的是 '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>

output with ant -f build.xml -Dbuildtype=prod使用 ant -f build.xml -Dbuildtype=prod 输出
or要么
ant -f build.xml -Dbuildtype=prod -Ddummybuild=whatever ant -f build.xml -Dbuildtype=prod -Ddummybuild=whatever

[echo] ..starting ProductionBuild

output with typo => ant - build.xml -Dbuildtype=testt带有错字的输出 => ant - build.xml -Dbuildtype=testt

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

output with ant -f build.xml -Ddummybuild=whatever使用 ant -f build.xml -Ddummybuild=whatever 输出

[echo] .. is DummyBuild

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM