Ant 执行 YUICompressor
Ant版权声明:本文为博主原创文章,未经博主允许不得转载。
首先,需要准备二个 .jar 文件,分别是 YUIAnt.jar 和 yuicompressor-2.4.x.jar 。本帖发表日期是 2012-4-5 周四,最新版是 yuicompressor-2.4.7 。
YUIAnt.jar 下载地址 http://www.ubik-ingenierie.com/miscellanous/YUIAnt/
yuicompressor-2.4.x.jar 下载地址 http://www.julienlecomte.net/yuicompressor/
在 Ant 的构建过程描述文件(build.xml)中,可以参考如下例子来引入。
- <property name="dir.lib.yuicompress" value="lib"/><!-- 存放 YUI Compress 二个 .jar 文件的目录 -->
- <property name="dir.build.js" value="dist/webapp/js"/><!-- 存放压缩过的 JavaScript 文件目录 -->
- <property name="dir.build.css" value="dist/webapp/css"/><!-- 存放压缩过的 CSS 文件目录 -->
- <property name="dir.src.js" value="web/js"/><!-- JavaScript 源文件目录 -->
- <property name="dir.src.css" value="web/css"/><!-- CSS 源文件目录 -->
- <path id="path.build.classpath.yuicompress">
- <fileset dir="${dir.lib.yuicompress}">
- <include name="yuicompressor-2.4.2.jar"/>
- <include name="YUIAnt.jar"/>
- </fileset>
- </path>
- <target name="compres-js-css" description="压缩 .js 和 .css 文件">
- <taskdef name="compress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
- <classpath refid="path.build.classpath.yuicompress"/>
- </taskdef>
- <compress linebreak="150" warn="false" munge="yes"
- preserveallsemicolons="true" outputfolder="${dir.build.js}">
- <fileset dir="${dir.src.js}">
- <include name="**/*.js"/>
- </fileset>
- </compress>
- <compress linebreak="150" warn="false" munge="yes" charset="UTF-8"
- preserveallsemicolons="true" outputfolder="${dir.build.css}">
- <fileset dir="${dir.src.css}">
- <include name="**/*.css"/>
- </fileset>
- </compress>
- </target>
其中 <compress> 标签的 charset 参数的含义是指定输出文件的字符编码集。原版存在无法以指定字符编码集读取源文件的问题。所以我对此(com.yahoo.platform.yui.compressor.YUICompressTask)进行了改造。此改造方法为原创,经测试无误。
其实,原先的设计根本就没有考虑到源文件字符编码集的问题。首先我们为 <compress> 标签增加 encoding 这个属性,用来指定源文件的字符编码集。然后在读取文件的时候,用这个 Ant 构建文件中指定的 encoding 来打开文件输入流。所有改造都只针对 com/yahoo/platform/yui/compressor/YUICompressTask.Java 这一个文件。看了源文件,发现雅虎源代码的水平真是太不考究了……空格和 Tab 混用,行尾多余空白也不消除,空行也没有规范,注释也不指名调用顺序……不感叹了,下面是改写方法。
首先,要改变最开始的 import 部分。
原先的程序:
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
改为无误:
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
第二,属性确认方法要增加对 encoding 未指定的支持,并根据 YUI 官方的提议,修改 charset 默认值得逻辑。在 validate() 方法中修改。
原先的程序:
- /**
- *
- */
- private void validate() {
- if(charset==null)
- {
- charset = System.getProperty("file.encoding");
- if(charset == null)
- {
- charset = "UTF-8";
- }
- }
- this.munge = (this.munge != null) ? munge : Boolean.FALSE;
- this.lineBreak = (this.lineBreak==null) ? new Integer(-1) : this.lineBreak;
- }
改为:
- /**
- * Set attribute default value.
- * Modified by Shane Loo Li at 2012-4-4 Wednesday
- */
- private void validate() {
- if (this.charset == null)
- {
- //this.charset = System.getProperty("file.encoding");
- /*
- * Modified by Shane Loo Li at 2012-4-5 Thursday.
- * In YUI Compressor 2.4.7, The development team think that 'UTF-8' is better than local
- * charset for the output file.
- */
- this.charset = this.charset != null ? this.charset : "UTF-8";
- }
- if (this.encoding == null)
- {
- this.encoding = System.getProperty("file.encoding");
- this.encoding = this.encoding != null ? this.encoding : "UTF-8";
- }
- this.munge = (this.munge != null) ? munge : Boolean.FALSE;
- this.lineBreak = (this.lineBreak==null) ? new Integer(-1) : this.lineBreak;
- }
其中三目运算符优先级低于比较运算,高于赋值运算,刚好不用加括号。
第三,源文件 185 行是打开源文件以读取,原来是这么写的:
- if(inputFile.getAbsolutePath().equals(outputFile.getAbsolutePath()))
- {
- log("Input and Output file are the same, creating a copy");
- tempFile = File.createTempFile("temp",
- inputFile.getName().substring(inputFile.getName().lastIndexOf(".")));
- log("Copying "+inputFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
- copy(inputFile, tempFile);
- reader = new BufferedReader(new FileReader(tempFile));
- }
- else
- {
- reader = new BufferedReader(new FileReader(inputFile));
- }
改为:
- if(inputFile.getAbsolutePath().equals(outputFile.getAbsolutePath()))
- {
- log("Input and Output file are the same, creating a copy");
- tempFile = File.createTempFile("temp",
- inputFile.getName().substring(inputFile.getName().lastIndexOf(".")));
- log("Copying "+inputFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
- copy(inputFile, tempFile);
- // Modified by Shane Loo Li a 2012-4-4 Wednesday to support different source file charset.
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile), this.encoding));
- //reader = new BufferedReader(new FileReader(tempFile));
- }
- else
- {
- // Modified by Shane Loo Li a 2012-4-4 Wednesday to support different source file charset.
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), this.encoding));
- //reader = new BufferedReader(new FileReader(tempFile));
- }
这么更改是因为 FileReader 不提供用指定字符编码集读取,所以要换成别的打开方式。
第四,在文件前边有对象成员变量声明,增加
- private String encoding;
在文件后边有一组 getter 和 setter ,增加
- /**
- * @return the encoding
- */
- public String getEncoding() {
- return this.encoding;
- }
- /**
- * @param set the source file encoding
- */
- public void setEncoding(String encoding) {
- this.encoding = encoding;
- }
然后就可以了,编译一下,将编译出来的主 .class 替换掉原来 .jar 包中的 .class 文件,就可以用了。
以下提供源代码、.class 和 .jar 都改动了的合集。通过 CSDN 下载站上传。
http://download.csdn.net/detail/shanelooli/4200449
参考资料
用 Ant 调用 YUI Compressor : http://www.iteye.com/topic/368724
源文件字符集写死成 UTF-8 改造: http://moly.iteye.com/blog/718122
Ant 执行 YUICompressor的更多相关文章
- 使用Ant和YUICompressor链接合并压缩你的JS和CSS代码
JS代码和CSS代码在上线前要压缩大家应该都是知道的了.记得之前做项目的时候,最后要交差的时候是找了个网站,将JS代码的文件一个一个地复制,粘贴,复制,粘贴. 当时就在想:TMD有没有好一点的方法,劳 ...
- Mac上使用jenkins+ant执行第一个程序
本文旨在让同学们明白如何让jenkis在mac笔记本上运行,以模拟实际工作中在linux上搭建jenkins服务平台首先按照笔者的习惯先说一下如何安装jenkis和tomcat,先安装tomcat,在 ...
- Jenkins+Jmeter持续集成笔记(二:ANT执行Jmeter脚本)
Jmeter接口测试脚本运行后生成的是jtl(xml)格式的文件,这些文件不具备可读性,所以我们要把他转化为可以阅读的html格式报告. Ant是一个功能强大的打包编译工具.我们使用他的目的是将xml ...
- 配置Ant执行Jmeter脚本
1.将 jmeter下extras目录中ant-jmeter-1.1.1.jar包拷贝至ant安装目录下的lib目录中,否则会报错ant-jmeter-1.1.1不存在 2.在jmeter根目录下创建 ...
- 利用ant 执行jmeter用例生成html格式报告
1.安装ant 2.准备jmeter 及用例文件.jmx 3.编辑ant 执行文件build.xml <?xml version="1.0" encoding="G ...
- 配置 Ant 执行 Jmeter 脚本
1.将 Jmeter 下 extras 目录中 ant-jmeter-1.1.1.jar 包拷贝至 ant 安装目录下的lib目录中,否则会报错 ant-jmeter-1.1.1 不存在 2.创建 ...
- Ant执行一个含有main方法的class文件
目前需要使用ant来执行一个含有main方法的class文件,并且需要通过命令来行传两个参数(start和end)到main方法. <target name="gsp" de ...
- ant 执行jmeter脚本
环境准备 1.jdk版本:java version "1.8.0_201" 2.jmeter版本:5.0 3.ant版本:Apache Ant(TM) version 1.10.5 ...
- Ant执行Jmeter工程模版
<?xml version="1.0" encoding="GB2312"?><project name="ant-jmeter-t ...
随机推荐
- myeclipse10安装findbugs
尝试过myeclipse10环境下,在线安装findbugs,插件包是能下载到指定目录下,可是由于版本问题,findbugs插件是不能使用的.所以才有了下面的离线安装 离线安装findbugs 操作系 ...
- 【转】初识CGI
一.基本原理 CGI:通用网关接口(Common Gateway Interface)是一个Web服务器主机提供信息服务的标准接口.通过CGI接口,Web服务器就能够获取客户端提交的信息,转交给服务器 ...
- nginx安装过程,报错处理:make[1]: *** [objs/addon/src/bson.o] Error 1
nginx安装过程中,经常会有各种错误: 具体安装步骤这里不做说明,网上一搜大把: 主要分析安装过程中遇到的问题 在make编译的时候,若报如下错误: cc1: warnings being trea ...
- 测试管理_测试人员招聘[持续更新ing]
招聘之难,难于上青天. 如何招聘到一位称心如意的员工想必是每个公司和管理者都要面临而且头疼的问题.尤其在初建团队或团队缺人的情况下问题会显得更加严重. 作为一个测试管理者,如何招聘到合适的测试人员是必 ...
- Mongo 的相关增删改查
1,Mongod 下载安装,请看https://www.mongodb.com/download-center 2,插入语句: MongoDB后台管理 Shell 如果你需要进入MongoDB后台管理 ...
- linux下mysql安装、目录结构、配置
1.准备安装程序(官方网站下载) 服务端:MySQL-server-community-5.1.44-1.rhel4.i386.rpm 客户端:MySQL-client-community-5.1.4 ...
- mini2440的SDRAM分析
首先是2440的存储控制器: 暂时不管是从nand启动还是nor启动,因为我现在只关注内存,从上图可以看到由2440的Memory Controller可以寻址的范围是0x0000,0000---0x ...
- common-pool2对象池(连接池)的介绍及使用
我们在服务器开发的过程中,往往会有一些对象,它的创建和初始化需要的时间比较长,比如数据库连接,网络IO,大数据对象等.在大量使用这些对象时,如果不采用一些技术优化,就会造成一些不可忽略的性能影响.一种 ...
- NOIP2008提高组火柴棒等式(模拟)——yhx
题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 加号与等号各自 ...
- 合工大OJ 1337 一加二减三
Description 题目描述:给一个串,形如一+二-三,求值 Input 第一行为一个正整数T,表示数据的组数,接下来有T行每行都是一个形如一+二-三的串,一,二,三均为正整数 Output 对于 ...