您的位置:首页 > 其它

利用ant脚本 自动构建svn增量/全量 系统程序升级包

2015-09-11 18:35 591 查看
一、基本流程



值得一提的是jar包这个部分,这个步骤是为下面编译增量包做准备的。因为增量包导出的增量文件,它依赖于整个项目的其他代码,如果没有这些代码的支持是 编译不通过。然而又不能直接通过diff得到增量的class,所以只能导出增量文件后,通过引用全部工程的代码的class再进行编译即可。

二、运行环境


1、安装jdk,不会自己上网查其他的

2、如果你还没有安装ant,那么你可以参考:http://www.cnblogs.com/hoojo/archive/2013/06/14/java_ant_project_target_task_run.html

会介绍一下ant的安装和使用的方法。

3、这里需要用到svn的ant相关工具包、命令支持。你需要下载svnant-1.3.1.zip,将里面的lib库放置在你的ant脚本的编译运行环境中。

4、因为某些项目使用到了泛型、annotation注解,使用javac有些代码是编译不通过的,所以这里使用了jdt的编译方式。参考:使用eclipse JDT compile class 会有很详细的介绍。

需要用到

jdtCompilerAdapter.jar
org.eclipse.jdt.compiler.tool_1.0.1.v_793_R33x.jar
org.eclipse.jdt.core_3.3.3.v_793_R33x.jar
org.eclipse.jdt.debug.ui_3.2.102.v20071002_r332.jar
复制到ant_home/lib目录下,如果是利用eclipse运行脚本就需要把它加载到运行环境中。可以参考上面的:使用eclipse JDT compile class



三、编写ant的build脚本



简单介绍下目录结构:

src下面的ExportIncrementFiles.java是导出增量文件要用的,它在build命令increment中执行。它会读取diff 比较后的文件中的内容,并导出文件

dest 是checkout出来最新的svn的工程

dist 是编译上面dest目录中的工程,也是svn全量war的工程目录和jar

increment_dest 是增量工程,也就是上面的ExportIncrementFiles工具导出的工程

increment_dist 是编译上面increment_dest 的工程,也是增量包的工程目录

因为每个人的项目工程目录结构不一样,所以这个脚本并不会通用,我这里指针对自己的项目进行测试。

lib中是运行环境需要的jar库,其中主要的就是svnlib 这个你可以去下载 svnant-1.3.1.zip 以及JDT编译class的jar包,这个可以通过eclipse中的plugin中的jar包找到,可以参考:使用eclipse JDT compile class

increment.export.jar就是ExportIncrementFiles的class打成的jar包,这个是自己打的包,可以直接应用class也可以的,在increment命令引用,jar下载:http://download.csdn.net/detail/ibm_hoojo/6501165

build.properties是当前build的配置文件

build.xml就是主要的ant脚本

选中部分是打的war包,这个就可以部署了

patch.txt就是svn的diff 比较出的增量文件的目录路径列表

package com.hoo.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;

/**
* <b>function:</b> 导出在增量的文件
* @file ExportIncrementFiles.java
* @package com.hoo.util
*/
public class ExportIncrementFiles {
/**
* <b>function:</b> 导出增量文件
* @author hoojo
* @createDate 2013-11-2 下午10:15:43
* @param configPath 增量文件路径配置目录
* @param baseDir 基本路径 目标位置
* @param destDir 增量文件保存位置
* @throws Exception
*/
private static void export(String configPath, String baseDir, String destDir) throws Exception {
String srcFile = baseDir + configPath;
String desFile = destDir + configPath;
int lastIndex = desFile.lastIndexOf("/");
String desPath = desFile.substring(0, lastIndex);
File srcF = new File(srcFile);
if(srcF.exists()){//如果不存在这样的源文件,就不再拷贝,这个用来解决版本之间有删除文件的情况。
File desF = new File(desFile);
File desP = new File(desPath);
if(!desP.exists()) {
desP.mkdirs();
}
System.out.println(srcFile);
FileInputStream fis = new FileInputStream(srcF);
FileOutputStream fos = new FileOutputStream(desF);

byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1) {
fos.write(buf,0,len);
}
fos.flush();
fos.close();
fis.close();
}
}

