Antで1つのファイルを別のファイルに埋め込むにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/167990

  •  03-07-2019
  •  | 
  •  

質問

小さなWebアプリプロジェクト(ColdFusion)を開発しており、開発中にプロジェクトを複数のファイルに分割し続けようとしていますが、完了時に1つのファイルのみをデプロイします。

たとえば、外部ファイルへの参照があります:

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

この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ではありませんが、それは私の最初のGroovyの1つです。また、コンパイルするためにいくつかのクラスパスjarが必要でした。追跡できませんでしたが、javax.scriptingエンジン、groovy-engine.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>

テストしましたが、期待通りに機能しました。置換するテキストと挿入する値には、代わりにすべての文字「<!> lt;」、「<!> gt;」を挿入しますおよび '<!> quot;' <!> amp; lt ;, <!> amp; gt;として引用する必要がありますおよび<!> amp; quot。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top