Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在ant属性文件中的If-then-else_Java_Ant - Fatal编程技术网

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

Java 在ant属性文件中的If-then-else,java,ant,Java,Ant,我有一个Java Ant项目,其中有一个build.xml文件,该文件从build.properties内部获取很多属性。类似于build.properties中的内容 p1=<val1> p2=<val2> p3=<val3> .. p1= p2= p3= .. 现在,我想根据p1的值有条件地修改属性p2和p3。比如: <if p1 == "some_val"> p2=<new_val> p3=<new_val>

我有一个Java Ant项目,其中有一个build.xml文件,该文件从build.properties内部获取很多属性。类似于build.properties中的内容

p1=<val1>
p2=<val2>
p3=<val3>
..
p1=
p2=
p3=
..
现在,我想根据p1的值有条件地修改属性p2和p3。比如:

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

p2=
p3=
p2=
p3=
问题是,我无法将值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*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>

如果我理解正确,p1、p2和p3已经从属性文件中读取,这意味着p2和p3无法写入。在这种情况下,解决方案是使用前缀从文件中读取它们,例如
,后跟
,然后如上所述继续。