ant属性文件中的If-then-else



我有一个带有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。

有什么建议吗?

尝试如下:

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

有一个具有所需逻辑的脚本

<?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*p1s*=.*$"/>
            </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>

相关内容

  • 没有找到相关文章

最新更新