---
title: "ANT压缩JS"
author: "三问飞絮"
date: "2015-08-09T23:38:41Z"
tags: []
categories: ["Web前端"]
slug: "ant-js"
draft: false
lastmod: "2015-08-09T23:40:22Z"
id: 2233944
url: "https://simon-fish.iteye.com/blog/2233944"
---

在打包发布WAR时，自动压缩JS文件的写法，先记录下，回头再整理

   

```
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyTask" basedir="." default="compress-js">
	<property name="src.dir" value="./testres" />
	<property name="target.dir" value="./target/aa" />
	<property name="yuicompressor" value="./antlibs/yuicompressor-2.4.2.jar" />

	<target name="compress-js">
		<apply executable="java" parallel="false" failonerror="true" dest="${target.dir}"  append="false" force="true">
			<!--多个子目录压缩-->
			 <!--单个文件压缩-->  
			<fileset dir="${src.dir}/" includes="**/*.js"/>
			<arg line="-jar" />
			<arg path="${yuicompressor}" />
			<arg line="--charset utf-8" />
			<arg line="-o" />
			<targetfile  /> 
			<srcfile /> 
			<mapper type="glob" from="*.js" to="*.js" />
			<!--只压缩去掉空格，不混淆-->
		</apply>
	</target>
	<target name="compress-css">
		<apply executable="java" parallel="false" failonerror="true" dest="${src.dir}" append="false" force="true">
			<!--单个文件压缩-->
			<fileset dir="${src.dir}/">
				<include name="1.css" />
				<include name="2.css" />
			</fileset>	
				
			<!--多个子目录压缩-->
			<fileset dir="${src.dir}/" includes="css1/*.css"/>
			<fileset dir="${src.dir}/" includes="css2/*.css"/>

			<arg line="-jar" />
			<arg path="${yuicompressor}" />
			<arg line="--charset utf-8" />
			<srcfile />
			<arg line="-o" />
			<mapper type="glob" from="*.css" to="*.css" />
			<targetfile />
		</apply>
	</target>
</project>
```