---
title: "ant打包"
author: "weick"
date: "2013-12-16T10:31:04Z"
tags: []
categories: ["Web前端"]
slug: "ant"
draft: false
lastmod: "2014-05-04T16:02:57Z"
id: 1990114
url: "https://weick.iteye.com/blog/1990114"
---

基本的ant打包分三步：

1、配置基本信息，比如

  （1）打包依赖的jdk，以及第三方jar等的路径。

  （2）打包的目标代码根路径

  （3）打包jar输出路径

    ......

2、配置打包的目标代码信息

  （1）先编译代码，需指定编译依赖的包，要编译的目标代码源码路径

  （2）指定编译代码输出路径

  （3）将编译的class打包，需指定用来打包的class文件路径等等信息，可包含第三方jar包的class

3、配置用于打包的class文件信息，主要是文件路径。

以下是参考例子。

 

```
<project basedir="../"> -- 指定根路径

	  --先配置基本信息

	 <property name="lib_out" value="${basedir}/ant/output"/>   --指定打包的class文件输出路径
	 
	 <property name="src_dir" value="${basedir}/src/com/"/>  -- 指定src的相对路径
	 
	 -- 需包含的第三方jar的解压路径
	 <mkdir dir="${basedir}/ant/libclass"/>
	 <property name="libclass_dir" value="${basedir}/ant/libclass"/>
	 
	 --指定编译代码依赖的jdk及第三方jar路径
	 <path id="compile.classpath">
	        <fileset dir="${basedir}/lib">
	            <include name="*.jar"/>
	        </fileset>
	  </path>
	  
	  
	
	 --以下配置待打包的目标代码信息  
	
	<target name="用于打包的目标代码1">  --打包的目标代码1 target1

		--先编译代码

 		<echo message="先编译"/>  

    <echo message="app代码编译"/>
    <mkdir dir="${basedir}/build/app_compiledClass_path"/>  -- 在指定目录下创建class文件存放路径
    
    -- 指定需要编译的目标代码路径
    <javac classpathref="compile.classpath" srcdir="${src_dir}"

               destdir="${basedir}/build/app_compiledClass_path"   --指定编译后的class文件存放路径
               optimize="true" debug="on"
               >
     
     	--排除不用编译的文件
     	<exclude name="**/xx.java"/>
     
               
     </javac>
               
               
     -- 将编译好的文件打包到jar        
    <echo message="打包到jar"/>
        <jar destfile="${lib_out}/app_package.jar">
            -- 指定要打包的目标class
            <fileset refid="task_files_app">   
            
            -- 包含第三方jar包的class
						<fileset refid="jar_class_file"/>
            </fileset>
        </jar>
   </target>        
   
   -- 先解压第三方jar包，然后再包含其class文件
    <unzip src="${basedir}/lib/dom4j-1.6.1.jar" dest="${libclass_dir}/">
        <patternset>
            <exclude name="**/META-INF"/>
        </patternset>
    </unzip>
    
    --编译后删除编译文件    
    <echo message="删除临时文件夹"/>
    <delete dir="${basedir}/build/app_compiledClass_path"/>   
        
    --用来打包的目标class路径 target1 
  <fileset dir="${basedir}/build/app_compiledClass_path" id="task_files_app">
        <include name="package1/p1/**/*.class"/>
        <include name="/package1/p2/**/*.class"/>
   </fileset> 
   
   --包含第三方jar包的class
   <fileset dir="${libclass_dir}" id="jar_class_file">
        <include name="*/**/*.class"/>
   </fileset>
   
   
   
   --继续编译及打包的另一部分目标代码，要和上一配置的编译打包的目标分开打包的可在以下继续配置 target2
   <target>
    ....
   </target> 
   
   -- 用来打包的目标class路径 target2
   <fileset dir='...' id='...'>
    .. 
   </fileset> 
   
   -- 继续配置编译及打包目标
   ...
   
</project>     
```