2008-12-04 72 views
2

我需要從Ant腳本的文件中讀取屬性的值,並去掉前幾個字符。有關財產是在Ant中讀取屬性值

path=file:C:/tmp/templates 

此屬性是儲存在一個文件中,我可以通過

<property file="${web.inf.dir}/config.properties"/> 

ant腳本中訪問我有兩個問題:

  1. 如何閱讀加載的屬性文件中的單個'路徑'屬性?
  2. 如何從屬性值中刪除前導'file:'?

最後,我想有Ant腳本內訪問下列名稱 - 值對:

path=C:/tmp/templates 

乾杯, 唐

回答

0

你可以使用ant的exec任務和一個系統命令。

我很快就寫了這個來測試的概念:

<target name="foo"> 
    <property name="my.property" value="file:C:/foo/bar"/> 
    <exec executable="/bin/cut" inputstring="${my.property}" outputproperty="new.property"> 
    <arg line="-d':' -f2-"/> 
    </exec> 
    <echo message="FOO: ${new.property}"/> 
</target> 

不幸的是如果你可以建立/斌/剪切或某種形式的可執行文件,你可以使用一個系統上這僅適用。

+0

不幸的是,這需要在Windows和Linux – 2008-12-04 22:17:51

+0

工作是啊,這會是一個有點麻煩,如果你沒有像你的Windows系統上安裝了Cygwin。 – 2008-12-04 22:31:59

3

螞蟻1.6或更高版本,您可以使用LoadProperties與嵌套FilterChain

<loadproperties srcFile="${property.file.name}"> 
    <filterchain> 
    <tokenfilter> 
     <containsstring contains="path=file:"/> 
     <replaceregex pattern="path=file:" replace="path=" flags=""/> 
    </tokenfilter> 
    </filterchain> 
</loadproperties> 

這將導致path財產加載帶有字符串「文件:」剝離。

沒有測試,概不退換......

+0

不支持嵌套 2008-12-05 15:20:26

3

如何只是改變了屬性文件,這樣你就可以同時訪問和滿簡單的路徑?

path=C:/tmp/templates 
fullpath=file:${path}