【问题标题】:How can I embed one file into another with Ant?如何使用 Ant 将一个文件嵌入到另一个文件中?
【发布时间】:2010-09-15 03:25:57
【问题描述】:

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

【问题讨论】:

    标签: ant groovy build


    【解决方案1】:

    这是你想要的吗?

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

    【讨论】:

    • 彼得,谢谢你的建议。这个解决方案对我不起作用。我刚刚得到: 我尝试了使用和不使用 CDATA 块的情况,没有任何变化。它正在加载 ${jQueryContent} 值,但没有在文件中替换它。
    【解决方案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>
    

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

    【讨论】:

      【解决方案3】:

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

      <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

      【讨论】:

      • 感谢您的建议,对不起,但我会选择我自己的答案,因为它对未来更具可扩展性。谢谢大家!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 2015-02-02
      • 1970-01-01
      • 2020-08-18
      相关资源
      最近更新 更多