2014-03-07 28 views
5

我想弄明白一種方法讓Ant運行一個.jar可執行文件,它接受一個文件並從單個輸入文件中分出幾個生成的文件。具體來說,我試圖生成編譯的.js文件,同時生成.map文件。爲Ant應用指定多個輸出文件或目標文件任務

通常情況下,命令會是這樣的:

java -jar compiler-latest --js a.js --js_output_file a.min.js --create_source_map a.js.map 

其中:

  • compiler-latest是封閉編譯罐子
  • a.js是JavaScript文件編譯
  • a.min.js是編譯的JavaScript
  • a.js.map是源地圖

我的Ant腳本是這樣的:

<project name="BuildTest" default="Build" basedir="."> 
    <description> 
     HTML Build Test with Ant 
    </description> 
    <property name="src" location="../js"/> 
    <property name="dst" location="../build"/> 
    <property name="compiler" location="../compiler.jar"/> 

    <!--Make Dest Directory--> 
    <target name="-destination"> 
     <mkdir dir="${dst}"/> 
    </target> 

    <!--Compile JS--> 
    <target name="Build" depends="-destination"> 

     <!--Filesets and Mappers--> 
     <fileset id="sourceFiles" dir="${src}" includes="*.js"/> 
     <mapper id="compiledJs" type="glob" from="*.js" to="*.compiled.js"/> 
     <mapper id="mapJs" type="glob" from="*.js" to="*.js.map"/> 

     <!--Apply Everything--> 
     <apply executable="java" parallel="false" dest="${dst}"> 

     <!--Closure Compiler--> 
     <arg value="-jar"/> 
     <arg path="${compiler}"/> 
     <arg value="--compilation_level=SIMPLE_OPTIMIZATIONS"/> 

     <!--Source Files--> 
     <arg value="--js"/> 
     <srcfile/> 
     <fileset refid="sourceFiles"/> 

     <!--Output Files--> 
     <arg value="--js_output_file"/> 
     <targetfile/> 
     <mapper refid="compiledJs"/> 

     <!--Source Maps--> 
     <arg value="--source_map_format=V3"/> 
     <arg value="--create_source_map"/> 
     <arg value="--js_output_file"/> 
     <targetfile/> 
     <mapper refid="mapJs"/> 
     </apply> 
    </target> 

    <!--Clean Project--> 
    <target name="Clean" description="Cleans the project"> 
     <delete dir="${dst}"/> 
    </target> 
</project> 

但是,我得到一個錯誤,說我不能有多個<targetfile/>元素

apply doesn't support multiple targetfile elements. 

回答

6

這是一個解決方法,不好,但有效。

您可以使用Ant <compositemapper>構建您的應用程序的命令行。

下面是一個例子。您需要在任務上設置relative="yes",以便相對於構建目錄的文件名優先於絕對文件名使用,否則映射更困難。要構建命令行,請在<compositemapper>內提供一個映射器列表。對於固定部分使用<mergemapper>(像--output_file這樣的參數),並在需要生成文件名時使用合適的其他映射器,可能是glob。 需要一系列映射器來將傳遞給java的參數分開<apply>,否則它們將作爲一個帶有嵌入空格的long arg傳遞。

<apply executable="java" parallel="false" relative="yes"> 
    <arg line="-jar compiler-latest --js"/> 
    <srcfile /> 
    <targetfile /> 

    <compositemapper> 
    <mergemapper    to="--js_output_file" /> 
    <globmapper from="*.js" to="*.compiled.js" /> 
    <mergemapper    to="--source_map_format=V3" /> 
    <mergemapper    to="--create_source_map" /> 
    <globmapper from="*" to="*.map" /> 
    </compositemapper> 

    <fileset dir="." includes="*.js" /> 
</apply> 

對於一個簡單的測試,導致類似的命令行:

java -jar compiler-latest --js 1.js --js_output_file 1.compiled.js --source_map_format=V3 --create_source_map 1.js.map 
+0

謝謝,這幾乎解決了我的問題。我確實需要做一些調整,因爲我的編譯器離我的實際項目目錄很遠,這些目錄爲我提供了一些有趣的相關內容。不過,我現在需要弄清楚如何將地圖名稱附加到縮小腳本的文本末尾,我不確定如果沒有某些擴展,Ant就無法做到這一點。我試圖用一個'echo'和'concat'任務來運行,所以我想我可以堅持使用批處理/ shell'Makefile'解決方案。非常感謝您的幫助。螞蟻仍然看起來很強大。 – zero298