---
title: "Eclipse中使用Ant"
author: "g21121"
date: "2013-07-18T14:34:11Z"
tags: []
categories: ["编程语言"]
slug: "eclipse-ant"
draft: false
lastmod: "2013-07-18T16:33:57Z"
id: 1909223
url: "https://286.iteye.com/blog/1909223"
---

Eclipse中已经集成了Ant，我们可以直接在Eclipse中运行Ant，这里我要做的不是直接运行已编写好的build.xml文件，而是利用Ant插件来生成一个构建文件。

        首先打开Eclipse，点击导航栏的"Window"--&gt;"Preferences"--&gt;"Ant"

![](http://dl2.iteye.com/upload/attachment/0087/1084/4aacfc8d-7de5-385f-805d-fbb42d03cbc8.png)

 

        其中有Ant的代码模板，格式化，classpath等属性设置，可以根据具体情况自行设置，都比较简单。

        我们要构建一个有依赖jar包项目的构建文件，这里我拿log4j为例，将log4j添加到HelloAnt项目的build path当中，如图所示：

![](http://dl2.iteye.com/upload/attachment/0087/1094/2cfc2640-2ed8-339e-bca2-915026329a24.png)  
 

        修改HelloAnt.java的源代码为：

```
package com.ant.hello;

import org.apache.log4j.Logger;

public class HelloAnt {
    
    private static Logger log=Logger.getLogger(HelloAnt.class);
	
    public static void main(String[] args){
        log.info("hello Ant!");

    }
    
}
```

 

        修改build path中的class输出路径为"HelloAnt/classes"。

 

        我们在项目名称上右键--&gt;选择Export（导出）--&gt;Ant Buildfile--&gt;下一步--&gt;修改相应属性--&gt;Finish

![](http://dl2.iteye.com/upload/attachment/0087/1103/a5148774-33e5-3847-bbf5-5dc2f89435a0.png)  
![](http://dl2.iteye.com/upload/attachment/0087/1105/57051c43-1c4c-3baa-98ce-313178065600.png)  
        完成之后我们会发现项目目录里多了一个"build.xml"文件：

![](http://dl2.iteye.com/upload/attachment/0087/1109/b3939a17-e870-301e-8747-e68e855dcde0.png)

 

        打开之后是如下代码：

```
<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<project basedir="." default="build" name="HelloAnt">
    <property environment="env"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.6"/>
    <property name="source" value="1.6"/>
    <path id="HelloAnt.classpath">
        <pathelement location="classes"/>
        <pathelement location="../log4j.jar"/>
    </path>
    <target name="init">
        <mkdir dir="classes"/>
        <copy includeemptydirs="false" todir="classes">
            <fileset dir="src">
                <exclude name="**/*.launch"/>
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="classes"/>
    </target>
    <target depends="clean" name="cleanall"/>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="classes" includeantruntime="false" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="HelloAnt.classpath"/>
        </javac>
    </target>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
        <copy todir="${ant.library.dir}">
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </copy>
        <unzip dest="${ant.library.dir}">
            <patternset includes="jdtCompilerAdapter.jar"/>
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </unzip>
    </target>
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target>
    <target name="HelloAnt">
        <java classname="com.ant.hello.HelloAnt" failonerror="true" fork="yes">
            <classpath refid="HelloAnt.classpath"/>
        </java>
    </target>
</project>
```

 

      我们可以根据情况修改其中的&lt;path&gt;标签下的命名及路径，当然这种事比较傻瓜式的生成方法，下面我们换一种可以详细设置的生成方法。

 

        点击导航栏"Project"--&gt;"Properties"--&gt;"Builder"--&gt;"New"--&gt;"Ant Builder"：

![](http://dl2.iteye.com/upload/attachment/0087/1117/13dd9e15-1101-397f-96b4-cf2d1436aaf3.png)  
        根据给出的界面我们可以详细设置。