YUI+Ant 实现JS CSS压缩

今天研究了一下YUI yahoo开源框架,感觉很猛啊。

于是乎我做了一个YUI的ant实现,网上好多关于bat的实现,我就另辟蹊径,出个关于这个的ant实现,嘿嘿独一无二的文章,如果转载的话,其注明作者和网站

copyright:Mr.chen

好了具体操作如下:

官网:

yuicompressor-2.4.6.jar 下载地址 http://yuilibrary.com/downloads/#yuicompressor

YUIAnt.jar 下载地址 http://www.ubik-ingenierie.com/miscellanous/YUIAnt/

具体的相关代码如下:

Xml代码  收藏代码
  1. #css work dir  
  2. commonCss.dir = css  
  3.   
  4. #js work dir  
  5. commonJs.dir = js  
  6.   
  7. #build temp dir   
  8. output.temp.dir = build  
  9.   
  10. #output files in the directory  
  11. output.dir = ${output.temp.dir}_output  
  12.   
  13. #environment needs lib  
  14. liblib = lib  
Build.xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <project name="Compress CSS-JS" default="compress" basedir=".">  
  4.   
  5.     <property file="bulid.properties" />  
  6.   
  7.     <path id="yuiClasspath">  
  8.         <fileset dir="${lib}">  
  9.             <include name="*.*" />  
  10.         </fileset>  
  11.     </path>  
  12.   
  13.     <!-- #######################Init the environment of the tool ##########################-->  
  14.     <target name="init">  
  15.         <echo message="begin to init the init" />  
  16.         <echo message="delete all reference files." />  
  17.         <delete dir="${output.dir}" />  
  18.         <echo message="delete end" />  
  19.         <echo message="make the reference files." />  
  20.         <mkdir dir="${output.dir}" />  
  21.         <mkdir dir="${output.temp.dir}" />  
  22.         <echo message="make end." />  
  23.     </target>  
  24.   
  25.     <!-- #######################Combine the css files             ##########################-->  
  26.     <target name="combinecss" depends="init" description="Combine common css files">  
  27.         <echo message="begin to combine the css files to one file." />  
  28.         <concat destfile="${output.temp.dir}/combined_css.css" encoding="UTF-8" append="false">  
  29.             <fileset dir="${commonCss.dir}">  
  30.                 <include name="*.css" />  
  31.             </fileset>  
  32.         </concat>  
  33.         <echo message="combine end." />  
  34.     </target>  
  35.   
  36.     <!-- #######################Combine the js files             ##########################-->  
  37.     <target name="combinejs">  
  38.         <echo message="begin to combine the js files to one file." />  
  39.         <concat destfile="${output.temp.dir}/all_source.js" encoding="utf-8" append="false">  
  40.             <fileset dir="${commonJs.dir}">  
  41.                 <include name="*.js" />  
  42.             </fileset>  
  43.         </concat>  
  44.         <echo message="combine end." />  
  45.     </target>  
  46.   
  47.     <!-- #######################Compress the js and css files  ##########################-->  
  48.     <target name="compress" depends="combinecss,combinejs" description="Compress">  
  49.         <echo message="begin to compress the css file." />  
  50.         <taskdef name="yuicompress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">  
  51.             <classpath>  
  52.                 <path refid="yuiClasspath" />  
  53.             </classpath>  
  54.         </taskdef>  
  55.         <!-- first method compress the css files -->  
  56.         <yuicompress linebreak="10000000" warn="false" munge="yes" preserveallsemicolons="true" outputfolder="${output.dir}">  
  57.             <fileset dir="${output.temp.dir}">  
  58.                 <include name="*.css" />  
  59.             </fileset>  
  60.         </yuicompress>  
  61.         <echo message ="compress the css end." />  
  62.         <!-- second method compress the js files-->  
  63.         <echo message ="begin to compress the js file." />  
  64.         <apply executable="java" parallel="false" failonerror="true">  
  65.             <fileset dir="${output.temp.dir}" includes="all_source.js" />  
  66.             <arg line="-jar" />  
  67.             <arg path="${lib}/yuicompressor-2.4.6.jar" />  
  68.             <arg line="--charset utf-8" />  
  69.             <arg line="-o ${output.dir}/combined_js.js" />  
  70.             <srcfile />  
  71.         </apply>  
  72.         <echo message ="compress the js end." />  
  73.         <delete dir="${output.temp.dir}" />  
  74.     </target>  
  75.   
  76. </project>  
Xml代码  收藏代码
  1. @echo off  
  2. echo ################################################  
  3. echo ##########Tool Compress the js and css##########  
  4. echo ################################################  
  5. echo Please make sure your css and js in the css'directory and js'directory.  
  6. echo If sure,please enter any button to continue the tool.  
  7. pause  
  8. call ant -buildfile compress.xml compress>build.log  
  9. echo compress end   
  10. pause  

 相关的文件我提供下载,感觉好的,就留言吧

原文地址:https://www.cnblogs.com/ranzige/p/3968379.html