我正在开发一个小型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没有基本的方法来做到这一点。有人知道吗?
答案 0 :(得分: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>
答案 1 :(得分: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="<script type="text/javascript" src="${js-filename}"></script>" value="<script type="text/javascript">${js-file}</script>"/>
<replacefilter token="<link type="text/css" rel="stylesheet" href="${css-filename}" />" value="<style type="text/css">${css-file}</style>"/>
</replace>
</target>
我测试了它,它按预期工作。在要替换的文本中,您插入的值代替所有字符'&lt;','&gt;'和“”应引用为&amp; lt;,&amp; gt;和&amp; quot。
答案 2 :(得分: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,但它是我的第一个。此外,它需要一些classpathed jar来进行编译。我忘记了哪个,但我相信它是javax.scripting引擎,groovy-engine.jar和groovy-all-1.5.6.jar