/**
* <b>function:</b> 主函数 执行导出增量包任务
* @param args 参数1 增量包导出文件路径,参数2 要导出的文件的所在目标位置,参数3 增量包导出保存的位置路径
*/
public static void main(String[] args) {
if (args.length > 0) {
if (args.length == 1 && "help".equals(args[0])) {
System.out.println("args[0] is Export Increment Files content path");
System.out.println("args[1] is Export Increment Files target path");
System.out.println("args[2] is Increment Files Export loaction");
} else {
String configPath = args[0];
String baseDir = args[1];
String destDir = args[2];
try {
BufferedReader br = new BufferedReader(new FileReader(configPath));
String s = null;
while((s = br.readLine()) != null) {
s = s.trim();//去掉路径前面的空格
String str = destDir + s;
if(!destDir.equals(str)){//过滤空行
export(s, baseDir, destDir);
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}


main函数参数看注释,主要就是读取patch.txt增量文件路径,导出文件到指定目录的。

<?xml version="1.0" encoding="UTF-8" ?>
<!-- createDate 2013-10-28 -->
<!-- author hoojo & http://blog.csdn.net/IBM_hoojo & http://hoojo.cnblogs.com -->
<project default="checkout" basedir=".">
<property file="build.properties"/>
<!-- svn 比较项目最新路径 -->
<property name="svn.url" value="${svn._url}"/>
<!-- svn 备份路径-->
<property name="bak.svn.url" value="${bak.svn._url}"/>
<property name="svn.username" value="${svn.username}"/>
<property name="svn.password" value="${svn.password}"/>
<!-- 项目名称 -->
<property name="webapp" value="${webapp.name}"/>
<!-- 目标项目的Web 名称(WEB-INF上一级的目录名称) -->
<property name="webroot" value="${web.root}"/>
<!-- svn改动文件列表信息 -->
<property name="compare.path.file" value="${increment.file}"/>
<!-- svn导出/切出文件存放目录 -->
<property name="dest.path" location="dest/${webapp}"/>
<!-- svn导出/切出文件编译后存放目录 -->
<property name="dist.path" location="dist/${webapp}"/>
<!-- svn增量文件保存目录 -->
<property name="increment.dest.path" location="increment_dest/${webapp}"/>
<!-- svn增量文件编译后保存目录 -->
<property name="increment.dist.path" location="increment_dist/${webapp}"/>
<!-- 利用jdt编译class 解决泛型不能转换的问题 需要将
jdtCompilerAdapter.jar
org.eclipse.jdt.compiler.tool_1.0.1.v_793_R33x.jar
org.eclipse.jdt.core_3.3.3.v_793_R33x.jar
org.eclipse.jdt.debug.ui_3.2.102.v20071002_r332.jar
复制到ant_home/lib目录下
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
-->
<path id="svnant.classpath">
<fileset dir="${basedir}">
<include name="**/*.jar"/>
</fileset>
</path>
<path id="buildpath">
<fileset dir="${dest.path}">
<include name="**/lib/*.jar"/>
</fileset>
<fileset dir="C:/Program Files/Java/jdk1.6.0_13">
<include name="**/*.jar"/>
</fileset>
</path>
<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.classpath"/>
<svnSetting id="svn.settings" javahl="false" svnkit="true" username="${svn.username}" password="${svn.password}" failonerror="true"/>
<target name="init" description="init clean dirs">
<echo message="${svn.username}"/>
<echo message="${svn.password}"/>
<echo message="${webapp}"/>
<echo message="${webroot}"/>
<echo message="${compare.path.file}"/>
<delete dir="${dest.path}" failonerror="false" deleteonexit="true" excludes="**/lib"/>
<delete dir="${dist.path}" failonerror="false" deleteonexit="true" excludes="**/lib"/>
<delete file="${compare.path.file}" failonerror="false"/>
<delete dir="${increment.dest.path}" failonerror="false" deleteonexit="true"/>
<delete dir="${increment.dist.path}" failonerror="false" deleteonexit="true"/>
</target>

<!-- that is to test i svnant is available //-->
<target name="tool-available" depends="init">
<echo message="run task test svnant is available"></echo>
<available resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.classpath" property="available.svnant"/>
<echo message="SVN-ANT is available = ${available.svnant}"></echo>
</target>
<!-- 比较差异 增量文件 -->
<target name="diff" description="deff/compare project">
<svn refid="svn.settings">
<diffSummarize oldUrl="${bak.svn.url}" newUrl="${svn.url}" outFile="${compare.path.file}" recurse="true"/>
</svn>
</target>
<!-- 下载 切成 导出 服务器上最新代码 -->
<target name="checkout" depends="tool-available" description="checkout/export project code ${svn.url} ">
<echo message="checkout/export project code ${svn.url}"></echo>
<svn refid="svn.settings">
<export srcUrl="${svn.url}" destPath="${dest.path}" revision="HEAD" force="true"/>
</svn>
</target>

<!-- javac编译 -->
<target name="compile">
<buildnumber/>
<echo>compile ${dest.path} ......</echo>
<delete dir="${dist.path}" failonerror="false" deleteonexit="true" excludes="**/lib"/>
<mkdir dir="${dist.path}/classes"/>
<javac nowarn="true" debug="${javac.debug}" debuglevel="${javac.debuglevel}" destdir="${dist.path}/classes" source="${javac.source}" target="${javac.target}" encoding="utf-8" fork="true" memoryMaximumSize="512m" includeantruntime="false">
<src path="${dest.path}/src"/>
<!--
<compilerarg value="-Xlint:unchecked"/>
<compilerarg value="-Xlint:deprecation"/>
<compilerarg value="-Xlint"/>
-->
<classpath refid="buildpath"/>
<classpath refid="svnant.classpath"/>
</javac>
</target>

<!-- 利用JDT编译 -->
<target name="compile_jdt">
<buildnumber/>
<echo>compile ${dest_path} ......</echo>
<delete dir="${dist_path}" failonerror="false" deleteonexit="true" excludes="**/lib"/>
<mkdir dir="${dist_path}/classes"/>

<javac compiler="org.eclipse.jdt.core.JDTCompilerAdapter" nowarn="true" debug="${javac.debug}" debuglevel="${javac.debuglevel}" destdir="${dist_path}/classes" source="${javac.source}" target="${javac.target}" encoding="utf-8" fork="true" memoryMaximumSize="512m" includeantruntime="false">
<src path="${dest_path}/src"/>

<classpath refid="buildpath"/>
<classpath refid="svnant.classpath"/>
</javac>
</target>

<!-- 利用JDT编译SVN 最新项目 -->
<target name="compile_svn">
<!-- 回调任务 -->
<antcall target="compile_jdt">
<param name="dest_path" value="${dest.path}"/>
<param name="dist_path" value="${dist.path}"/>
</antcall>
</target>

<!-- 将全部项目的class 建立jar包 -->
<target name="jar" depends="compile_svn">
<jar destfile="${basedir}/lib/${webapp}.jar" level="9" compress="true" encoding="utf-8" basedir="${dist.path}/classes">
<manifest>
<attribute name="Implementation-Version" value="Version: 2.2"/>
</manifest>
</jar>
</target>

<!-- 导出增量文件 -->
<target name="increment" depends="diff">
<java classname="com.hoo.util.ExportIncrementFiles" classpath="${basedir}/lib/increment.export.jar" fork="true">
<arg value="${compare.path.file}"/>
<arg value="${dest.path}/"/>
<arg value="${increment.dest.path}/"/>
</java>
</target>

<!-- 利用JDT编译增量文件 -->
<target name="compile_increment">
<antcall target="compile_jdt">
<param name="dest_path" value="${increment.dest.path}"/>
<param name="dist_path" value="${increment.dist.path}"/>
</antcall>
</target>

<!-- 全部打包 -->
<target name="war">
<echo>create war file.......</echo>

<copy todir="${dist_path}" failonerror="false">
<fileset dir="${dest_path}/${webroot}" includes="**"/>
</copy>
<move todir="${dist_path}/WEB-INF/classes" failonerror="false">
<fileset dir="${dist_path}/classes" />
</move>
<copy todir="${dist_path}/WEB-INF/classes" failonerror="false">
<fileset dir="${dest_path}/src/main/" includes="**/*.xml, **/*.properties, **/*.xsd"/>
<fileset dir="${dest_path}/src/test/" includes="**/*.xml, **/*.properties, **/*.xsd"/>
<fileset dir="${dest_path}/src/resource/" includes="**/*.xml, **/*.properties, **/*.xsd"/>
</copy>

<!--得到当前日期-->
<tstamp>
<format property="DSTAMP" pattern="yyyyMMdd" locale="zh"/>
<format property="TSTAMP" pattern="HHmmss" locale="zh"/>
</tstamp>

<war destfile="${basedir}/${webapp}_${DSTAMP}_${TSTAMP}.war" basedir="${dist_path}" webxml="${dist_path}/WEB-INF/web.xml"/>
</target>

<!-- 全部打包 -->
<target name="war_svn">
<antcall target="war">
<param name="dest_path" value="${dest.path}"/>
<param name="dist_path" value="${dist.path}"/>
</antcall>
</target>

<!-- 全部打包 -->
<target name="war_increment">
<copy todir="${increment.dist.path}/WEB-INF" file="${dest.path}/${webroot}/WEB-INF/web.xml"/>
<antcall target="war">
<param name="dest_path" value="${increment.dest.path}"/>
<param name="dist_path" value="${increment.dist.path}"/>
</antcall>
</target>

<!-- svn 全量包 -->
<target name="svn_war" depends="checkout, compile_svn, war_svn"/>
<!-- 增量包 -->
<target name="increment_war" depends="checkout, increment, jar, compile_increment, war_increment"/>
</project>


#Mon, 04 Nov 2013 11:18:12 +0800
svn._url=http://172.31.100.100/svn/iMVS_DataComm2
bak.svn._url=http://172.31.100.100/svn/iMVS_DataComm
svn.username=hoojo
svn.password=mypass
webapp.name=iMVS_DataComm
web.root=WebRoot
increment.file=patch.txt

javac.debuglevel=source,lines,vars
javac.target=1.6
javac.source=1.6
javac.debug=true

运行svn_war任务可以打全部的包,也就是svn最新地址的项目工程包。
运行increment_war任务可以打增量包,也会形成一个war文件。
如果你需要发布到tomcat目录,可以写一个任务copy相关war包到tomcat的webapps的目录下,这个很简单~如果你需要调用tomcat的相关任务或命令,你需要在build脚本中加入
<target name="_def_tomcat_tasks">
<!-- tasks: deploy,undeploy,reload,stop,start,list,roles,resources -->
<taskdef name="deploy"    classname="org.apache.catalina.ant.DeployTask" />
<taskdef name="list"      classname="org.apache.catalina.ant.ListTask" />
<taskdef name="reload"    classname="org.apache.catalina.ant.ReloadTask" />
<taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask" />
<taskdef name="roles"     classname="org.apache.catalina.ant.RolesTask" />
<taskdef name="start"     classname="org.apache.catalina.ant.StartTask" />
<taskdef name="stop"      classname="org.apache.catalina.ant.StopTask" />
<taskdef name="undeploy"  classname="org.apache.catalina.ant.UndeployTask" />
</target>
关于这些命令使用方法有兴趣的可以自己研究研究。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: