簡體   English   中英

ant屬性文件中的if-then-else

[英]If-then-else in ant properties file

我有一個帶有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*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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM