3

我正在开发一个小型 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 似乎没有基本的方法来做到这一点。有人知道吗?

4

3 回答 3

3

这是做你想做的吗?

<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>
于 2008-10-03T21:56:11.043 回答
2

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

<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>

我测试了它,它按预期工作。在要替换的文本和您插入的值中,所有字符 '<'、'>' 和 '"' 都应引用为 <、> 和 "。

于 2008-11-05T16:54:58.567 回答
1

经过几个小时的黑客攻击后回答我自己的问题......

<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

于 2008-10-06T17:21:14.433 回答