繁体   English   中英

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

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

我有一个用于构建的 Ant XML 文件。

我有3个属性。 如果这些属性不包含任何值,我想中断构建。 如果值为空,我也想中断构建。

我怎样才能在 Ant 中做到这一点?

我使用 Ant 而不是 Ant-contrib。

您可以使用<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>

基于其他答案,这是我的首选形式,作为宏:

<!-- 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>将两个失败元素合并为一个元素,但我更喜欢我的错误消息将我视为我自己无法思考;)

我使用的是旧版本的 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>

试试这个。

<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."/>

从 Ant 1.9.1 开始,可以使用特殊命名空间在所有任务和嵌套元素上添加ifunless属性。

为了使用此功能,您需要添加以下命名空间声明

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

ifunless命名空间支持以下条件:

  • true - 如果属性的值计算为 true,则为 true
  • blank - 如果属性值为 null 或为空,则为 true
  • set - 如果set了指定的属性,则为 true

样本:

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

来自 Ant 手册“如果和除非”

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

使用Ant 插件 Flaka,您可以使用以下模式:

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

具体检查空虚:

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

一个完整的例子,还使用了一些带有 '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>

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

[echo] ..starting ProductionBuild

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

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

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

[echo] .. is DummyBuild

暂无
暂无

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

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