2011-08-31 78 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