【问题标题】:Ant - If condition - reading from external array/file and compare it with first argAnt - 如果条件 - 从外部数组/文件读取并将其与第一个 arg 进行比较
【发布时间】:2018-02-16 03:11:05
【问题描述】:

我正在尝试使用 ant 来构建针对各种条件的结构。 我要根据他们所属的大陆为地球和大陆的所有国家行动

<if>
      <or>
          <equals arg1="${country}" arg2="US" />
          <equals arg1="${country}" arg2="CA" />
       </or>
    <then>
        <!-- do stuff -->
    </then>
    <elseif>
        <or>
          <equals arg1="${country}" arg2="DE" />
          <equals arg1="${country}" arg2="AT" />
          <equals arg1="${country}" arg2="FR" />
          <equals arg1="${country}" arg2="NL" />
          <equals arg1="${country}" arg2="SE" />
          <equals arg1="${country}" arg2="ES" />
          ..
       </or>
    <then>
        <!-- do stuff -->
    </then>
    </elseif>
    <elseif>
        <or>
          <equals arg1="${country}" arg2="JP" />
          <equals arg1="${country}" arg2="KR" />
          <equals arg1="${country}" arg2="AU" />
          <equals arg1="${country}" arg2="SA" />
          <equals arg1="${country}" arg2="PL" />
          <equals arg1="${country}" arg2="CN" />
          ..
       </or>
    <then>
        <!-- do stuff -->
    </then>
    </elseif>
</if>

现在,如果您可以想象有 200 多个国家/地区 + 我也想对将导致非常大的文件的语言使用 NOT、AND 和 OR 条件,以后将很难修改和维护。 有没有办法通过将所有国家/地区排序为文件中的数组或列表并让 ant 从该文件中读取来最小化代码。 外部文件:

North_America=["US","CA"]
Europe=["DE","GB","FR",...]
Asia=["JP","PL","CN",..]
AFRICA=[..]
South_America[..]

那么像这样使用ant就很简单了:

<if>
      <equals arg1="${country}" arg2="${North_America}" />
    <then>
        <!-- do stuff -->
    </then>
    <elseif>
          <equals arg1="${country}" arg2="${Asia}" />
    <then>
        <!-- do stuff -->
    </then>
    </elseif>
    <elseif>
        <equals arg1="${country}" arg2="${Africa}" />
    <then>
        <!-- do stuff -->
    </then>
    </elseif>
..
</if>

我的问题不是如何在 ant 中加载或读取属性文件,而是如何从数组或列表中读取值

<equals arg1="${country}" arg2="${Asia}" />

“亚洲”在哪里是数组(或列表),如果第一个 arg 等于该数组的任何值,是否有可能在 ant 中实现这样的 IF 条件?或者即使有更好的方法来完成这项任务。

【问题讨论】:

    标签: java ant


    【解决方案1】:

    Ant 属性始终是字符串,因此无法真正存储或读取数组(或任何其他类型的对象)。

    幸运的是,对于您的问题,国家/地区缩写总是以唯一的 2 个字母字符串的形式出现,因此只需从存储为一个长字符串的逗号分隔列表中读取它们就可以轻松可靠地完成。

    如果可能的话,我建议完全放弃 ant-contrib。有时您的脚本需要for 循环,实际上没有其他方法可以解决,但如果您只是处理一堆条件,最好使用原生Ant。如果您好奇,我可以进一步详细说明。

    关于国家信息是否应该在您的构建脚本或属性文件中分组的问题,这只是一个选择问题。我个人更喜欢将数据存储在属性文件中,只在脚本本身中保留逻辑。

    continents.properties

    continent_southAmerica=AR,BR
    continent_NorthAmerica=US,CA
    continent_Asia=PL,SY,JP,SA,CN
    continent_Africa=EG,TU,SU,MR,Ly
    continent_Europe=FR,Nl,DE,DK,GB
    

    构建.xml

    <fail message="Please specify a country. (-Dcountry=US)">
        <condition>
            <not>
                <isset property="country" />
            </not>
        </condition>
    </fail>
    
    <property file="continents.properties" />
    
    <target
        name="build"
        depends="
            north-america-stuff,
            south-america-stuff,
            asia-stuff,
            africa-stuff,
            europe-stuff"
    >
        <echo message="Build complete for ${country}, ${continent}." />
    </target>
    
    <target name="init-continent">
        <condition property="continent.is.north.america">
            <contains string="${continent_NorthAmerica}" substring="${country}" />
        </condition>
    
        <condition property="continent.is.south.america">
            <contains string="${continent_SouthAmerica}" substring="${country}" />
        </condition>
    
        <condition property="continent.is.asia">
            <contains string="${continent_Asia}" substring="${country}" />
        </condition>
    
        <condition property="continent.is.africa">
            <contains string="${continent_Africa}" substring="${country}" />
        </condition>
    
        <condition property="continent.is.europe">
            <contains string="${continent_Europe}" substring="${country}" />
        </condition>
    </target>
    
    <target name="north-america-stuff" if="continent.is.north.america" depends="init-continent">
        <property name="continent" value="North America" />
    
        <echo message="Continent: ${continent}" />
    </target>
    
    <target name="south-america-stuff" if="continent.is.south.america" depends="init-continent">
        <property name="continent" value="South America" />
    
        <echo message="Continent: ${continent}" />
    </target>
    
    <target name="asia-stuff" if="continent.is.asia" depends="init-continent">
        <property name="continent" value="Asia" />
    
        <echo message="Continent: ${continent}" />
    </target>
    
    <target name="africa-stuff" if="continent.is.africa" depends="init-continent">
        <property name="continent" value="Africa" />
    
        <echo message="Continent: ${continent}" />
    </target>
    
    <target name="europe-stuff" if="continent.is.europe" depends="init-continent">
        <property name="continent" value="Europe" />
    
        <echo message="Continent: ${continent}" />
    </target>
    

    【讨论】:

    • 感谢@CAustin 非常有用的答案。
    【解决方案2】:

    在询问和重新思考之后,我发现最好的方法是创建新的属性文件并将其中的值添加到 ant 脚本中加载该属性文件。 我也没有使用“等于 arg”条件,而是使用了“包含子字符串”

    continent.properties 文件

    continent_southAmerica="AR", "BR"...
    continent_NorthAmerica="US", "CA"
    continent_Asia="PL", "SY", "JP", "SA", "CN",....
    continent_Africa="EG", "TU", "SU", "MR", "Ly",....
    continent_Europe="FR", "Nl", "DE", "DK", "GB",....
    

    蚂蚁脚本

      <!-- load continent.properties file -->
        <loadproperties srcfile="../${continent.properties}" encoding="UTF-8">
            <filterchain>
                <escapeunicode />
            </filterchain>
        </loadproperties>
    
      <if>
          <contains string="${continent_southAmerica}" substring="${country}" />
        <then>
            <!-- do stuff -->
        </then>
        <elseif>
              <contains string="${continent_Asia}" substring="${country}" />
        <then>
            <!-- do stuff -->
        </then>
        </elseif>
        <elseif>
            <contains string="${continent_Europe}" substring="${country}" />
        <then>
            <!-- do stuff -->
        </then>
        </elseif>
         ..
      </if>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多