2012-04-05 10:46 5376人阅读 评论(4) 收藏 举报
 分类:
Java(14)  Ant

版权声明:本文为博主原创文章,未经博主允许不得转载。

发布 JavaScript 的时候,无论从代码保护还是提高性能角度,都应该对代码进行压缩,去除重叠的空白分隔符,混淆变量。雅虎交互(YUI)提供了非常强大的压缩工具,对 .js 文件和 .css 文件都有效。这里所说明的情况,是用 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)中,可以参考如下例子来引入。

  1. <property name="dir.lib.yuicompress" value="lib"/><!-- 存放 YUI Compress 二个 .jar 文件的目录 -->
  2. <property name="dir.build.js" value="dist/webapp/js"/><!-- 存放压缩过的 JavaScript 文件目录 -->
  3. <property name="dir.build.css" value="dist/webapp/css"/><!-- 存放压缩过的 CSS 文件目录 -->
  4. <property name="dir.src.js" value="web/js"/><!-- JavaScript 源文件目录 -->
  5. <property name="dir.src.css" value="web/css"/><!-- CSS 源文件目录 -->
  6. <path id="path.build.classpath.yuicompress">
  7. <fileset dir="${dir.lib.yuicompress}">
  8. <include name="yuicompressor-2.4.2.jar"/>
  9. <include name="YUIAnt.jar"/>
  10. </fileset>
  11. </path>
  12. <target name="compres-js-css" description="压缩 .js 和 .css 文件">
  13. <taskdef name="compress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
  14. <classpath refid="path.build.classpath.yuicompress"/>
  15. </taskdef>
  16. <compress linebreak="150" warn="false" munge="yes"
  17. preserveallsemicolons="true" outputfolder="${dir.build.js}">
  18. <fileset dir="${dir.src.js}">
  19. <include name="**/*.js"/>
  20. </fileset>
  21. </compress>
  22. <compress linebreak="150" warn="false" munge="yes" charset="UTF-8"
  23. preserveallsemicolons="true" outputfolder="${dir.build.css}">
  24. <fileset dir="${dir.src.css}">
  25. <include name="**/*.css"/>
  26. </fileset>
  27. </compress>
  28. </target>

其中 <compress> 标签的 charset 参数的含义是指定输出文件的字符编码集。原版存在无法以指定字符编码集读取源文件的问题。所以我对此(com.yahoo.platform.yui.compressor.YUICompressTask)进行了改造。此改造方法为原创,经测试无误。

其实,原先的设计根本就没有考虑到源文件字符编码集的问题。首先我们为 <compress> 标签增加 encoding 这个属性,用来指定源文件的字符编码集。然后在读取文件的时候,用这个 Ant 构建文件中指定的 encoding 来打开文件输入流。所有改造都只针对 com/yahoo/platform/yui/compressor/YUICompressTask.Java 这一个文件。看了源文件,发现雅虎源代码的水平真是太不考究了……空格和 Tab 混用,行尾多余空白也不消除,空行也没有规范,注释也不指名调用顺序……不感叹了,下面是改写方法。

首先,要改变最开始的 import 部分。
原先的程序:

  1. import java.io.FileOutputStream;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;

改为无误:

  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;

第二,属性确认方法要增加对 encoding 未指定的支持,并根据 YUI 官方的提议,修改 charset 默认值得逻辑。在 validate() 方法中修改。
原先的程序:

  1. /**
  2. *
  3. */
  4. private void validate() {
  5. if(charset==null)
  6. {
  7. charset = System.getProperty("file.encoding");
  8. if(charset == null)
  9. {
  10. charset = "UTF-8";
  11. }
  12. }
  13. this.munge = (this.munge != null) ? munge : Boolean.FALSE;
  14. this.lineBreak = (this.lineBreak==null) ? new Integer(-1) : this.lineBreak;
  15. }

