`
iwebcode
  • 浏览: 2009606 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Google Closure Compiler+Ant压缩文件(Js)

 
阅读更多

一、在命令行中压缩
首先下载并解压 closure java application。
下载地址: http://closure-compiler.googlecode.com/files/compiler-latest.zip

在linux系统上,使用wget进行下载:(在google code中直接查询Closure Compile)

#wget http://closure-compiler.googlecode.com/files/compiler-latest.zip

#unzip compiler-latest.zip

得到文件 compiler.jar。

再确认一下java是否已经安装好了。

这个compiler.jar的使用方式如下:(在命令行执行)

java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js

或者java -jar compiler.jar --js=in1.js --js=in2.js ... --js_output_file=out.js

这样就把 hello.js 压缩成了 hello-compiled.js。

更多信息请看帮助:

java -jar compiler.jar --help


二、Closure Compiler+Ant使用(编译多个文件更方便)

Ant:http://ant.apache.org/

以下是例子:

<?xml version="1.0" encoding="UTF-8"?>
<project name="today" default="all" basedir="."><!-- '.'表示当前目录-->
	<!---------以下定义变量 -------->
	<property name="base" value=".."></property>
	<property name="jsFolder" value="${base}/teach_left_main"></property>
	<property name="cfolder" value="mypress"></property>
	<property name="mjs" value="${jsFolder}/${cfolder}/megre.js"></property>
	<property name="cjs" value="${jsFolder}/${cfolder}/c.js"></property>
	
	<!-------------------------------------------------------------
		三步骤:1.新建目录 2.合并多个JS 3.再压缩
		target createF 是创建等下要存放合并的js和压缩后的js
		target megerJS 把多个js合并起来,放到你创建的文件夹中
		target CompressJs 把合并好的js进行压缩
		depends标签表示依赖,要先走createF,COmpressJs才会走all
		${name}里面对应定义的变量
	--------------------------------------------------------------->
	<target name="all" depends="createF,CompressJs"></target>
	<!--<target name="all" depends="createF,megerJS,CompressJs"></target>-->
	<target name="createF">
		<echo message="start createF" />
			<mkdir dir="${jsFolder}/${cfolder}" />
		<echo message="end createF" />
	</target>
	
	<!-------------------------------------------------
	当然,第二步合并js,你也可以不用,直接压缩
	--------------------------------------------------->
	<!--
	<target name="megerJS">
			<echo message="start megerJS" />
			<concat destfile="${mjs}" encoding="UTF-8">
				<path path="${jsFolder}/core.js" /> 
				<path path="${jsFolder}/lookup_dis.js" /> 
				<path path="${jsFolder}/1.js" /> 
			</concat>
			<echo message="end megerJS"/>
	</target>
	-->
	<target name="CompressJs">
		<echo message="start CompressJs" />
			<java jar="compiler.jar" fork="true">
				<!--<arg value="--js=${mjs}" />  这里则变成导入所有的js-->
					<arg value="--js=${jsFolder}/base.js" />
					<arg value="--js=${jsFolder}/lookup_dis.js" />
					<arg value="--js=${jsFolder}/1.js" />
				<arg value="--js_output_file=${cjs}"/>
			</java>
		<echo message="end CompressJs" />
	</target>
</project>

以下是目录结构,上面的变量base则表示是WebRoot目录


更多查看:

http://lds2008.blogbus.com/logs/115112756.html

http://blog.csdn.net/hanzhou4519/article/details/7924588

http://foxling.org/js-ajax-dom/in-eclipse-use-ant-concat-compress/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics