如果你不知道UglifyJS就out了,jquery的官方压缩工具啦~
本来UglifyJS没有windows的客户端压缩工具,后来自己参照YUI的BAT工具(好像是淘宝一个哥们写的)改写了一个,具体见下面的说明。
REM ===================================== REM YUI Compressor CMD Script REM REM - by yubo@taobao.com REM - 2009-02-12 REM =====================================
自己修改后的:
UglifyJS 安装指南 ========= 安装步骤: 0. 安装nodejs运行环境:http://nodejs.org/,设置nodejs的安装目录到PATH中,一句话CMD中输入 node --version: --->C:\Users\xin>node --version --->v0.6.15 1. 安装请点击 install.cmd 2. 卸载请点击 uninstall.cmd 3. 如果安装过之前的版本,请先卸载老版本 压缩测试: 选中 test.js, 执行右键菜单“Process with UglifyJS”,会生成 test-min.js.
注意需要nodejs环境,怎么安装?自己谷歌吧,还有jsp里面的script标签要标准哦,只能是:"<script>"和'<script type="text/javascript">',其他的自己去写吧,text.indexOf('<script type="text/javascript">')
当然我觉得这个工具最好能支持压缩jsp里的js代码啦,所以自己看了下bin下面的uglifyjs文件,无非就是一些js的函数,找到了修改代码的切入点:output(squeeze_it(text)),接下来就好办了,下图为修改前后的对比:


最后看看实际效果图吧:

