Apache Ant如何在拼接作业中为每个文件添加页眉和页脚?

3

我相信这很简单,但我一直在苦思冥想。

我正在尝试将一个目录中的mustache模板(实质上是html文件)合并成一个文件,并用<div>标签包装每个模板。

例如:

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个回答

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. 在echo任务中使用我的最左边的符号,以避免结果文件中出现不必要的空格!
只需按照我上面的示例编写,您的文件将看起来像您想要的输出
2. 需要使用<![CDATA[...]]>,否则您将会得到一些错误,例如“echo不支持嵌套的“script”元素。”


谢谢您的想法 - 实际上我们没能让它工作(放弃了,改用bash脚本 :()。 - user922044
这个示例完全按照你的要求执行了 - 什么地方出了问题呢? - Rebse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接