在ant属性文件中是if-then-else

时间:2014-05-08 23:37:45

标签: java ant

我有一个带有build.xml文件的Java Ant项目,该文件在内部从build.properties获取了很多属性。 build.properties

中有类似的东西
p1=<val1>
p2=<val2>
p3=<val3>
..

现在,我想根据p1的值有条件地修改属性p2和p3。类似的东西:

<if p1 == "some_val">
  p2=<new_val>
  p3=<new_val>
<else>
  p2=<new2_val>
  p3=<new2_val>
</if>

问题是,我无法将值p1,p2和p3转换为build.xml,因为文件中有许多后续属性依赖于p1,p2和p3。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

<project name="demo" default="go">

  <condition property="p1_someval">
    <equals arg1="${p1}" arg2="someval"/>
  </condition>

  <target name="-go-someval" if="p1_someval">
    <property name="p2" value="newval"/>
    <property name="p3" value="newval"/>
  </target>

  <target name="-go-notsomeval" unless="p1_someval">
    <property name="p2" value="new2val"/>
    <property name="p3" value="new2val"/>
  </target>

  <target name="go" depends="-go-someval,-go-notsomeval">
    <echo message="p2=${p2}"/>
    <echo message="p3=${p3}"/>
  </target>

</project>

答案 1 :(得分:0)

有一个需要逻辑的脚本

<?xml version="1.0" encoding="UTF-8"?>
<project name="project">

    <!-- Load only p1 value from build.properties file -->
    <loadproperties srcfile="build.properties">
        <filterchain>
            <linecontainsregexp>
                <regexp pattern="^\s*p1\s*=.*$"/>
            </linecontainsregexp>
        </filterchain>
    </loadproperties>

    <!-- Set p2 and p3 depend on p1 value -->
    <condition property="p2" value="new_val" else="new2_val">
        <equals arg1="${p1}" arg2="some_val" trim="yes"/>
    </condition>
    <condition property="p3" value="new_val" else="new2_val">
        <equals arg1="${p1}" arg2="some_val" trim="yes"/>
    </condition>

    <!-- Load other properties -->
    <property file="build.properties"/>

</project>