2009-11-04 38 views
8

我有一個實用程序構建腳本,可以從構建服務器上的各種特定於項目的構建腳本中調用。一切正常,直到相對目錄結構改變。那就是:在ant文件中引用ant腳本位置

trunk/ 
    utilities/ 
     imported.xml 
     some_resource_file 
    projectName/ 
     importing.xml 

工作得很好,但有時我們需要:

trunk/ 
    importing.xml 
    utilities/ 
     imported.xml 
     some_resource_file 
    projectName/ 

的問題是,imported.xml需求some_resource_file,目前參照../utilities/some_resource_file得到它。這在第一種情況下很明顯,因爲工作目錄是utilities的兄弟。

難道imported.xml有一個簡單的方法來知道它在什麼目錄中,相當於在bash中的dirname $0?或者我必須這樣做,我必須以某種方式從導入腳本中注入它?

回答

19

確保imported.xml定義的項目具有name屬性。然後,您可以通過ant.file.name屬性將該名稱用於螞蟻文件的絕對路徑。

我已經大寫了IMPORTED,所以你可以很容易地在代碼中看到它。

<project 
    name="IMPORTED" 
> 
    <dirname 
    property="IMPORTED.basedir" 
    file="${ant.file.IMPORTED}" 
    /> 
    <property name="myresource" 
    location="${IMPORTED.basedir}/some_resource_file" 
    /> 
</project> 
+0

這很好。我在手冊中找到了答案,但只在這裏複製了鏈接。也許這會幫助那些不太容易瀏覽ant手冊的人。 – 2009-11-05 15:11:16