如何使用Ant将一个文件嵌入另一个文件?

时间:2020-03-06 15:05:16  来源:igfitidea点击:

我正在开发一个小型Web应用程序项目(ColdFusion),并且试图在开发过程中将项目拆分为多个文件,但在完成时仅部署一个文件。

我引用了外部文件,例如:

<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
<link type="text/css" rel="stylesheet" href="project.css" />

在构建项目时,我希望文件包含并嵌入在单个成品文件中。

<script type="text/javascript">eval(function(p,a,c,k,e,r) [...]</script>
<style type="text/css">div{font:normal;} [...]</style>

无论如何,Ant似乎没有一种基本的方法可以做到这一点。有人知道吗

解决方案

这是我们想要的吗?

<property
    name="filename"
    value="jquery-1.2.6.pack.js"
/>

<loadfile
    property="contents"
    srcfile="${filename}"
/>

<replace dir=".">
    <include name="index.cfm"/>
    <replacetoken><![CDATA[<script type="text/javascript" src="${filename}"></script>]]></replacetoken>
    <replacevalue><![CDATA[<script type="text/javascript">${contents}</script>]]></replacevalue>
</replace>

经过数小时的骇客活动,回答了我自己的问题...

<script language="groovy" src="build.groovy" />

并且此groovy脚本用文件内容本身替换了所有引用的javascript或者css文件。

f = new File("${targetDir}/index.cfm")
fContent = f.text
fContent = jsReplace(fContent)
fContent = cssReplace(fContent)
f.write(fContent)

// JS Replacement
def jsReplace(htmlFileText) {
    println "Groovy: Replacing Javascript includes"
    // extract all matched javascript src links
    def jsRegex = /<script [^>]*src=\"([^\"]+)\"><\/script>/
    def matcher = (htmlFileText =~ jsRegex)
    for (i in matcher) {
        // read external files in
        def includeText = new File(matcher.group(1)).text
        // sanitize the string for being regex replace string (dollar signs like jQuery/Prototype will screw it up)
        includeText = java.util.regex.Matcher.quoteReplacement(includeText)
        // weak compression (might as well)
        includeText = includeText.replaceAll(/\/\/.*/, "") // remove single-line comments (like this!)
        includeText = includeText.replaceAll(/[\n\r\f\s]+/, " ") // replace all whitespace with single space
        // return content with embedded file
        htmlFileText = htmlFileText.replaceFirst('<script [^>]*src="'+ matcher.group(1) +'"[^>]*></script>', '<script type="text/javascript">'+ includeText+'</script>');
    }
    return htmlFileText;
}

// CSS Replacement
def cssReplace(htmlFileText) {
    println "Groovy: Replacing CSS includes"
    // extract all matched CSS style href links
    def cssRegex = /<link [^>]*href=\"([^\"]+)\"[^>]*>(<\/link>)?/
    def matcher = (htmlFileText =~ cssRegex)
    for (i in matcher) {
        // read external files in
        def includeText = new File(matcher.group(1)).text
        // compress CSS
        includeText = includeText.replaceAll(/[\n\r\t\f\s]+/, " ")
        // sanitize the string for being regex replace string (dollar signs like jQuery/Prototype will screw it up)
        includeText = java.util.regex.Matcher.quoteReplacement(includeText)
        // return content with embedded file
        htmlFileText = htmlFileText.replaceFirst('<link [^>]*href="'+ matcher.group(1) +'"[^>]*>(<\/link>)?', '<style type=\"text/css\">'+ includeText+'</style>');
    }
    return htmlFileText;
}

所以我想这对我来说就可以了。它运行良好,并且可扩展。绝对不是有史以来最好的Groovy,但这是我的第一个。而且,它需要几个类路径的jar才能进行编译。我没有弄清楚哪个,但是我相信它是javax.scripting引擎groovy-engine.jar和groovy-all-1.5.6.jar

有关纯蚂蚁的解决方案,请尝试以下操作:

<target name="replace">
    <property name="js-filename" value="jquery-1.2.6.pack.js"/>
    <property name="css-filename" value="project.css"/>
    <loadfile property="js-file" srcfile="${js-filename}"/>
    <loadfile property="css-file" srcfile="${css-filename}"/>
    <replace file="input.txt">
        <replacefilter token="&lt;script type=&quot;text/javascript&quot; src=&quot;${js-filename}&quot;&gt;&lt;/script&gt;" value="&lt;script type=&quot;text/javascript&quot;&gt;${js-file}&lt;/script&gt;"/>
        <replacefilter token="&lt;link type=&quot;text/css&quot; rel=&quot;stylesheet&quot; href=&quot;${css-filename}&quot; /&gt;" value="&lt;style type=&quot;text/css&quot;&gt;${css-file}&lt;/style&gt;"/>
    </replace>
</target>

我对其进行了测试,并按预期工作。在要替换的文本和我们插入的值中,所有字符'<','>'和'"'均应引用为<,>和"。