めじなてっく

出先で読みたいがためにナレッジを書き溜めるブログ

Antでbundle&minifyしたい場合

AntでいわゆるJSやCSSのバンドル化をしたいと思った時に使ったテク。

concat

複数ファイルを文字通りに連結させる。

<concat destfile="bundle.js" force="no">
  <fileset dir="/js" includes="*.js" excludes="hoge.js, piyo.js"/>
</concat>

minify

jarコマンドでYUICompressorを実行していたが、タスクにして呼べるようにするがいいかもね。

<?xml version="1.0" encoding="UTF-8"?>
<project default="build">

  <target name="build">

    <!-- JavaScript 連結 -->
    <concat destfile="httpdocs/all.js" encoding="UTF-8" fixlastline="yes" eol="lf">
      <filelist dir="httpdocs" files="jquery-1.4.2.js"/>
      <filelist dir="httpdocs/foo" files="bar.js"/>
      <filelist dir="httpdocs" files="common.js"/>
    </concat>

    <!-- JavaScript yui 圧縮、gzip 圧縮 -->
    <antcall target="yui.gzip"><param name="src" value="httpdocs/all.js"/></antcall>

    <!-- CSS 連結 -->
    <concat destfile="httpdocs/all.css" encoding="UTF-8" fixlastline="yes" eol="lf">
      <filelist dir="httpdocs" files="hoge.css"/>
      <filelist dir="httpdocs" files="fuga.css"/>
      <filelist dir="httpdocs" files="common.css"/>
    </concat>

    <!-- CSS yui 圧縮、gzip 圧縮 -->
    <antcall target="yui.gzip"><param name="src" value="httpdocs/all.css"/></antcall>
  </target>

  <target name="yui.gzip">
    <exec executable="java.exe" dir=".">
      <arg value="-jar"/>
      <arg value="yuicompressor-2.4.2.jar"/>
      <arg value="--charset"/>
      <arg value="UTF-8"/>
      <arg value="-o"/>
      <arg value="${src}.min"/>
      <arg value="${src}"/>
    </exec>

    <gzip src="${src}.min" destfile="${src}.gz" />
    <delete file="${src}.min" />
  </target>
</project>