一、环境准备
-
Jenkins:
-
到官网下载jenkins.war包:http://jenkins-ci.org/
- 安装方法有两种:
-
把下载下来的jenkins.war包放到文件夹下,如C:\jenkins,然后打开命令行窗口并进到该目录下,执行java -jar jenkens.war命令,当提示:“Jenkins is fully up and running”时,表示启动成功,这时在浏览器窗口输入:
http://localhost:8080/ 就可到jenkins的首页。
1. 如果有tomcat,把jenkins.war包放在tomcat的webapps文件夹下,启动tomcat时会自动启动jenkins,这时通过
http://localhost:8080/jenkins就 可以访问jenkins的首页了。
- ANT:
下载ant并配置ANT_HOME,官网:http://ant.apache.org/。
3、Junit:
下载junit.jar包,没用过的可参考:http://blog.csdn.net/lengyuhong/article/details/5815017
4、SVN:
1、用本地硬盘当SVN仓库:http://wenku.baidu.com/view/12b02f6a011ca300a6c39081.html
2、SVN服务器搭建和使用:https://www.daimajiaoliu.com/daima/487164fd8900406 Windows , https://www.daimajiaoliu.com/daima/485d2461a900404 Linux
二、项目代码:
****环境准备好了之后就可开始写代码、单元测试案例以及ANT用来构建的build.xml文件,这些内容在上一篇ANT task之Junit、JunitReport有讲过,这里不细讲:
1、Java代码:
1
2
3
4
5
6
7
8
9
10
11
12 1package com.glen.he;
2
3public class ComplexCalculation {
4 public int Division(int a,int b){
5 return (a/b);
6 }
7
8 public int Multiply(int a,int b){
9 return (a*b);
10 }
11}
12
ComplexCalculation.java
1
2
3
4
5
6
7
8
9
10
11
12
13 1package com.glen.he;
2
3public class SimpleCalculation {
4 public int Add(int a,int b){
5 return (a+b);
6 }
7
8 public int Subtration(int a,int b){
9 return(a-b);
10 }
11
12}
13
SimpleCalculation.java
2、单元测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 1package com.glen.he;
2
3import com.glen.he.ComplexCalculation;
4
5import static org.junit.Assert.*;
6
7import org.junit.Test;
8
9
10public class ComplexCalculationTest {
11
12 ComplexCalculation cc = new ComplexCalculation();
13
14 @Test
15 public void DivisionTest() {
16
17 int c = cc.Division(100, 5);
18
19 assertEquals(20, c);
20 }
21
22 @Test
23 public void MultiplyTest() {
24
25 int c = cc.Multiply(100, 5);
26
27 assertEquals(500, c);
28 }
29}
30
ComplexCalculationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 1package com.glen.he;
2
3import com.glen.he.SimpleCalculation;
4
5import static org.junit.Assert.*;
6import org.junit.Test;
7
8public class SimpleCalculationTest {
9
10 SimpleCalculation sc = new SimpleCalculation();
11
12 @Test
13 public void AddTest() {
14
15 int c = sc.Add(3, 5);
16
17 assertEquals(8, c);
18 }
19
20 @Test
21 public void SubtrationTest() {
22
23 int c = sc.Subtration(20, 5);
24
25 assertEquals(15, c);
26 }
27}
28
SimpleCalculationTest.java
3、build.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 1<?xml version="1.0" encoding="UTF-8"?>
2<project name="AntDemo" default="junit" basedir=".">
3 <!-- =================================================================== -->
4 <!-- 变量设置 -->
5 <!-- =================================================================== -->
6
7 <!-- 源代码src路径 -->
8 <property name="src.path" value="src/java"/>
9 <!-- 单元测试代码路径 -->
10 <property name="test.path" value="src/test"/>
11 <!-- 编译文件class路径 -->
12 <property name="build.path" value="build"/>
13 <!-- jar包路径 -->
14 <property name="dist.path" value="dist"/>
15 <!-- lib包路径 -->
16 <property name="lib.path" value="lib"/>
17 <!-- 生成报告junit4.xml路径 -->
18 <property name="report.path" value="report"/>
19
20 <!-- =================================================================== -->
21 <!-- 设置classpath -->
22 <!-- =================================================================== -->
23 <path id="compile.path">
24 <fileset dir="${lib.path}">
25 <include name="**/*.jar"/>
26 </fileset>
27
28 <pathelement path="${build.path}"/>
29 </path>
30
31 <!-- 初始化 -->
32 <target name="init">
33 <mkdir dir="${build.path}"/>
34 <mkdir dir="${report.path}"/>
35 <mkdir dir="${dist.path}"/>
36 </target>
37
38 <!-- =================================================================== -->
39 <!-- 清除历史编译class -->
40 <!-- =================================================================== -->
41 <target name="clean" description="clean">
42 <delete dir="${build.path}"/>
43 <delete dir="${report.path}"/>
44 <delete dir="${dist.path}"/>
45 </target>
46
47 <!-- =================================================================== -->
48 <!-- 编译测试文件,初始化目录 -->
49 <!-- =================================================================== -->
50 <target name="compile" depends="init">
51 <javac srcdir="${src.path}" destdir="${build.path}" classpathref="compile.path" includeantruntime="true"/>
52 <javac srcdir="${test.path}" destdir="${build.path}" classpathref="compile.path" includeantruntime="true"/>
53 </target>
54
55 <!-- =================================================================== -->
56 <!-- 执行测试案例 -->
57 <!-- =================================================================== -->
58 <target name="junit" depends="compile">
59 <junit printsummary="true" fork="true">
60 <formatter type="xml" usefile="true"/>
61
62 <classpath refid="compile.path"/>
63
64 <batchtest fork="on" todir="${report.path}" haltonfailure="no">
65 <fileset dir="${build.path}">
66 <include name="**/*Test.class"/>
67 </fileset>
68 </batchtest>
69 </junit>
70 </target>
71
72 <target name="junit-report" depends="junit">
73 <!-- 产生单元测试报表文档 -->
74 <junitreport todir="${report.path}">
75 <fileset dir="${report.path}">
76 <include name="TEST-*.xml" />
77 </fileset>
78
79 <report format="frames" todir="${report.path}" />
80 </junitreport>
81 </target>
82
83 <target name="make-jar" depends="compile" description="make jar file">
84 <jar jarfile="${dist.path}/AntDemo.jar">
85 <fileset dir="${build.path}">
86
87 <!--除去test文件-->
88 <exclude name="**/*Test.class"/>
89 </fileset>
90 </jar>
91 </target>
92
93</project>
94
build.xml
三、配置Jenkins:
********PS:Jenkins可以通过master/slave来支持分布式的job运行,本文运行在master,即Jenkins所在的机器。
**** 1、打开jenkins首页,新建一个job,输入Item名称,选择 构建一个自由风格的软件项目,点击"OK"
2、在 源码管理 那里,选择Subversion,在Repository URL后面,输入你的SVN地址。
PS:Repository URL使用本地磁盘当仓库这种方法后来我在其它机器上试验时,发现老是报错:svn: E180001: Unable to open an ra_local session to URL。一时没有找到解决办法,大家如果也碰到此问题,可以搭建SVN服务器来管理源代码,我试了,挺好使的。
3、在 构建 那里也可以有两种做法:
I、选择Execute Windows batch command,在输入框输入如下命令(这里我选择的是这个方法):
set path=C:\ANT_HOME\Apache-Ant-1.7.0\bin;path 把ant的安装目录添加到path
ant junit 执行junit task**
**II、方法I比较麻烦,如果我们设置好了ANT_HOME,可以选择Invoke Ant,然后在targets里面指定我们build.xml里的task name。
4、点击保存,然后选择立即构建,执行结果: