2011-08-31 80 views
3

我敢肯定,這是微不足道的 - 但一直在抨擊我的頭牆 我想要一個目錄充滿了鬍鬚模板(html文件本質上)並將它們組合成一個文件 - 包裝每一個與標籤Apache Ant爲每個文件的concat作業添加頁眉和頁腳

例子:

File1 = <a>This is a Link</a> 
File2 = <b>This is in bold</b> 

我要像輸出看:

<script type="text/mustache" id="File1"> 
<a>This is a Link</a> 
</script> 
<script type="text/mustache" id="File2"> 
<b>This is in bold</b> 
</script> 

我使用的是CONCATŧ問

<concat destfile="mustache.js" fixlastline="yes"> 
<fileset dir="." includes="**/*.mustache"/> 
</concat> 

,但無法弄清楚如何讓腳本塊顯示

回答

1

起初,我想過使用CONCAT某種方式與頁眉和頁腳,但沒有找到一個有效的解決方案。
如果你不能迴避使用一些Ant的插件了,這裏是一個基於Flaka =

<project name="demo" xmlns:fl="antlib:it.haefelinger.flaka"> 

<!-- make standard ant tasks understand EL expressions --> 
    <fl:install-property-handler /> 

    <!-- we use path instead of pure fileset because we need 
     absolute filenames for loadfile later in for loop --> 
    <path id="foo"> 
    <fileset dir="/some/path" includes="**/*.mustache"/> 
    </path> 

    <!-- iterate over the path/fileset --> 
    <fl:for var="file" in="split('${toString:foo}', ':')"> 
    <!-- unset property for next loop --> 
    <fl:unset>content</fl:unset> 
    <!-- load file contents to property --> 
    <loadfile property="content" srcFile="#{file}"/> 

    <echo file="/some/path/foobar/mustache.js" append="true"> 
    <!-- the id attribute gets filled with the basename of the current fileitem --> 
<![CDATA[<script type="text/mustache" id="#{replace(file, '$1' , '.+?(\w+)\..+')}"> 
#{trim('${content}')} 
</script>]]></echo> 
    </fl:for> 

</project> 

注意的解決方案:
1.我的回聲任務中最左邊的符號,以避免不必要的空白產生的文件!
只是寫在上面我的例子,你的文件將看起來像你想要的輸出
2. <![CDATA[...]]>是必要的,否則你會得到一些錯誤,如「回聲不支持嵌套的」腳本「元素。

+0

感謝您的想法 - 實際上我們無法得到這個工作(並放棄了 - 使用bash腳本,而不是: – user922044

+0

這個例子完全是你要求的 - 什麼不適合你!? – Rebse