2012-04-27 74 views
3

所有,Path元素包括條件

可有人請就如何包括在路徑元素的目錄幫助有條件的,如果該目錄存在:所以像下面

<path id="lib.path.ref"> 
    <fileset dir="${lib.dir}" includes="*.jar"/> 
    <path location="${build.dir}" if="${build.dir.exist}" /> 
</path> 

這目前還不是因爲工作path元素不支持if屬性。在我的情況下,我想包含build.dir,只要它存在。

感謝

+0

剛剛爲您找到這一個:http://stackoverflow.com/questions/666718/need-to-set-path-cp-in-ant-script-depending-on-value-of-a-property可能有幫助。 – htulsiani 2012-04-27 22:18:21

回答

3

無需安裝Ant-Contrib或類似螞蟻的擴展,你可以完成你想要的有以下XML:

<project default="echo-lib-path"> 
    <property name="lib.dir" value="lib"/> 
    <property name="build.dir" value="build"/> 
    <available file="${build.dir}" type="dir" property="build.dir.exists"/> 

    <target name="-set-path-with-build-dir" if="build.dir.exists"> 
     <echo message="Executed -set-path-with-build-dir"/> 
     <path id="lib.path.ref"> 
      <fileset dir="${lib.dir}" includes="*.jar"/> 
      <path location="${build.dir}" /> 
     </path> 
    </target> 

    <target name="-set-path-without-build-dir" unless="build.dir.exists"> 
     <echo message="Executed -set-path-without-build-dir"/> 
     <path id="lib.path.ref"> 
      <fileset dir="${lib.dir}" includes="*.jar"/> 
     </path> 
    </target> 

    <target name="-init" depends="-set-path-with-build-dir, -set-path-without-build-dir"/> 

    <target name="echo-lib-path" depends="-init"> 
     <property name="lib.path.property" refid="lib.path.ref"/> 
     <echo message="${lib.path.property}"/> 
    </target> 
</project> 

這裏最重要的部分是在-init目標會發生什麼。它取決於-set-path-with-build-dir-set-path-without-build-dir目標,但Ant僅根據是否設置了build.dir.exists來執行一個目標。

閱讀更多關於可用任務在這裏:http://ant.apache.org/manual/Tasks/available.html

+0

非常感謝您花時間給出一個清晰簡潔的工作解決方案。感謝這個社區。 – Afamee 2012-04-30 17:49:16