---
title: "我写的ant编译、打包、测试、测试报告生成和邮件发送模板"
author: "danlley"
date: "2007-07-17T21:40:42Z"
tags: []
categories: ["编程语言"]
slug: "ant"
draft: false
lastmod: "2017-04-25T21:22:00Z"
id: 102144
url: "https://danlley.iteye.com/blog/102144"
---

js 代码

 

01. &lt;?xml version="1.0"?&gt;
02. &lt;project name="springproj" basedir="." default="mail"&gt;
03. &lt;property name="pro.name" value="springproj" /&gt;
04. &lt;property name="pro.author" value="Danlley Wei" /&gt;
05. &lt;property name="src.dir" value="src/main/java" /&gt;
06. &lt;property name="pro.web.root" value="war" /&gt;
07. &lt;property name="pro.web.source" value="${pro.web.root}/WEB-INF" /&gt;
08. &lt;property name="pro.build.path" value="${pro.web.source}/classes" /&gt;
09. &lt;property name="user.dir" value="${pro.build.path}" /&gt;
10. &lt;target name="mail" depends="mkzip"&gt;
11. &lt;mail mailhost="smtp.126.com" mailport="25" subject="The Build Test" user="用户名" password="邮箱密码"&gt;
12. &lt;from address="发送地址" name="Danlley Wei" /&gt;
13. &lt;fileset dir="report/html"&gt;
14. &lt;include name="\*\*/test-result${ant.project.name}.zip" /&gt;
15. &lt;/fileset&gt;
16. &lt;to address="邮件接收地址" name="Danlley Wei" /&gt;
17. &lt;message&gt;The ${pro.name}--${pro.author} has been tested ! &lt;/message&gt;
18. &lt;/mail&gt;
19. &lt;/target&gt;
20. &lt;target name="mkzip" depends="report"&gt;
21. &lt;jar destfile="report/html/test-result${ant.project.name}.zip"&gt;
22. &lt;fileset dir="report/html"&gt;
23. &lt;include name="\*\*/\*.html" /&gt;
24. &lt;include name="\*\*/\*.css" /&gt;
25. &lt;include name="\*\*/\*.txt" /&gt;
26. &lt;/fileset&gt;
27. &lt;/jar&gt;
28. &lt;/target&gt;
29. &lt;target name="report" depends="junit"&gt;
30. &lt;junitreport id="myJUnitReport" taskname="reported" todir="report" description="Junit Report"&gt;
31. &lt;fileset dir="report"&gt;
32. &lt;include name="TEST-\*.xml" /&gt;
33. &lt;/fileset&gt;
34. &lt;report todir="report/html" /&gt;
35. &lt;/junitreport&gt;
36. &lt;/target&gt;
37. &lt;target name="junit" depends="build"&gt;
38. &lt;mkdir dir="report/html" /&gt;
39. &lt;junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes"&gt;
40. &lt;formatter type="plain" usefile="false" /&gt;
41. &lt;formatter type="xml" /&gt;
42. &lt;test name="org.danlley.hibernate.dao.DeptDAOImplTest" todir="report" /&gt;
43. &lt;classpath refid="master-classpath" /&gt;
44. &lt;/junit&gt;
45. &lt;/target&gt;
46. &lt;target name="build" depends="prepare"&gt;
47. &lt;javac destdir="${pro.build.path}" target="1.5"&gt;
48. &lt;src path="${src.dir}" /&gt;
49. &lt;classpath refid="master-classpath" /&gt;
50. &lt;/javac&gt;
51. &lt;javac destdir="${pro.build.path}" target="1.5"&gt;
52. &lt;src path="src/main/test" /&gt;
53. &lt;classpath refid="master-classpath" /&gt;
54. &lt;/javac&gt;
55. &lt;/target&gt;
56. &lt;target name="prepare" depends="clean"&gt;
57. &lt;copy todir="${pro.build.path}"&gt;
58. &lt;fileset dir="${src.dir}"&gt;
59. &lt;include name="\*\*/\*.properties" /&gt;
60. &lt;include name="\*\*/\*.xml" /&gt;
61. &lt;/fileset&gt;
62. &lt;/copy&gt;
63. &lt;/target&gt;
64. &lt;target name="clean"&gt;
65. &lt;delete&gt;
66. &lt;fileset dir="${pro.build.path}"&gt;
67. &lt;include name="\*\*/\*.\*" /&gt;
68. &lt;/fileset&gt;
69. &lt;fileset dir="report"&gt;
70. &lt;include name="\*\*/\*.\*" /&gt;
71. &lt;/fileset&gt;
72. &lt;/delete&gt;
73. &lt;/target&gt;
74. &lt;path id="master-classpath"&gt;
75. &lt;fileset dir="lib"&gt;
76. &lt;include name="\*.jar" /&gt;
77. &lt;/fileset&gt;
78. &lt;pathelement path="${pro.build.path}" /&gt;
79. &lt;/path&gt;
80. &lt;/project&gt;

 

target依赖关系：  
mail→mkzip→report→junit→build→prepare→clean

工程classpath：master-classpath

```
clean节点：
删除测试和编译过程中生成的所有文件

prepare节点：
将工程编译打包所需资源文件全部拷贝到编译路径下

build节点：
生成工程源文件和测试代码源文件的二进制版本

junit节点：
运行测试用例

report节点：
生成测试报告

mkzip节点：
将生成的测试报告以zip格式进行打包

mail节点：
将测试结果发送到开发小组邮箱 
```

 

```
1. 我在模板 中使用的126邮箱，如果你的邮箱是新申请的126邮箱，上述模板可能没办法发送你的邮件，原因是126邮箱对于新申请的用户暂时不开通smtp和pop3服务

2.ant并不会自带安装发送邮件所需相关资源包，因此需要我们手动到sun官方网站下载，然后把资源包放到ant的lib下面，我的位置是：D:\apache-ant-1.7.0\lib

可以从下面地址下载ant发邮件所需的两个资源（mail.jar和activation.jar）：
http://java.sun.com/products/javamail
http://java.sun.com/products/javabeans/glasgow/jaf.html 
```