Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ant在静态模板中包装html代码_Ant_Build Automation - Fatal编程技术网

使用ant在静态模板中包装html代码

使用ant在静态模板中包装html代码,ant,build-automation,Ant,Build Automation,我有几个HTML文件位于不同的位置(在公共根目录中),如下所示: index.html moduleA/list.html moduleA/add.html moduleB/list.html moduleB/add.html ... <project default="build"> <target name="build"> <copy todir="${dir.intermediate}/temp"> &

我有几个HTML文件位于不同的位置(在公共根目录中),如下所示:

index.html
moduleA/list.html
moduleA/add.html
moduleB/list.html
moduleB/add.html
...
<project default="build">
    <target name="build">
        <copy todir="${dir.intermediate}/temp">
            <fileset dir="${dir.source}" includes="**/*.html"/>
        </copy>
    </target>
</project>
<html>
  <head><title>Page-Title</title></head>
  <body>
  </body>
</html>
此外,我还有一个名为_template.html的文件,其中包含html代码和占位符
#CONTENT#
。 我需要的是:

  • 将所有HTML文件复制到public/directory
  • public/directory中的每个HTML文件的原始内容周围还应该包含来自_template.HTML的代码
  • 我使用ANT复制文件,但我不知道如何将模板代码包装在代码周围。。。 我的ANT脚本如下所示:

    index.html
    moduleA/list.html
    moduleA/add.html
    moduleB/list.html
    moduleB/add.html
    ...
    
    <project default="build">
        <target name="build">
            <copy todir="${dir.intermediate}/temp">
                <fileset dir="${dir.source}" includes="**/*.html"/>
            </copy>
        </target>
    </project>
    
    <html>
      <head><title>Page-Title</title></head>
      <body>
    
      </body>
    </html>
    
    
    

    示例:

    index.html

    <div>This is the index-page</div>
    
    这是索引页
    
    _template.html

    <html>
        <head><title>Page-Title</title></head>
        <body>
            #CONTENT#
        </body>
    </html>
    
    
    页面标题
    #内容#
    
    应生成输出文件:

    <html>
        <head><title>Page-Title</title></head>
        <body>
            <div>This is the index-page</div>
        </body>
    </html>
    
    
    页面标题
    这是索引页
    
    这在纯ant任务中是完全可能的:

    首先使用loadfile将“替换”字符串加载到属性中:

    <loadfile property="replacement" srcFile="index.html"/>
    
    
    
    然后在将模板复制到最终文件所在的某处后,执行以下操作:

    <replaceregexp file="${my.final.file}"
                   match="#CONTENT#"
                   replace="${replacement}"
    />
    
    
    

    就是这样,您的文件现在应该是所需的:)

    如果您想坚持使用内置的ant任务,一个可能的解决方法是将模板文件分为两个文件,一个“pre”部分如下:

    index.html
    moduleA/list.html
    moduleA/add.html
    moduleB/list.html
    moduleB/add.html
    ...
    
    <project default="build">
        <target name="build">
            <copy todir="${dir.intermediate}/temp">
                <fileset dir="${dir.source}" includes="**/*.html"/>
            </copy>
        </target>
    </project>
    
    <html>
      <head><title>Page-Title</title></head>
      <body>
    
      </body>
    </html>
    

    如果不使用ant contrib或脚本之类的工具,我看不到任何使用单个模板文件的方法。

    但这只适用于单个文件。您需要做更多的工作才能将所有文件包装到一个文件集中。@matt这可以是目标的一部分,并且可以使用ant contrib foreach任务多次调用目标。如果你想要一个例子,让我知道:)这个解决方案有风格,我喜欢它!然而,将它应用到许多文件(for loop+extra target)需要额外的脚本,这将使我已经庞大的ant脚本变得更大,更难维护。谢谢!此解决方案简单有效。读了这个答案后,我能在3分钟内解决我的问题。