Eclipse中已经集成了Ant,我们可以直接在Eclipse中运行Ant,这里我要做的不是直接运行已编写好的build.xml文件,而是利用Ant插件来生成一个构建文件。
首先打开Eclipse,点击导航栏的"Window"-->"Preferences"-->"Ant"
其中有Ant的代码模板,格式化,classpath等属性设置,可以根据具体情况自行设置,都比较简单。
我们要构建一个有依赖jar包项目的构建文件,这里我拿log4j为例,将log4j添加到HelloAnt项目的build path当中,如图所示:
修改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"。
我们在项目名称上右键-->选择Export(导出)-->Ant Buildfile-->下一步-->修改相应属性-->Finish
完成之后我们会发现项目目录里多了一个"build.xml"文件:
打开之后是如下代码:
<?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" failοnerrοr="true" fork="yes"> <classpath refid="HelloAnt.classpath"/> </java> </target> </project>
我们可以根据情况修改其中的<path>标签下的命名及路径,当然这种事比较傻瓜式的生成方法,下面我们换一种可以详细设置的生成方法。
点击导航栏"Project"-->"Properties"-->"Builder"-->"New"-->"Ant Builder":
根据给出的界面我们可以详细设置。