سؤال

أقوم بتطوير مشروع تطبيق ويب صغير (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 ذلك. أي أحد يعرف؟

هل كانت مفيدة؟

المحلول 3

الإجابة على سؤالي بعد بضع ساعات من القرصنة ...

<script language="groovy" src="build.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;
}

لذلك أعتقد أن هذا يفعل ذلك بالنسبة لي. لقد كان يعمل بشكل جيد ، وهو قابل للتمديد. بالتأكيد ليست أفضل رائعة على الإطلاق ، لكنها واحدة من أولى. أيضا ، كان يتطلب بعض الجرار classpathed حتى تجمعها. لقد فقدت المسار ، لكنني أعتقد أنه محرك Javax.scripting ، محرك Groovy.jar و Groovy-All-1.5.6.Jar

نصائح أخرى

هل هذا يفعل ما تريد؟

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

للحصول على حل في النمل النقي ، جرب ما يلي:

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

لقد اختبرته ، وعملت كما هو متوقع. في النص المراد استبداله والقيمة التي تقوم بإدراجها بدلاً من ذلك ، يجب نقل جميع الأحرف "<" و ">" و "" "كـ <،> و".

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top