最后附上maven下面的压缩脚本:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>copy-config</id> <goals> <goal>copy-resources</goal> </goals> <phase>compile</phase> <configuration> <outputDirectory>${project.build.directory}</outputDirectory> <resources> <resource> <directory>src/test/resources</directory> <includes> <include>uglify/*</include> <include>uglify/lib/*</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <phase>install</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <echo message="开始用uglifyjs压缩 JSP 文件. 请耐心等待!" /> <property name="uglifyjs" value="${project.build.directory}/uglify/uglifyjs" /> <property name="jsp.compress.directory" value="${project.build.directory}/${project.build.finalName}/WEB-INF" /> <apply executable="node" parallel="false" failonerror="true" dest="${jsp.compress.directory}"> <fileset dir="${jsp.compress.directory}" includes="**/*.jsp" /> <arg line=" ${uglifyjs}" /> <arg line=" --ascii" /> <arg line=" -o" /> <targetfile /> <srcfile /> <mapper type="regexp" from="^(.*)\.(jsp)$" to="\1-min.\2" /> </apply> <move todir="${jsp.compress.directory}"> <fileset dir="${jsp.compress.directory}" includes="**/*.jsp" /> <mapper type="regexp" from="^(.*)-min\.(jsp)$" to="\1.\2" /> </move> <echo message="成功用uglifyjs压缩 JSP 文件" /> <echo message="开始用uglifyjs压缩 JS 文件. 请耐心等待!" /> <property name="uglifyjs" value="${project.build.directory}/uglify/uglifyjs" /> <property name="js.compress.directory" value="${project.build.directory}/${project.build.finalName}/resources/js" /> <apply executable="node" parallel="false" failonerror="true" dest="${js.compress.directory}"> <fileset dir="${js.compress.directory}" includes="**/*.js" /> <arg line=" ${uglifyjs}" /> <arg line=" --ascii" /> <arg line=" -o" /> <targetfile /> <srcfile /> <mapper type="regexp" from="^(.*)\.(js)$" to="\1-min.\2" /> </apply> <move todir="${js.compress.directory}"> <fileset dir="${js.compress.directory}" includes="**/*.js" /> <mapper type="regexp" from="^(.*)-min\.(js)$" to="\1.\2" /> </move> <echo message="成功用uglifyjs压缩 JS 文件" /> </target> </configuration> </execution> </executions> </plugin> <!-- CSS压缩 --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>yuicompressor-maven-plugin</artifactId> <version>1.3.0</version> <executions> <execution> <phase>install</phase> <goals> <goal>compress</goal> </goals> <configuration> <includes> <include>**/*.css</include> </includes> <nosuffix>true</nosuffix><!-- 不需要后缀 --> <linebreakpos>-1</linebreakpos><!-- 是否在一行 --> </configuration> </execution> </executions> </plugin>
1 楼
白云飞
2012-05-22
大神啊,好东西,下来看看
2 楼
Yrain
2012-05-29
悲剧,搞了两天了,压缩后的文件始终不会自动打包到War里去。
3 楼
matychen
2012-05-29
Yrain 写道
悲剧,搞了两天了,压缩后的文件始终不会自动打包到War里去。
呵呵,是有这个问题,自己手动打包下就可以了。war就是个zip包。
4 楼
matychen
2012-05-29
Yrain 写道
悲剧,搞了两天了,压缩后的文件始终不会自动打包到War里去。
可以修改下这个吧。
<phase>install</phase>
5 楼
Yrain
2012-05-29
matychen 写道
Yrain 写道
悲剧,搞了两天了,压缩后的文件始终不会自动打包到War里去。
可以修改下这个吧。
<phase>install</phase>
试过了。还是不行,目前还在找资料解决。
6 楼
matychen
2012-05-29
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>copy-config</id> <goals> <goal>copy-resources</goal> </goals> <phase>compile</phase> <configuration> <outputDirectory>${project.build.directory}</outputDirectory> <resources> <resource> <directory>src/test/resources</directory> <includes> <include>uglify/*</include> <include>uglify/lib/*</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution><!-- 打包之前做 --> <phase>prepare-package</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <echo message="开始用uglifyjs压缩 JSP 文件. 请耐心等待!" /> <property name="uglifyjs" value="${project.build.directory}/uglify/uglifyjs" /> <property name="jsp.compress.out.directory" value="${project.build.directory}/${project.build.finalName}/WEB-INF" /> <property name="jsp.compress.directory" value="src/main/webapp/WEB-INF" /> <apply executable="node" parallel="false" failonerror="true" dest="${jsp.compress.directory}"> <fileset dir="${jsp.compress.directory}" includes="**/*.jsp" /> <arg line=" ${uglifyjs}" /> <arg line=" --ascii" /> <arg line=" -o" /> <targetfile /> <srcfile /> <mapper type="regexp" from="^(.*)\.(jsp)$" to="\1-min.\2" /> </apply> <move todir="${jsp.compress.out.directory}"> <fileset dir="${jsp.compress.directory}" includes="**/*-min.jsp" /> <mapper type="regexp" from="^(.*)-min\.(jsp)$" to="\1.\2" /> </move> <echo message="成功用uglifyjs压缩 JSP 文件" /> <echo message="开始用uglifyjs压缩 JS 文件. 请耐心等待!" /> <property name="uglifyjs" value="${project.build.directory}/uglify/uglifyjs" /> <property name="js.compress.out.directory" value="${project.build.directory}/${project.build.finalName}/resources/js" /> <property name="js.compress.directory" value="src/main/webapp/resources/js" /> <apply executable="node" parallel="false" failonerror="true" dest="${js.compress.directory}"> <fileset dir="${js.compress.directory}" includes="**/*.js" /> <arg line=" ${uglifyjs}" /> <arg line=" --ascii" /> <arg line=" -o" /> <targetfile /> <srcfile /> <mapper type="regexp" from="^(.*)\.(js)$" to="\1-min.\2" /> </apply> <move todir="${js.compress.out.directory}"> <fileset dir="${js.compress.directory}" includes="**/*-min.js" /> <mapper type="regexp" from="^(.*)-min\.(js)$" to="\1.\2" /> </move> <echo message="成功用uglifyjs压缩 JS 文件" /> </target> </configuration> </execution> </executions> </plugin> <plugin> <!-- 打war包插件 --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <!-- 声明:packagingExcludes中的*.properties文件均位于src/main/resources目录中 warSourceExcludes中的?test/*,venue/**位于src/main/webapp目录中 --> <warSourceExcludes>resources/js/**/*.js,WEB-INF/views/**/*.jsp</warSourceExcludes> <archive> <!-- 不把maven的依赖加入 --> <addMavenDescriptor>false</addMavenDescriptor> </archive> </configuration> </plugin> <!-- 思路:打包直接把js和jsp压缩后复制到目的地,在war插件的复制里面,排除刚刚复制的js和jsp,这样就不会覆盖了。打包的时候也能打进去了。 -->