持续集成安装与实践
1. 准备
(1) 从 verycd 上下了一个持续集成的服务器 bamboo ,商业的,据说比较容易上手。其实还有别的,也下了, CruiseControl 和 hudson 都有,也试了一下,确实这个比较容易上手。
(2) 看到持续集成服务器都需要从 cvs 或 svn 上下代码才能进行,于是下了 cvsnt2.5 。
(3) 由于原来用的都是 eclipse3.4 ,所以安装了 m2eclipse ,这个过程比较费劲。要注意的是要把 java_home 设到 jdk 下面。在外面环境和内部都要设。
(4) 单独的 maven 是下的 springside3allinone 里带的,打通 path 就行了。
2. 试验
( 1 )先试 maven2 ,从 springside3 的例子 min-web 导入到 eclipse 中,项目叫 ss3 。其实是以前玩这个的时候建的,现在用来试 maven2 。改了几个地方,一个是自建了一个 mysql 的库,一个是改了一下 action 的输出,改用 json 实现,不再这里说了。
( 2 )在命令行下试 mvn ,及 mvn test 。
( 3 )安了 m2eclipse 后,其实倒有点不会用 mvn 了,不知有什么用,于是开始试。
( 4 )原来的 test 处改了下,改为可以直接做集成测试,将 pom-parent.xml 的部分改为:
< artifactId > maven -surefire -plugin </ artifactId >
<!--configuration>
<excludes>
<exclude>**/integration/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>test</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/integration/**</include>
</includes>
</configuration>
</execution>
</executions-->
这个 surefire 插件是个好东西,没事也看了看。
这样才真的走了 test ,以前这里被跳过去了,还是看了江南大大的说明才了解的。
(5) 原来了 junit 用的是 3.8.1 ,看到 junit4.4 又有了很好的特性,于是改为用 4.4 。
(6) 新写了一个测试类
package ssh.integration.service.user;
import java.util.List;
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Assume;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import ssh.entity.user.User;
import ssh.service.user.UserManager;
import org.springframework.util.Assert;
/**
* UserManager 的集成测试用例 .
*
* 调用实际的 DAO 类进行操作 , 默认在操作后进行回滚(在 hibernate 中不会实际执行 SQL 操作 ).
*
* @author calvin
*/
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class junit4UserManagerTest extends AbstractTransactionalJUnit4SpringContextTests {
/**
* 也可以用 Resource , autowired 匹配类型, resource 匹配名字
*/
@Autowired
private UserManager userManager;
@Test
public void testCreateUser() {
User entity = new User();
// 因为 LoginName 要求唯一性,因此添加 random 字段。
entity.setLoginName("tester" + RandomStringUtils.randomAlphabetic(5));
userManager.saveUser(entity);
Assert.notNull(entity.getId());
}
@Test
public void testAuth() {
Assert.isTrue(userManager.auth("admin", "admin"));
Assert.isTrue(!userManager.auth("admin", ""));
}
}
其实跟原来差不太多。只是对 junit4.4 中的 assertthat 这个没有办法实现。
总之,写好了之后,去 run as junit 看到图形化的结果。
(7) 改写 pom.xml ,将 junit 写为 4.4 。试一下。
(8) 下面想把项目放到 cvs 上
先在 cvsnt 下建仓库 e:\cvsnt ,使用 unicode 方式,其实原来安这个的时候还是比较头痛的,权限问题好不容易才理解了,这里只是想试验,不是真的用,所以什么也不改,登录的时候用 windows 的 administrator 的用户名和口令。
(9) 在 eclipse 中, team->share project ,新设一个 repository
:pserver:administrator@192.168.32.164:/cvsnt 然后一路下一步下去。这里还出过一次问题,原来 eclipse 里的目录,让我在 windows 下给删了,而在上传时总是出错,说 XXX 没有什么的,我查了半天,后来无意中 refresh 了一下,就好了,郁闷!
(10) 上传完了之后,再 checkout 到另一处,运行 maven ,却总也不成,说什么没有类什么的,我还为此装了 tortoiseCVS ,其实也没什么用。
看了半天,结果是由于以前加的 josn 输出用了几个类,没有在 maven 的 dependency 里,所以 eclipse 编译过了,但 mvn 编译过不了。又看了半天如何加,出下面的语句
mvn install:install-file -Dfile=jsonplugin-0.33.jar -DgroupId=com.googlecode.jsonplugin -DartifactId=jsonplugin -Dversion=0.33 -DgeneratePom=true -Dpackaging=jar
mvn install:install-file -Dfile=ezmorph-1.0.6.jar -DgroupId=ezmorph -DartifactId=ezmorph -Dversion=1.0.6 -DgeneratePom=true -Dpackaging=jar
然后,在 pom.xml 里加了
< dependency >
< groupId > com .googlecode .jsonplugin </ groupId >
< artifactId > jsonplugin </ artifactId >
< version > 0.33 </ version >
< type > jar </ type >
< scope > compile </ scope >
</ dependency >
< dependency >
< groupId > ezmorph </ groupId >
< artifactId > ezmorph </ artifactId >
< version > 1.0.6 </ version >
< type > jar </ type >
< scope > compile </ scope >
</ dependency >
主要问题是,其中一个类是 googlecode 的,在 maven 的主类库里都找不到,所以才用了这个方式。这也用来加载一些自制的类包。
(11) 编译通过之后,开启了 bamboo ,用管理员登录, create plan ,
Source Repository 里用 cvs
CVS Root:
:pserver:administrator@192.168.32.164:/cvsnt
Module:
ss3
Build Strategy:
Repository Polling
后面设 builder
选 maven2 和 jdk
但在 administration 里重设 maven 的路径为本机 springside3 tool 里的 maven
重设 jdk 的路径为 jdk_1.5 而不用原来的 jre 。
(12) 经过多次试验之后,终于可以自动执行了,但看 test 时总也没有。所以,在 builder 设的时候要选 The build will produce test results. 而因为原来比较规范,所以,这里选 The build will produce test results. 就可以了,实际用的还是 maven-surefire 的结果。
再运行,看到 test 里有数了。
至此,大致完成了搭建的过程,以及实践一个项目的持续集成。下一步,还要试一下自动部署的过程,以及 jire 的使用。另外,再换换 hudson 和 CC 来试试。