经过几个小时的黑客攻击后回答我自己的问题......
<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