改为:

  1. /**
  2. * Set attribute default value.
  3. * Modified by Shane Loo Li at 2012-4-4 Wednesday
  4. */
  5. private void validate() {
  6. if (this.charset == null)
  7. {
  8. //this.charset = System.getProperty("file.encoding");
  9. /*
  10. * Modified by Shane Loo Li at 2012-4-5 Thursday.
  11. * In YUI Compressor 2.4.7, The development team think that 'UTF-8' is better than local
  12. * charset for the output file.
  13. */
  14. this.charset = this.charset != null ? this.charset : "UTF-8";
  15. }
  16. if (this.encoding == null)
  17. {
  18. this.encoding = System.getProperty("file.encoding");
  19. this.encoding = this.encoding != null ? this.encoding : "UTF-8";
  20. }
  21. this.munge = (this.munge != null) ? munge : Boolean.FALSE;
  22. this.lineBreak = (this.lineBreak==null) ? new Integer(-1) : this.lineBreak;
  23. }

其中三目运算符优先级低于比较运算,高于赋值运算,刚好不用加括号。

第三,源文件 185 行是打开源文件以读取,原来是这么写的:

  1. if(inputFile.getAbsolutePath().equals(outputFile.getAbsolutePath()))
  2. {
  3. log("Input and Output file are the same, creating a copy");
  4. tempFile = File.createTempFile("temp",
  5. inputFile.getName().substring(inputFile.getName().lastIndexOf(".")));
  6. log("Copying "+inputFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
  7. copy(inputFile, tempFile);
  8. reader = new BufferedReader(new FileReader(tempFile));
  9. }
  10. else
  11. {
  12. reader = new BufferedReader(new FileReader(inputFile));
  13. }

改为:

  1. if(inputFile.getAbsolutePath().equals(outputFile.getAbsolutePath()))
  2. {
  3. log("Input and Output file are the same, creating a copy");
  4. tempFile = File.createTempFile("temp",
  5. inputFile.getName().substring(inputFile.getName().lastIndexOf(".")));
  6. log("Copying "+inputFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
  7. copy(inputFile, tempFile);
  8. // Modified by Shane Loo Li a 2012-4-4 Wednesday to support different source file charset.
  9. reader = new BufferedReader(new InputStreamReader(new FileInputStream(tempFile), this.encoding));
  10. //reader = new BufferedReader(new FileReader(tempFile));
  11. }
  12. else
  13. {
  14. // Modified by Shane Loo Li a 2012-4-4 Wednesday to support different source file charset.
  15. reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), this.encoding));
  16. //reader = new BufferedReader(new FileReader(tempFile));
  17. }

这么更改是因为 FileReader 不提供用指定字符编码集读取,所以要换成别的打开方式。

第四,在文件前边有对象成员变量声明,增加

  1. private String encoding;

在文件后边有一组 getter 和 setter ,增加

  1. /**
  2. * @return the encoding
  3. */
  4. public String getEncoding() {
  5. return this.encoding;
  6. }
  7. /**
  8. * @param set the source file encoding
  9. */
  10. public void setEncoding(String encoding) {
  11. this.encoding = encoding;
  12. }

