一个简单的Ant示例 :: 阳光下的蚂蚁
未识别博客 · hamal · 2004-08-07
阳光下的蚂蚁
在JAVA中前行 在生命中跋涉
.:
发表评论 :.
.:
最后更新 :.
背单词
回头看问题
世界末日
找房
第一次吸毒
确诊
死火山 活火山
医院
一个简单的Ant示例
在resin3.0中配置hibernate2.1.2连mysql
::
<<<在resin3.0中配置hibernate2.1.2连mysql
|
首页
|
医院>>>
一个简单的Ant示例
本示例仅仅作为一个初级Ant示例教程(windows运行版)
author: hamal
运行条件:
1.已安装并配置好ant
2.已安装并配置好junit(仅限junit部分)
3.已安装并配置好cvs(仅限cvs部分)
本示例仅仅是一个流程演示,描述了用Ant自动构建项目的主要基本步骤:
从CVS获得项目源文件-》编译源文件-》用JUnit进行单元测试-》打包class文件-》部署到服务器。
在本示例中将不会详细解释ant中的各种任务标签,所以请在使用本示例时参考ant文档
所有示例在ant1.6.1下测试通过,junit部分在junit3.8.1下测试通过,cvs为TortoiseCVS1.6.8
使用方法:
将打包文件解压到任何目录,直接运行相应的bat文件即可。
build文件说明:
cvs.xml cvs示例
compile.xml 编译示例
package.xml 打包示例
junit1.xml junit示例
junit2.xml
deploy.xml 部署示例
build.xml 综合示例
clear.xml 恢复到原始状态的快捷方式
build.xml文件------------------------------------------------------
<?xml version="1.0" encoding="GBK"?>
<project name="Build All Elements" default="deploy" basedir=".">
<!-- ===================================================================================== -->
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="log" location="log"/>
<property name="lib" location="lib"/>
<property name="cvs_dest" location="cvs"/>
<property name="compile_dist" location="classes"/>
<property name="JUnit_lib" location="D:\junit3.8.1\junit.jar"/>
<!-- ===================================================================================== -->
<!-- 从cvs服务器获取源文件 -->
<target name="cvs" description="get src from cvs server">
<mkdir dir="${cvs_dest}"/>
<cvs cvsRoot=":pserver:cvsstudy@192.162.125.252:/demo-study"
dest="${cvs_dest}"
command="-q checkout -P CvsStudy"
output="${log}\cvs_patch.txt"
/>
</target>
<!-- ===================================================================================== -->
<!-- 编译源文件 -->
<target name="compile" description="compile src" depends="cvs">
<mkdir dir="${compile_dist}"/>
<javac srcdir="${src}"
destdir="${compile_dist}"
classpath="${JUnit_lib}"
/>
</target>
<!-- ===================================================================================== -->
<!-- 调用单个测试类的java命令方式 -->
<target name="junit1" description="junit test" depends="compile">
<!-- 单个测试类 -->
<java classname="HelloWorldTest" failonerror="true">
<classpath>
<pathelement path="${compile_dist}" />
<pathelement path="${JUnit_lib}" />
</classpath>
</java>
</target>
<!-- ===================================================================================== -->
<!-- 用Ant可批量处理的junit测试 -->
<target name="junit2" description="junit test" depends="compile">
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement path="${compile_dist}" />
<pathelement path="${JUnit_lib}" />
</classpath>
<!-- 单个测试类 -->
<!-- <test name="HelloWorldTest" haltonfailure="yes" /> -->
<!-- 批量调用测试类 -->
<batchtest fork="yes" todir="${compile_dist}">
<fileset dir="${src}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
</target>
<!-- ===================================================================================== -->
<!-- 打包class文件 -->
<target name="jar" description="pack jar" depends="junit1,junit2">
<mkdir dir="${lib}"/>
<jar jarfile="${lib}\helloworld.jar" basedir="${compile_dist}"/>
</target>
<!-- ===================================================================================== -->
<!-- 部署应用 -->
<target name="deploy" description="deploy application" depends="jar">
<!-- set properties for this target -->
<property name="deploy" location="deploy"/>
<property name="deploy_lib" location="deploy\lib"/>
<property name="deploy_classes" location="deploy\classes"/>
<!-- 重建目录 -->
<delete dir="${deploy_dir}"/>
<mkdir dir="${deploy}"/>
<mkdir dir="${deploy_lib}"/>
<mkdir dir="${deploy_classes}"/>
<!-- 拷贝部署文件 -->
<copy todir="${deploy_classes}">
<fileset dir="${compile_dist}"/>
</copy>
<copy todir="${deploy_lib}">
<fileset dir="${lib}"/>
</copy>
</target>
</project>
----------------------------------------------------------------------
clear.xml文件---------------------------------------------------------
<?xml version="1.0" encoding="GBK"?>
<project name="Build All Elements" default="clear" basedir=".">
<target name="clear" description="the main build target">
<!-- 删除示例生成的目录 -->
<delete dir="classes"/>
<delete dir="lib"/>
<delete dir="cvs"/>
<delete dir="deploy"/>
<delete>
<fileset dir="log"/>
</delete>
<echo message="删除完成!2秒后退出Ant"/>
<sleep seconds="2"/>
</target>
</project>
-----------------------------------------------------------------------
compile.xml文件--------------------------------------------------------
<?xml version="1.0" encoding="GBK"?>
<project name="Build All Elements" default="compile" basedir=".">
<target name="compile" description="the main build target">
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="dist" location="classes"/>
<property name="JUnit_lib" location="D:\junit3.8.1\junit.jar"/>
<mkdir dir="${dist}"/>
<javac srcdir="${src}"
destdir="${dist}"
classpath="${JUnit_lib}"
/>
</target>
</project>
-----------------------------------------------------------------------
cvs.xml文件------------------------------------------------------------
<?xml version="1.0" encoding="GBK"?>
<project name="Build All Elements" default="cvs" basedir=".">
<target name="cvs" description="the main build target">
<!-- set global properties for this build -->
<property name="dest" location="cvs"/>
<property name="log" location="log"/>
<mkdir dir="${dest}"/>
<!-- 从cvs服务器获取源文件 -->
<cvs cvsRoot=":pserver:cvsstudy@192.162.125.252:/demo-study"
dest="${dest}"
command="-q checkout -P CvsStudy"
output="${log}\cvs_patch.txt"
/>
</target>
</project>
-----------------------------------------------------------------------
deploy.xml文件---------------------------------------------------------
<?xml version="1.0" encoding="GBK"?>
<project name="Build All Elements" default="deploy" basedir=".">
<target name="deploy" description="the main build target">
<!-- set global properties for this build -->
<property name="src_lib" location="lib"/>
<property name="src_classes" location="classes"/>
<property name="deploy" location="deploy"/>
<property name="deploy_lib" location="deploy\lib"/>
<property name="deploy_classes" location="deploy\classes"/>
<!-- 重建目录 -->
<delete dir="${deploy}"/>
<mkdir dir="${deploy}"/>
<mkdir dir="${deploy_lib}"/>
<mkdir dir="${deploy_classes}"/>
<!-- 拷贝部署文件 -->
<copy todir="${deploy_classes}">
<fileset dir="${src_classes}"/>
</copy>
<copy todir="${deploy_lib}">
<fileset dir="${src_lib}"/>
</copy>
</target>
</project>
-----------------------------------------------------------------------
使用java命令的JUnit测试------------------------------------------------
<?xml version="1.0" encoding="GBK"?>
<project name="Build All Elements" default="runtests" basedir=".">
<target name="runtests">
<!-- set global properties for this build -->
<property name="test_package" location="classes"/>
<property name="junit_lib" location="d:\junit3.8.1\junit.jar"/>
<!-- 单个测试类 -->
<java classname="HelloWorldTest" failonerror="true">
<classpath>
<pathelement path="${test_package}" />
<pathelement path="${junit_lib}" />
</classpath>
</java>
</target>
</project>
-----------------------------------------------------------------------
使用ant的JUnit任务进行的测试-------------------------------------------
<?xml version="1.0" encoding="GBK"?>
<project name="Build All Elements" default="runtests" basedir=".">
<target name="runtests">
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="classes" location="classes"/>
<property name="junit_lib" location="d:\junit3.8.1\junit.jar"/>
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement path="${classes}" />
<pathelement path="${junit_lib}" />
</classpath>
<!-- 单个测试类 -->
<test name="HelloWorldTest" haltonfailure="yes" />
<!-- 批量调用测试类 -->
<!--
<batchtest fork="yes" todir="${classes}">
<fileset dir="${src}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
-->
</junit>
</target>
</project>
-----------------------------------------------------------------------
package.xml文件--------------------------------------------------------
<?xml version="1.0" encoding="GBK"?>
<project name="Build All Elements" default="jar" basedir=".">
<target name="jar">
<!-- set global properties for this build -->
<property name="dist" location="classes"/>
<property name="lib" location="lib"/>
<mkdir dir="${lib}"/>
<jar jarfile="${lib}\helloworld.jar" basedir="${dist}"/>
</target>
</project>
-----------------------------------------------------------------------
测试用的HelloWorld.java源文件------------------------------------------
//import mypackage.TestClassesName;
public class HelloWorld {
public String sayHello() {
return "Hello World";
}
public static void main(String[] args) {
HelloWorld world = new HelloWorld();
System.out.println(world.sayHello());
}
}
class HelloWorld1 {
public String sayHello() {
return "Hello World 1";
}
public static void main(String[] args) {
HelloWorld1 world1 = new HelloWorld1();
System.out.println(world1.sayHello());
}
}
class HelloWorld2 {
public String sayHello() {
return "Hello World 2";
}
public static void main(String[] args) {
HelloWorld2 world2 = new HelloWorld2();
System.out.println(world2.sayHello());
}
}
-----------------------------------------------------------------------
HelloWorldTest.java源文件----------------------------------------------
import junit.framework.TestCase;
import junit.framework.AssertionFailedError;
/**
* JUnit 3.2 testcases for HelloWorld
*/
public class HelloWorldTest extends TestCase {
public HelloWorldTest(String name) {
super(name);
}
public static void main(String args[]) {
junit.textui.TestRunner.run(HelloWorldTest.class);//文本方式
//junit.swingui.TestRunner.run(HelloWorldTest.class);//图形方式
}
public void testSayHello() {
HelloWorld world = new HelloWorld();
this.assertTrue(world!=null);//判定world对象存在
assertEquals("Hello World", world.sayHello());//测试sayHello()方法的输出
}
}
-----------------------------------------------------------------------
hamal
|
引用(trackback0)
|
编辑
□ 评论
原始快照:web.archive.org