最近部门在搞持续集成提速,增量构建是个很重要的措施。但是实现增量构建需要识别代码库变化的代码、文件关联到的构建步骤,比如进程编译、后台出包还是其他步骤。当前我们使用的持续集成插件不支持该功能,因此针对我们产品情况,我自己写了一个自定义任务,来进行代码库变化与任务的识别。
我们使用的插件功能都是jar包,通过ant脚本来进行调用,因此实现自定义任务,也就是做一个Ant的自定义任务。
Ant开发自定义任务,一般都是通过java开发,在eclipse中开发,步骤如下:
1、创建java工程;
2、配置工程,引入ant.jar;
3、创建类一定要继承Task;
4、进行自定义功能开发。
比如我做的这个代码库变化与构建任务识别的代码如下:
package com.aaa.CustomTask;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class SelectBuildPhase extends Task {
private String configFile = "";
private String deltaFile = "";
private String destFile = "";
private ArrayList<BuildPhase> myBuildPhaseArray = new ArrayList<BuildPhase>();
public String getConfigFile() {
return configFile;
}
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
public String getDeltaFile() {
return deltaFile;
}
public void setDeltaFile(String deltaFile) {
this.deltaFile = deltaFile;
}
public String getDestFile() {
return destFile;
}
public void setDestFile(String destFile) {
this.destFile = destFile;
}
@Override
public void execute() throws BuildException {
try {
parseConfig();
IsNeedBuild();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void IsNeedBuild() {
//识别过程
}
private void parseConfig() throws JDOMException, IOException {
SAXBuilder mySAXBuilder = new SAXBuilder();
InputStream myInputStream = new FileInputStream(configFile);
Document myDocument = mySAXBuilder.build(myInputStream);
Element root = myDocument.getRootElement();
List<Element> elementList = root.getChildren();
StringBuffer sb = new StringBuffer();
for(Element outerTemp : elementList){
List<Element> innerList = outerTemp.getChildren();
BuildPhase bp = new BuildPhase();
bp.setName(outerTemp.getAttributeValue("name"));
bp.setDependsList(outerTemp.getAttributeValue("depends"));
for(Element inner : innerList){
sb.append(inner.getAttributeValue("name"));
sb.append(";");
}
bp.setRefDirList(sb.toString());
myBuildPhaseArray.add(bp);
sb.delete(0, sb.toString().length());
}
}
/**
* @param args
*/
public static void main(String[] args) {
SelectBuildPhase sbp = new SelectBuildPhase();
sbp.setConfigFile("D:\\config.xml");
sbp.setDeltaFile("D:\\delta.xml");
sbp.setDestFile("D:\\buildPhase.property");
sbp.execute();
}
}
代码完成后,将整个工程打包成一个jar包,然后将该jar包放入%ANT_HOME%\bin下,然后就可以通过taskdef标签来对该功能进行引用了,示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestCustomTask" default="test">
<target name="init">
<taskdef name="SelectBuildPhase"
classname="com.aaa.CustomTask.SelectBuildPhase"
classpathref="SelectBuildPhase.jar"
/>
</target>
<target name="test" depends="init">
<SelectBuildPhase
configFile="D:\\config.xml"
deltaFile="D:\\delta.txt"
destFile="D:\\result.property"
/>
</target>
</project>
至此,就完成了自定义任务的开发和使用。