Ant 执行 YUICompressor
Ant 执行 YUICompressor 任务压缩 JavaScript 和 CSS 文件,解决中文乱码问题,增加源文件字符编码集设定
版权声明:本文为博主原创文章,未经博主允许不得转载。
首先,需要准备二个 .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
转载于:https://www.cnblogs.com/developer-ios/p/6047262.html
Ant 执行 YUICompressor相关推荐
- ant学习笔记之(ant执行命令的详细参数和Ant自带的系统属性)
2019独角兽企业重金招聘Python工程师标准>>> 一:ant执行命令的详细参数 -buildfile<file>,-file<file>,-f<f ...
- jmeter脚本结合ant执行测试用例并生成测试报告
前言:本篇主要讲ant执行测试用例并生成测试报告,至于jmeter脚本本篇文章不多说,不懂的小伙伴可查看我的上篇文章:jmeter环境配置.使用以及参数化之CSV Data Set Config 1. ...
- [Ant自动打包]ant执行命令的详细参数和Ant自带的系统属性
转自:http://hi.baidu.com/yuanzhifeng/item/75d03d11e9b2010eb88a1a6d?qq-pf-to=pcqq.c2c ant学习笔记之(ant执行命令的 ...
- ant 执行多个构建文件
如果需要在一个构件文件中通过Ant命令去执行另一个Ant项目,或者说要执行本项目的子项目的构件文件.这时Ant这个核心任务就派上用场了.简单地说Ant任务的作用就相当于在命令行通过Ant命令执行构件文 ...
- 用ANT执行Jmeter脚本时提示jtl文件不存在
1.在cmd中输入ant -buildfile D:\apache-ant-1.10.5\bin\build.xml 2.ANT构建时提示jtl does not exist: 3.Google上查找 ...
- 使用ant执行Java代码
一个小陷阱:如果用tomcat manager应用,即html页面点击reload,发起的HTTP请求的url:http://localhost:9032/manager/html/reload;js ...
- jmeter通过ant执行时报错 jmeter.log not found
原因:权限执行不够,改为root用户即可 :sudo su 日志报错如下: test:[jmeter] Executing test plan: /home/ec2-user/jmeterProgr ...
- 使用ant直接执行shell命令
可以使用ant执行执行简单的shell命令,代码如下(本人亲测): <?xml version="1.0" ?> <project name="uplo ...
- 使用 Ant 自动生成项目构建版本
引言 对 于多版本项目,要提供新版本来跟上新功能或缺陷报告增加的速度,并同时仍然保持可接受的质量水平,可能是一项不小的挑战.构建自动化可确保准确性和消除人 为错误的可能性,从而部分地解决此问题.自动化 ...
- jenkins中配置Ant
一.安装ant 官方主页http://ant.apache.org下载新版的ant. *下载对应的版本,解压到我们的硬盘. 二.配置环境变量 Window中设置ant环境变量: ANT_HOME ...
最新文章
- AndroidStudio git 提交代码,创建分支,合并分支,回滚版本,拉取代码
- 【一步步学小程序】3. 使用自定义组件(component)
- Python基础之逻辑运算符
- 8、使用元数据(描述数据属性的信息)分析数据库
- 关于运维自动化工具saltstack的教程更新页面
- 斐波那契 (Standard IO)
- linux只允许从ttyS0设备登录,linux自动以root登录,并自动启动用户程序的设置方...
- 要闻君说:IBM最新量子计算机真真像个艺术品!鹅厂正式成立了自己的技术委员会哇!联想竟然也试着做了一款智能闹钟?...
- java作业 雏田的两个技能 类与对象
- Browser-Bookmark-Codeing
- 深入理解DOM事件机制系列第四篇——事件模拟
- wireshark找不到接口_下水管漏水,维修师傅看一眼就收了200,自己换其实不到10块...
- zabbix使用JMX监控tomcat性能
- teigha开发从入门到精通(3)-- 编译drawings sdk示例
- ggplot2柱状图进阶画法
- 【内容详细、源码详尽】MySQL极简学习笔记
- Android电视清理系统应用,【教程】无需root!卸载小米电视/盒子内置应用竟如此简单...
- C语言宏定义-跟踪调试宏
- 基于S3C2410A的MDB/ICP协议的实现
- mysql唯一索引什么意思_MySQL唯一索引什么意思