---
title: "ant 文件操作"
author: "zhou_wu"
date: "2011-01-18T18:15:53Z"
tags: []
categories: ["编程语言"]
slug: "ant"
draft: false
lastmod: "2011-01-18T18:18:15Z"
id: 883755
url: "https://leesmart.iteye.com/blog/883755"
---

1.bulid.xml的配置

```xml
<?xml version="1.0"?>
<project name = "runtimeFileOperate" default="zipFile">
	<target name = "dyMakeDir">
			<mkdir dir="${dyDir}"/>   
	</target>
 	<target name = "zipFile" >
 			<zip destfile="${version}.zip" basedir="${dyDir}" />
  </target>      
	<target name = "unzipFile" >
 			<unzip src="{version}.zip"  dest="${dyDir}"> </unzip>
  </target>
  <target name="deleteDir">
		<delete file="${dyDir}" />
	</target>
	<target name="copyFile">
		<copy todir="toDir">  
		   <fileset dir="file"/>  
		</copy>  
</target>
</project>
```


2.java代码操作

```java
import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;


public class AntOperattion {
	 public static void operateAnt(String buildPath,String targetName,String dyPath,String version){ 
		 	String xmlPath = buildPath + "allDeployJsp//build.xml";
	        File buildFile = new File(xmlPath);   
	        
	        Project p = new Project();   
	        try {   
	            p.fireBuildStarted();   
	            p.init();   
	            ProjectHelper helper = ProjectHelper.getProjectHelper();   
	            helper.parse(p, buildFile);   
	            p.setNewProperty("dyDir", dyPath);
	            p.setNewProperty("version", version);
	            p.executeTarget(targetName);
	            p.fireBuildFinished(null);   
	        } catch (BuildException e) {   
	            p.fireBuildFinished(e);   
	        }   
	    }   
}

```