<?xml version="1.0" encoding="UTF-8"?>
<!-- 对指定目录下的所有js进行压缩,放入指定位置 -->
<project name="jsbuild" default="build">
<property name="web.dir" location="WebRoot" />
<property name="yuicompressor.jar" location="${web.dir}/WEB-INF/lib/yuicompressor-2.4.2.jar" />
<!-- 得到当前日期 -->
<tstamp>
<format property="now.date" pattern="yyyy-MM-dd" locale="zh" />
</tstamp>
<!-- 要压缩的js源文件所在文件夹,这个文件里的所有js将被压缩 -->
<property name="source.dir" location="${web.dir}/js"/>
<!-- 目标目录,压缩后的js文件放在哪个目录下 -->
<property name="dest.dir" location="${web.dir}/js/compress"/>
<!-- 压缩过程中的临时目录 -->
<property name="temp" location="${web.dir}/tmp/${now.date}"/>
<!-- 压缩完成后, copy要压缩文件至目标目录 -->
<target name="build" depends="js.compress">
<!--
<echo message="压缩完成后, 拷贝${temp}/*.js至${source.dir}目录,并删除tmp目录">[开始]</echo>
<copy todir="${source.dir}" overwrite="true">
<fileset dir="${temp}" includes="*.js" />
</copy>
<delete dir="${web.dir}/tmp"/>
<echo message="压缩完成后, 拷贝${temp}/*.js至${source.dir}目录,并删除tmp目录">[结束]</echo>
-->
<!-- <copy file="${temp}/compress-all-pack.js" tofile="${dest.dir}/compress-all-pack.js" overwrite="true" failοnerrοr="false"/>
<echo message="压缩完成后, 拷贝${temp}/compress-all-pack.js至目标目录"></echo> -->
</target>
<!-- YUI Compress 压缩合并操作-->
<target name="js.compress" depends="createTemp">
<!-- 循环压缩文件 -->
<echo message="循环压缩${temp}所有js文件">[开始]</echo>
<apply executable="java" parallel="false" failοnerrοr="true">
<fileset dir="${source.dir}" includes="*.js" /> <!-- 源文件及所在目录, 指定下面要循环文件 -->
<arg line="-jar" />
<arg path="${yuicompressor.jar}" />
<arg line="--charset utf-8" />
<srcfile />
<arg line="-o" />
<targetfile />
<mapper type="glob" from="*.js" to="${temp}/*.js" />
</apply>
<echo message="循环压缩${temp}所有js文件">[结束]</echo>
<!-- 连接文件至目标目录 fixlastline="true" 每个文件占一行 -->
<echo message="将${temp}目录下所有js压缩成一个文件至${temp}/compress-all-pack.js">[开始]</echo>
<concat destfile="${dest.dir}/compress-all-pack.js" fixlastline="true" encoding="UTF-8">
<fileset dir="${temp}">
<include name="**.js" />
<exclude name="*.css"/> <!-- 排除 -->
</fileset>
</concat>
<echo message="将${temp}目录下所有js压缩成一个文件至${dest.dir}/compress-all-pack.js">[结束]</echo>
</target>
<!-- 创建临时目录 -->
<target name="createTemp" description="创建临时目录">
<delete dir="${temp}"/>
<mkdir dir="${temp}"/>
<echo>创建临时目录${temp}</echo>
</target>
</project>
yuicompressor压缩js的ant脚本
最新推荐文章于 2021-02-13 01:54:04 发布