Continuous Integration实践之Implementation(续)

阅读更多

相信大家看完了上篇文章(http://gull.iteye.com/blog/1137200),会对自动化编译,部署和测试的过程与实现细节有所了解。那么我们稍微总结下就会发现其实现的实质就是一系列linux shell命令组成,且都是很常见的,如maven, ssh, scp, unzip, cp, mv, delete, kill, sed, awk等.

 

我们回想下——项目是采用maven2构建的,而我们CI自动化job的阶段也是借鉴maven2中默认的phases,因此我们做了下映射, 如下图:

Continuous Integration实践之Implementation(续)_第1张图片

 

此时你会有什么样的想法? —— 我们能否扩展maven2的生命周期,将我们不同流程的脚本嵌入到maven2的各个生命周期中,这样就可以直接使用原生的maven2命令来完成CI自动化流程了。

 

 

所幸的是maven2已经提供了这样的机制和插件,在实践中我们主要用到了下面这2个插件:

  • maven-antrun-plugin: 自定义ant task来完成代码同步(p4 sync),参数替换,打包上传(scp),集成测试(ant-jmeter)等操作
  • maven-sshexec-plugin:主要是用来执行远程服务器命令来完成清理和部署(ssh, del, unzip, scp, kill, java)等操作

这2个插件如何操作可以查看相关手册:

  http://maven.apache.org/plugins/maven-antrun-plugin/

        http://evgeny-goldin.com/wiki/Maven-sshexec-plugin

 

 

maven-antrun-plugin这个插件官方介绍得比较简单,远不能达到我们的需要,因此我们需要扩展ant task来完成CI流程的定制化。

 

如何扩展?我们将以code sync和test为例来介绍。

code sync&test stage

  • 实现org.apache.tools.ant.Task接口
public class CodeSync extends Task {
	//todo
	
	public void execute() throws BuildException {
		//todo prepare some parameters
		
		try {
			//sync the application code
			
			sync(depot, p4ClientRoot, p4ClientFolder);

		} catch (Exception e) {
			e.printStackTrace();
			throw new BuildException(e);
		}
	}


	private void sync(String depotRoot, String clientRoot, String folderPath) throws Exception {
		String depotPath = depotRoot + "/" + folderPath + "/...";
		IServer server = ServerFactory.getServer("p4java://p4host:p4port", null);
		server.connect();
		server.setCharsetName(p4Charset);
		server.setUserName(p4User);

		//login the p4 server
		server.login(p4Passwd);


		IClient client = server.getClient(p4Client);

		server.setCurrentClient(client);

		List fileSpecs = server.getDepotFiles(FileSpecBuilder.makeFileSpecList(new String[] { depotPath }), false);
		log("sync "+fileSpecs.size()+" flies");
		client.sync(fileSpecs, true, false, false, false);

		server.logout();
	}
}

 

  •  创建taskdef.properties文件
codesync=com.xxx.ant.tasks.CodeSync

 

  •  配置pom插件

		
			
				org.apache.maven.plugins
				maven-antrun-plugin
				1.6
				
					
						initialize
						initialize
						
							
								
								
								
								
				
								
									xxx.xxx.xxx
									yyyy
								
								
								
								
								
								
							
						
						
							run
						
					
					
					
					
						compile-skip-unittest
						test
						
							
								
								
																
								
								
									
																
								
								
								
								
							
						
						
							run
						
					
					
					
						compile-unit-test
						test
						
							
								
								
								
								
								
								
								
									
								
								
								
								
								
							
						
						
							run
						
					
				

				
					
						com.xxxx
						ci-anttasks
						1.0.0
					
				
			
		
	

 

在这个配置文件中,我们扩展了maven2的initialize和test周期,并通过ant来执行我们想要的操作。

 

Deploy Stage

对于deploy过程也是比较杂的,也需要按照相同的方式来扩展,并且还要配合maven-sshexec-plugin插件来完成部署操作。

pom.xml配置片段

 


	org.apache.maven.plugins
	maven-antrun-plugin
	1.6
	
		
			upload package
			pre-integration-test
			
							
															
					
					
					
						 
							
						 
					
					...
				
			
			
				run
			
		
	

	
		
			com.xxxx
			ci-anttasks
			1.0.0
		
	


	com.goldin.plugins
	maven-sshexec-plugin
	
		
		
			do deploy
			pre-integration-test
			
				sshexec
			
			
				scp://$deployserver/uploadFolder
				$keyFile
				
					echo '********************************'
					echo '==== { Run Remote Commands} ===='
					echo '********************************'					

					
						unzip xx.zip
					
					
					echo ''
					echo '==== END Run Remote Commands ===='
					echo '*********************************'
					echo ''
				
			
		
	
 

 

Integration Test Stage

目前integration test我们主要是采用jmeter来进行完成API测试的,因此我们需要配置ant jmeter task.

 


	integration-test
	
		
			
			
			
			
			
				
				  
				
			
			
			
			
			

			
			
			
			
			
		
	
	
		run
	
 

到此maven2生命周期的扩展算是结束了,但如果子项目比较多的话,每一个这样配置很是麻烦,可以通过下面2种手段解决:

 

  1. 构建maven parent project: 通过继承和参数覆盖的方式简化配置,具体方式可参考maven手册。
  2. 编写pom生成工具: 通过简单的配置参数和文件模板生成目标pom,如图所示Continuous Integration实践之Implementation(续)_第2张图片

 

 

 

——全文完

  • Continuous Integration实践之Implementation(续)_第3张图片
  • 大小: 8.6 KB
  • Continuous Integration实践之Implementation(续)_第4张图片
  • 大小: 38.1 KB
  • 查看图片附件

你可能感兴趣的:(CI,Maven,Ant,Perforce)