然后就可以了,编译一下,将编译出来的主 .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的更多相关文章

  1. 使用Ant和YUICompressor链接合并压缩你的JS和CSS代码

    JS代码和CSS代码在上线前要压缩大家应该都是知道的了.记得之前做项目的时候,最后要交差的时候是找了个网站,将JS代码的文件一个一个地复制,粘贴,复制,粘贴. 当时就在想:TMD有没有好一点的方法,劳 ...

  2. Mac上使用jenkins+ant执行第一个程序

    本文旨在让同学们明白如何让jenkis在mac笔记本上运行,以模拟实际工作中在linux上搭建jenkins服务平台首先按照笔者的习惯先说一下如何安装jenkis和tomcat,先安装tomcat,在 ...

  3. Jenkins+Jmeter持续集成笔记(二:ANT执行Jmeter脚本)

    Jmeter接口测试脚本运行后生成的是jtl(xml)格式的文件,这些文件不具备可读性,所以我们要把他转化为可以阅读的html格式报告. Ant是一个功能强大的打包编译工具.我们使用他的目的是将xml ...

  4. 配置Ant执行Jmeter脚本

    1.将 jmeter下extras目录中ant-jmeter-1.1.1.jar包拷贝至ant安装目录下的lib目录中,否则会报错ant-jmeter-1.1.1不存在 2.在jmeter根目录下创建 ...

  5. 利用ant 执行jmeter用例生成html格式报告

    1.安装ant 2.准备jmeter 及用例文件.jmx 3.编辑ant 执行文件build.xml <?xml version="1.0" encoding="G ...

  6. 配置 Ant 执行 Jmeter 脚本

    1.将  Jmeter 下 extras 目录中 ant-jmeter-1.1.1.jar 包拷贝至 ant 安装目录下的lib目录中,否则会报错 ant-jmeter-1.1.1 不存在 2.创建 ...

  7. Ant执行一个含有main方法的class文件

    目前需要使用ant来执行一个含有main方法的class文件,并且需要通过命令来行传两个参数(start和end)到main方法. <target name="gsp" de ...

  8. ant 执行jmeter脚本

    环境准备 1.jdk版本:java version "1.8.0_201" 2.jmeter版本:5.0 3.ant版本:Apache Ant(TM) version 1.10.5 ...

  9. Ant执行Jmeter工程模版

    <?xml version="1.0" encoding="GB2312"?><project name="ant-jmeter-t ...

随机推荐

  1. 【转载】小米2进入recovery的方法

    用过M1的朋友都多多少少的了解到~进入recovery是关机下同时 按 音量(+)+电源键. 其实M2也一样,但是我觉得是有点区别的. 在M1的时候,只要同时长按这两个键就可以的了. 但是M2呢?我发 ...

  2. Centos 下oracle 11g 安装部署及手动建库过程

    Oracle 11g 手动建库,在虚拟环境中,不使用DBCA工具进行创建数据库 1.Linux环境的基本配置 2.ip  10.11.30.60 3.Oracle 11g安装过程 ---------- ...

  3. 第1章 Java中常用字符串方法总结

    1.1 charAt方法——提取指定字符 1.2 codePointAt方法——提取索引字符代码点 1.3 codePointBefore方法——获取索引前一个字符的代码点 1.4 codePoint ...

  4. ELK 信息统计分析-1

    Aggregations 格式如下: "aggregations"{ //可以简写为aggs "<aggregation_name>":{ //名称 ...

  5. 04_最长上升子序列问题(LIS)

    来源:刘汝佳<算法竞赛入门经典--训练指南> P60 问题6: 问题描述:给定n个整数a1,a2,...,an,按从左到右的顺序选出尽量多的整数,组成一个上升子序列(子序列可以理解为:删除 ...

  6. maven添加远程私服

    文件放在maven下和..m2下面 <?xml version="1.0" encoding="UTF-8"?>   <!-- License ...

  7. Linux 安装 redis

      环境:centos7 参考:http://blog.csdn.net/lk10207160511/article/details/50364088 步骤如下: 安装redis: 打开终端 输入 s ...

  8. xamarin.android 给View控件 添加数字提醒效果-BadgeView

    本文代码从java项目移植到.net项目   java开源项目:https://github.com/jgilfelt/android-viewbadger using System; using S ...

  9. 搭建docker私有仓库,建立k8s集群

    服务器IP角色分布 192.168.5.2 etcd server 192.168.5.2 kubernetes master 192.168.5.3 kubernetes node 192.168. ...

  10. HDU 4409 Family Name List --乱搞、LCA

    题意: 给出一些名字,名字间有父子关系,有三种操作: 1.按祖先到后代,兄弟间按字典序由小到大排序,然后输出 2.求某个节点的兄弟节点有多少个,包括自己(注意,根节点的兄弟节点是1) 3.求节点a和b ...