Ant 执行 YUICompressor

Ant 执行 YUICompressorAnt执行YUICompressor任务压缩JavaScript和CSS文件,解决中文乱码问题,增加源文件字符编码集设定标签:javascriptantcss任务encodingnull2012-04-0510:465376人阅读评论(4)收藏举报分类:Java(14)Ant版权声明:本文为博主原创文章,未经博主允许…

大家好,又见面了,我是你们的朋友全栈君。

 

Ant 执行 YUICompressor 任务压缩 JavaScript 和 CSS 文件,解决中文乱码问题,增加源文件字符编码集设定

标签: javascriptantcss任务encodingnull

Ant 执行 YUICompressor 分类:
Java(14) 
Ant 执行 YUICompressor 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)中,可以参考如下例子来引入。

[html] 
view plain  
copy

 

 
print
?

  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.   
  7. <path id=“path.build.classpath.yuicompress”>  
  8.     <fileset dir=“${dir.lib.yuicompress}”>  
  9.         <include name=“yuicompressor-2.4.2.jar”/>  
  10.         <include name=“YUIAnt.jar”/>  
  11.     </fileset>  
  12. </path>  
  13.   
  14. <target name=“compres-js-css” description=“压缩 .js 和 .css 文件”>    
  15.     <taskdef name=“compress” classname=“com.yahoo.platform.yui.compressor.YUICompressTask”>  
  16.         <classpath refid=“path.build.classpath.yuicompress”/>  
  17.     </taskdef>  
  18.     <compress linebreak=“150” warn=“false” munge=“yes”  
  19.             preserveallsemicolons=“true” outputfolder=“${dir.build.js}”>  
  20.         <fileset dir=“${dir.src.js}”>  
  21.             <include name=“**/*.js”/>  
  22.         </fileset>  
  23.     </compress>  
  24.     <compress linebreak=“150” warn=“false” munge=“yes” charset=“UTF-8”  
  25.             preserveallsemicolons=“true” outputfolder=“${dir.build.css}”>  
  26.         <fileset dir=“${dir.src.css}”>  
  27.             <include name=“**/*.css”/>  
  28.         </fileset>  
  29.     </compress>  
  30. </target>  

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

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

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

[java] 
view plain  
copy

 

 
print
?

  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;  

改为无误:

[java] 
view plain  
copy

 

 
print
?

  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() 方法中修改。
原先的程序:

[java] 
view plain  
copy

 

 
print
?

  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.   
  14.     this.munge = (this.munge != null) ? munge : Boolean.FALSE;  
  15.     this.lineBreak = (this.lineBreak==null) ? new Integer(-1) : this.lineBreak;       
  16. }  

改为:

[java] 
view plain  
copy

 

 
print
?

  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 行是打开源文件以读取,原来是这么写的:

[java] 
view plain  
copy

 

 
print
?

  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. }  

改为:

[java] 
view plain  
copy

 

 
print
?

  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 不提供用指定字符编码集读取,所以要换成别的打开方式。

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

[java] 
view plain  
copy

 

 
print
?

  1. private String encoding;  

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

[java] 
view plain  
copy

 

 
print
?

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161202.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android string.xml文字换行、空格等样式(转义字符)

    Android string.xml文字换行、空格等样式(转义字符)

    2021年10月1日
    158
  • 基于ZigBee的工业废气监测系统

    基于ZigBee的工业废气监测系统摘要本文主要对工业现场中排放的工业废气浓度进行检测。并根据国内外气体监测技术的发展现状,提出了基于ZigBee的工业废气监测系统的设计方案。本系统主要由单片机、气体浓度检测模块、模数转换模块、显示模块、按键、蜂鸣器和无线通信模块等组成。本文首先介绍了工业废气检测系统的研究背景意义,同时结合国内外气体检测技术的发展现状,提出了基于ZigBee的工业废气监测…

    2022年4月14日
    58
  • 手眼标定理解「建议收藏」

    手眼标定理解「建议收藏」参考:https://blog.csdn.net/yaked/article/details/77161160?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.control什么是手眼标定手眼标定是为了得到相机坐标系

    2022年5月1日
    35
  • 极限思想之芝诺悖论[通俗易懂]

    极限思想之芝诺悖论[通俗易懂]芝诺悖论是古希腊哲学家芝诺提出的一组悖论。芝诺是一个很有学问,同时也很好玩的人(淘气)。他如果在中国出生,估计很难大学毕业,只能跟池子(脱口秀演员~)一样,高中教室门外面站三年课,然后去讲脱口秀糊口。阿基里斯,大家都知道。古希腊神话中的战神。无论是力量,速度,耐力,格斗技巧,都是巅峰级别的。一夜睡三女,第二天依然可以血染特洛伊的男人。芝诺就提出:在跑步比赛中,如果跑得最慢的乌龟一开始领先…

    2022年6月18日
    35
  • listView1.SelectedItems选中行要注意count>0[通俗易懂]

    listView1.SelectedItems选中行要注意count>0[通俗易懂]在右边的ListView中选中一行,就把选中行的第二列里的值显示在textBox里 。但是当我第一次选择一行时没有什么问题,当我第二次选择一行时就出现下面的错误:未处理ArgumentOutOfRangeException InvalidArgument=“0”的值对于“index”无效。  参数名:index上网查找说是要加一句判断if(listView1.

    2022年7月12日
    13
  • Word2vec原理及其Python实现「建议收藏」

    Word2vec原理及其Python实现「建议收藏」目录一、为什么需要WordEmbedding二、Word2vec原理1、CBOW模型2、Skip-gram模型三、行业上已有的预训练词向量四、用Python训练自己的Word2vec词向量一、为什么需要WordEmbedding在NLP(自然语言处理)里面,最细粒度的是词语,词语组成句子,句子再组成段落、篇章、文档。所以要处理NLP的问题,首先就要拿词语开刀…

    2022年5月17日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号