项目代码:

  package com.jsw.app;

importorg.apache.commons.logging.Log;

importorg.apache.commons.logging.LogFactory;

/**

*@authorgang.huang

*

*/

publicclassAutoTestDepart{

privatestaticLoglog=LogFactory.getLog(AutoTestDepart.class);

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

log.info("引入了第三方JAR文件");

System.out.println("theisaautotestdepartok");

}

}

上面为测试代码,

下面编写ant脚本:

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

<!--======================================================================

2009-12-30上午11:49:53

用ant手工编译,运行一个项目

gang.huang

注意事项:basedir="."表示当前目录

======================================================================-->

<projectname="AutoTestDepart"default="run"basedir=".">

<description>

description

</description>

<!--定义全局属性-->

<!--需要编译的目录指当前的目录-->

<propertyname="src"value="."/>

<!--生成class类的目录,是需要我们创建的-->

<propertyname="build"value="build"/>

<propertyname="dist"value="dist"/>

<propertyname="AutoTest"value="AutoTest.jar"/>

<!--全局jar文件,供其他target引用-->

<pathid="ref-jar">

<pathelementlocation="${src}/lib/commons-logging-1.1.1.jar"/>

</path>

<!--=================================

target:default

=================================-->

<targetname="init">

<mkdirdir="${build}"/>

<mkdirdir="${dist}/lib/"/>

</target>

<targetname="run"depends="compile"description="description">

<javaclassname="com.jsw.app.AutoTestDepart"classpath="${build}">

<classpathrefid="ref-jar"/>

</java>

</target>

<!--编译,打包-->

<targetname="compile"depends="clean,init">

<javacsrcdir="${src}"destdir="${build}">

<classpathrefid="ref-jar"/>

</javac>

<jardestfile="${dist}/lib/AutoDepart.jar"basedir="${build}"/>

</target>

<targetname="clean">

<deletedir="${build}"/>

<deletedir="${dist}}/lib"/>

    </target>

</project>

结果:

D:\work\stuworkspace\AutoTestDepart>ant

Buildfile:build.xml

clean:

[delete]DeletingdirectoryD:\work\stuworkspace\AutoTestDepart\build

init:

[mkdir]Createddir:D:\work\stuworkspace\AutoTestDepart\build

compile:

[javac]Compiling1sourcefiletoD:\work\stuworkspace\AutoTestDepart\build

[jar]Buildingjar:D:\work\stuworkspace\AutoTestDepart\dist\lib\AutoDepart.jar

run:

[java]2009-12-3016:19:52com.jsw.app.AutoTestDepartmain

[java]信息:引入了第三方JAR文件

[java]theisaautotestdepartok

BUILDSUCCESSFUL

Totaltime:1second