Apache Ant,是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。由Apache软件基金会所提供。
1.先下载apache-ant-1.8.2-bin.zip 工具类包
2.把E:\development environment\ant\apache-ant-1.8.2-bin\apache-ant-1.8.2\bin路径添加到系统环境变量中
运行-crm-ant
---------------------------------------------------------------
Buildfile: build.xml does not exist!
Build failed
----------------------------------------------------------------
说明ant工具已安装成功!!
3.ant编译普通JAVA项目的流程
build -->所有的文件信息都保存在这个文件夹中
src -->所有的源码信息保存在这个文件夹中
classes-->编译好的所有文件保存在这个文件夹中
dist -->编译好的jar文件保存在这个文件夹中
新建JAVA项目.在项目中建立build.xml(ant edit)编译方式
在项目中建立ant体系(build,build/src,build/classes,build/dist)
接下来在build.xml中创建工作,先定义好目录常量(如果涉及到路径,属性一定要使用location)
<property name="build.dir" location="build"/>
<property name="build.src" location="${build.dir}/src"></property>
<property name="build.classes" location="${build.dir}/classes"/>
<property name="build.dist" location="${build.dir}/dist"/>
1.先创建build目录
<target name="init">
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.src}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.dist}"/>
</target>
target表示的是每一项具体的任务.ant build 刷新你会发现在项目中多了build这个目录
2、将src文件夹的数据拷贝到bulid/src
先定义src文件中的文件集
<fileset id="src.path" dir="src">
<include name="**/*.java"/> --包含
<exclude name="**/*.xml"/> --排除
</fileset>
--拷贝工作
<target name="copyStc" depends="init">
<copy todir="{build.src}">
<fileset refid="src.path"></fileset>
</copy>
</target>
depends的值是指定此项工作必须依赖另一项工作。
3、编译源代码
<target name="compile" depends="init">
<javac destdir="${build.classes}" srcdir="src"></javac>
</target>
4、将源代码打包为jar
先定义键值常量
<property name="jar.name" value="hello.jar"></property>
<property name="execute.class" value="ant.zzj.test.com.HelloWorld">
如常量数量太多的时候,也可引入外部文件,把常量定义到build.properties中去
<property file="build.properties"/> --引入
<target name="jar" depends="compile">
<jar destfile="${build.dist}/${jar.name}" basedir="${build.classes}">
<manifest>
<attribute name="Main-Class" value="ant.zzj.test.com.HelloWorld"/>
<attribute name="Build-By" value="zzj"/>
</manifest>
</jar>
</target>
5、直接运行程序
<target name="execute" depends="jar,copySrc">
<echo>基于类路径的classname来完成执行</echo>
<java classname="${execute.class}" classpath="${build.classes}">
<arg value="1"/>
<arg value="2"/>
<arg value="3"/>
</java>
<echo>基于jar文件执行</echo>
<java jar="${build.dist}/${jar.name}" fork="true">
<arg value="1"/>
<arg value="2"/>
<arg value="3"/>
</java>
</target>
6、多工作运行
<project default="execute">
</project>
---------------------------------------------------------------------------------------------------------------------
接下来介绍一下ant跟junit的整合
1.一个单元测试
HelloWorld.java
package ant.junit.zzj.com;
public class HelloWorld {
public String hello() {
return "world";
}
public String world() {
return "hello";
}
public String nil() {
return null;
}
public String notNil() {
return "abc";
}
public String ext() {
return null;
// throw new NumberFormatException();
}
}
TestHello.java
package ant.junit.zzj.com;
public class HelloWorld {
public String hello() {
return "world";
}
public String world() {
return "hello";
}
public String nil() {
return null;
}
public String notNil() {
return "abc";
}
public String ext() {
return null;
// throw new NumberFormatException();
}
}
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="junit-test">
<property name="src.dir" location="src"/>
<property name="test.src.dir" location="test"/>
<property name="lib.dir" location="lib"/>
<property name="build.dir" location="build"/>
<property name="build.classes" location="${build.dir}/classes"/>
<property name="build.test.dir" location="${build.dir}/test"/>
<property name="build.test.classes" location="${build.test.dir}/classes"/>
<property name="build.test.report" location="${build.test.dir}/report"/>
<property name="run.test.class" value="ant.junit.zzj.test.TestHello"/>
<path id="compile-path">
<fileset dir="${lib.dir}" includes="*.jar"></fileset>
</path>
<path id="compile-test-path">
<path refid="compile-path"></path>
<pathelement location="${build.classes}"/>
</path>
<path id="run-test-path">
<path refid="compile-test-path"></path>
<pathelement location="${build.test.classes}"/>
</path>
<target name="clean">
<echo>进行项目的清理工作</echo>
<delete dir="${bulid.dir}"></delete>
</target>
<target name="init" depends="clean">
<echo>项目的初始化工作</echo>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.test.dir}"/>
<mkdir dir="${build.test.classes}"/>
<mkdir dir="${build.test.report}"/>
</target>
<target name = "complie" depends="init">
<echo>编译源文件</echo>
<javac srcdir="${src.dir}" destdir="${build.classes}" includeantruntime="true" failοnerrοr="true"></javac>
</target>
<target name = "complie-test" depends="complie">
<echo>编译测试文件</echo>
<!-- classpathref指定源文件编译文件,因为测试文件依赖着源文件 -->
<javac srcdir="${test.src.dir}" destdir="${build.test.classes}" classpathref="compile-test-path" includeantruntime="true" failοnerrοr="true"></javac>
</target>
<target name="run-test" depends="complie-test">
<echo>运行单元测试</echo>
<junit printsummary="false" haltonfailure="false">
<classpath refid="run-test-path"></classpath>
<!-- 查看完整的错误信息 -->
<formatter type="brief" usefile="false"/>
<test name="${run.test.class}"></test>
</junit>
</target>
<target name="end" depends="run-test">
<echo>整个过程结束</echo>
</target>
</project>
ps: haltonfailure="true" 表示如果中间程序出错的情况下,不继续往下执行。
failοnerrοr="true" 表示如果中间程序出错的情况睛,不继续下执行。
2.多个单元测试
TestWorld.java
package ant.junit.zzj.test;
import org.junit.Assert;
import org.junit.Test;
public class TestWorld {
@Test
public void testStr() {
Assert.assertNull("发生出错",null);
}
@Test
public void testNo() {
Assert.assertNull("发生出错","123");
}
}
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="junit-test">
<property name="src.dir" location="src"/>
<property name="test.src.dir" location="test"/>
<property name="lib.dir" location="lib"/>
<property name="build.dir" location="build"/>
<property name="build.classes" location="${build.dir}/classes"/>
<property name="build.test.dir" location="${build.dir}/test"/>
<property name="build.test.classes" location="${build.test.dir}/classes"/>
<property name="build.test.report" location="${build.test.dir}/report"/>
<property name="run.test.class" value="**/Test*.class"/>
<path id="compile-path">
<fileset dir="${lib.dir}" includes="*.jar"></fileset>
</path>
<path id="compile-test-path">
<path refid="compile-path"></path>
<pathelement location="${build.classes}"/>
</path>
<path id="run-test-path">
<path refid="compile-test-path"></path>
<pathelement location="${build.test.classes}"/>
</path>
<target name="clean">
<echo>进行项目的清理工作</echo>
<delete dir="${bulid.dir}"></delete>
</target>
<target name="init" depends="clean">
<echo>项目的初始化工作</echo>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.test.dir}"/>
<mkdir dir="${build.test.classes}"/>
<mkdir dir="${build.test.report}"/>
</target>
<target name = "complie" depends="init">
<echo>编译源文件</echo>
<javac srcdir="${src.dir}" destdir="${build.classes}" includeantruntime="true" failοnerrοr="true"></javac>
</target>
<target name = "complie-test" depends="complie">
<echo>编译测试文件</echo>
<!-- classpathref指定源文件编译文件,因为测试文件依赖着源文件 -->
<javac srcdir="${test.src.dir}" destdir="${build.test.classes}" classpathref="compile-test-path" includeantruntime="true" failοnerrοr="true"></javac>
</target>
<target name="run-test" depends="complie-test">
<echo>运行单元测试</echo>
<junit printsummary="false" haltonfailure="false">
<classpath refid="run-test-path"></classpath>
<!-- 查看完整的错误信息 -->
<formatter type="brief" usefile="false"/>
<batchtest todir="${build.test.report}">
<fileset dir="${build.test.classes}" includes="${run.test.class}"></fileset>
</batchtest>
</junit>
</target>
<target name="end" depends="run-test">
<echo>整个过程结束</echo>
</target>
</project>
3.生成XML文件报告
<formatter type="xml"/>
<batchtest todir="${build.test.report}">
<fileset dir="${build.test.classes}" includes="${run.test.class}"></fileset>
</batchtest>
4.生成HTML文件报告
<?xml version="1.0" encoding="UTF-8"?>
<project name="junit-test">
<property name="src.dir" location="src"/>
<property name="test.src.dir" location="test"/>
<property name="lib.dir" location="lib"/>
<property name="build.dir" location="build"/>
<property name="build.classes" location="${build.dir}/classes"/>
<property name="build.test.dir" location="${build.dir}/test"/>
<property name="build.test.classes" location="${build.test.dir}/classes"/>
<property name="build.test.report" location="${build.test.dir}/report"/>
<property name="run.test.class" value="**/Test*.class"/>
<path id="compile-path">
<fileset dir="${lib.dir}" includes="*.jar"></fileset>
</path>
<path id="compile-test-path">
<path refid="compile-path"></path>
<pathelement location="${build.classes}"/>
</path>
<path id="run-test-path">
<path refid="compile-test-path"></path>
<pathelement location="${build.test.classes}"/>
</path>
<target name="clean">
<echo>进行项目的清理工作</echo>
<delete dir="${bulid.dir}"></delete>
</target>
<target name="init" depends="clean">
<echo>项目的初始化工作</echo>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.test.dir}"/>
<mkdir dir="${build.test.classes}"/>
<mkdir dir="${build.test.report}"/>
</target>
<target name = "complie" depends="init">
<echo>编译源文件</echo>
<javac srcdir="${src.dir}" destdir="${build.classes}" includeantruntime="true" failοnerrοr="true"></javac>
</target>
<target name = "complie-test" depends="complie">
<echo>编译测试文件</echo>
<!-- classpathref指定源文件编译文件,因为测试文件依赖着源文件 -->
<javac srcdir="${test.src.dir}" destdir="${build.test.classes}" classpathref="compile-test-path" includeantruntime="true" failοnerrοr="true"></javac>
</target>
<target name="run-test" depends="complie-test">
<echo>运行单元测试</echo>
<junit printsummary="false" haltonfailure="false">
<classpath refid="run-test-path"></classpath>
<!-- 查看完整的错误信息 -->
<formatter type="brief" usefile="false"/>
<formatter type="xml"/>
<batchtest todir="${build.test.report}">
<fileset dir="${build.test.classes}" includes="${run.test.class}"></fileset>
</batchtest>
</junit>
<junitreport todir="${build.test.report}">
<fileset dir="${build.test.report}" includes="TEST-*.xml"></fileset>
<report format="frames" todir="${build.test.report}/html"/>
</junitreport>
</target>
<target name="end" depends="run-test">
<echo>整个过程结束</echo>
</target>
</project>
ps:以上请使用JDK1.5 ,在1.5以上的版本会报错(版本问题)
5.上传到FTP服务器上
先建立FTP服务器
<!-- 上传到FTP服务器 -->
<target name="ftp" depends="zip">
<ftp userid="zhzj" password="123" server="localhost"
action="put" remotedir="user">
<fileset dir="${zip.dir}" includes="*.zip"></fileset>
</ftp>
</target>
执行发现会报错
F:\kzj_soa_workspace\ant_junit\build.xml:86: Could not create type ftp due to java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClientConfig
这是因为没导入common-net.jar(先导入)
在eclipse环境中
6.WEB项目打包成War自动导到tomcat目录下
<?xml version="1.0" encoding="UTF-8"?>
<project>
<property name="src.dir" location="src"></property>
<property name="build.dir" location="build"></property>
<property name="build.classes" location="${build.dir}/classes"></property>
<property name="build.war" location="${build.dir}/war"></property>
<property name="web.name" value="war_ant_web"></property>
<property name="web.root" value="WebContent"></property>
<property name="web.WEB-INF" location="${web.root}/WEB-INF"></property>
<property name="web.lib" location="${web.WEB-INF}/lib"></property>
<property environment="env"></property>
<path id="compile">
<fileset dir="${web.lib}" includes="*.jar"></fileset>
<fileset dir="${env.CATALINA_HOME}/lib" includes="*.jar"></fileset>
</path>
<target name="init">
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.war}"/>
</target>
<target name="compile" depends="init">
<javac destdir="${build.classes}" srcdir="${src.dir}" includeantruntime="true" classpathref="compile"></javac>
</target>
<target name="war" depends="compile">
<war destfile="${build.war}/${web.name}.war">
<fileset dir="${web.root}" includes="**/*.*"></fileset>
<lib dir="${web.lib}"></lib>
<webinf dir="${web.WEB-INF}"></webinf>
<classes dir="${build.classes}"></classes>
</war>
</target>
<target name="deploy" depends="war">
<copy todir="${env.CATALINA_HOME}/webapps">
<fileset dir="${build.war}" includes="${web.name}.war"></fileset>
</copy>
</target>
</project>