继续上一节
下面对具体的实现给出参考代码
做日编译首先要做的事情是SVN CheckOut资源参考脚本如下:
写道
<!--check out svn resource-->
<target name="checkout" description="Check Out Svn Resource">
<if>
<and>
<not>
<equals arg1="${project.name}" arg2=""/>
</not>
<not>
<available file="${project.name}" filepath="${resource.dir}"/>
</not>
</and><!--判断传递的参数project.name-->
<then>
<trycatch>
<try>
<svn username="${svnant.repository.user}" password="${svnant.repository.passwd}">
<checkout url="${svnant.this.url}${project.name}" revision="HEAD" destPath="${resource.dir}${file.separator}${project.name}"/>
</svn>
</try>
<catch><!--为了让构建结果是一种结构良好的且视觉效果良好,所以采用html方式进行构建-->
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="Error">
<td>${svnant.this.url}${project.name}</td><td>checkout failure</td></tr></echo>
</catch>
</trycatch>
</then>
<else>
<echo message="用户忽略了${project.name}的checkout"/>
</else>
</if>
</target>
<target name="checkout" description="Check Out Svn Resource">
<if>
<and>
<not>
<equals arg1="${project.name}" arg2=""/>
</not>
<not>
<available file="${project.name}" filepath="${resource.dir}"/>
</not>
</and><!--判断传递的参数project.name-->
<then>
<trycatch>
<try>
<svn username="${svnant.repository.user}" password="${svnant.repository.passwd}">
<checkout url="${svnant.this.url}${project.name}" revision="HEAD" destPath="${resource.dir}${file.separator}${project.name}"/>
</svn>
</try>
<catch><!--为了让构建结果是一种结构良好的且视觉效果良好,所以采用html方式进行构建-->
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="Error">
<td>${svnant.this.url}${project.name}</td><td>checkout failure</td></tr></echo>
</catch>
</trycatch>
</then>
<else>
<echo message="用户忽略了${project.name}的checkout"/>
</else>
</if>
</target>
以上的所有Ant任务来自于两个插件库中一种是ant基本库中。其中的if 、else、try、等均来自于扩展包ant-contrib-1.0b3.jar中。将此包放入到ant的lib目录下便可以使用。
对于具体任务的语法说明参见相应的doc文档。那里面有相似的说明和example
checkout资源后变可以编译了,无论是BFS模式还是DFS模式均需要这样的步骤,至于BFS模式和DFS模式之间的差异会在接下来的文章中说明。
写道
<target name="only-build" unless="already-complete" description="唯一构建工程,调用者必须传project.path参数和jar.name参数和encode参数"><!--此target只有在already-complete 为false情况下进行编译,否则不会进行二次编译和打包工作,虽然javac已经做到避免二次编译但时还是不能控制打包的二次打包,所以这里进行了控制-->
<trycatch property="JavacException">
<try>
<mkdir dir="${project.path}/${classes}"/>
<copy includeemptydirs="false" todir="${project.path}/${classes}">
<fileset dir="${project.path}/${src}">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
<javac destdir="${project.path}/${classes}" source="${source}" target="${target}" debug="no" encoding="${encode}" includeAntRuntime="false">
<src path="${project.path}/${src}"/>
<classpath>
<path refid="classpath"/>
<pathelement location="${project.path}/${classes}"/>
</classpath>
</javac><!--注意这里面的encoding属性,很多人都不会写这个属性,但这个属性确实必须的因为java文件也是文件,是文件就存在编码方式,为了做到国际化建议大家将java文件编码方式为utf8-->
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="info">
<td>${project.path}</td><td>compile success</td></echo>
<antcall target="junit">
<param name="project.path" value="${project.path}"/>
</antcall><!---这调用了junti测试的target 后续文章中会有介绍-->
<antcall target="findbugs">
<param name="name" value="${jar.name}"/>
<param name="project.path" value="${project.path}"/>
</antcall><!---这里调用了findbugs target 后续文章中会有介绍-->
<trycatch property="PakegeException">
<try>
<antcall target="pakege">
<param name="jar.name" value="${jar.name}"/>
<param name="project.path" value="${project.path}"/>
</antcall><!---这里的输出涉及的样式均在样式文件中定义附件中会提供-->
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="info">
<td>${jar.name}</td><td>package success</td></tr></echo>
</try>
<catch>
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="Error">
<td>${jar.name}</td><td>package failure</td></tr></echo>
</catch>
</trycatch>
</try>
<catch>
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="Error">
<td>${project.path}</td><td>compile failure</td></tr></echo>
</catch>
</trycatch>
</target>
<trycatch property="JavacException">
<try>
<mkdir dir="${project.path}/${classes}"/>
<copy includeemptydirs="false" todir="${project.path}/${classes}">
<fileset dir="${project.path}/${src}">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
<javac destdir="${project.path}/${classes}" source="${source}" target="${target}" debug="no" encoding="${encode}" includeAntRuntime="false">
<src path="${project.path}/${src}"/>
<classpath>
<path refid="classpath"/>
<pathelement location="${project.path}/${classes}"/>
</classpath>
</javac><!--注意这里面的encoding属性,很多人都不会写这个属性,但这个属性确实必须的因为java文件也是文件,是文件就存在编码方式,为了做到国际化建议大家将java文件编码方式为utf8-->
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="info">
<td>${project.path}</td><td>compile success</td></echo>
<antcall target="junit">
<param name="project.path" value="${project.path}"/>
</antcall><!---这调用了junti测试的target 后续文章中会有介绍-->
<antcall target="findbugs">
<param name="name" value="${jar.name}"/>
<param name="project.path" value="${project.path}"/>
</antcall><!---这里调用了findbugs target 后续文章中会有介绍-->
<trycatch property="PakegeException">
<try>
<antcall target="pakege">
<param name="jar.name" value="${jar.name}"/>
<param name="project.path" value="${project.path}"/>
</antcall><!---这里的输出涉及的样式均在样式文件中定义附件中会提供-->
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="info">
<td>${jar.name}</td><td>package success</td></tr></echo>
</try>
<catch>
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="Error">
<td>${jar.name}</td><td>package failure</td></tr></echo>
</catch>
</trycatch>
</try>
<catch>
<echo file="${report.dir}/html/build-result.html" append="true"><tr valign="top" class="Error">
<td>${project.path}</td><td>compile failure</td></tr></echo>
</catch>
</trycatch>
</target>
下面是样式表文件定义
写道
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns:stringutils="xalan://org.apache.tools.ant.util.StringUtils" xmlns:lxslt="http://xml.apache.org/xslt">
<head>
<META http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Build Information.</title>
<style type="text/css">
body {
font:normal 68% verdana,arial,helvetica;
color:#000000;
}
table tr td, table tr th {
font-size: 68%;
}
table.details tr th{
font-weight: bold;
text-align:left;
background:#a6caf0;
}
table.details tr td{
background:#eeeee0;
}
p {
line-height:1.5em;
margin-top:0.5em; margin-bottom:1.0em;
}
h1 {
margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
}
h2 {
margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
}
h3 {
margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
}
h4 {
margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
}
h5 {
margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
}
h6 {
margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
}
.Error {
font-weight:bold; color:red;
}
.info {
font-weight:bold; color:purple;
}
.Properties {
text-align:right;
}
</style>
</head>
<body>
<a name="top"></a>
<h1>Build Information. about more information please see the attachment's builddetail.html</h1>
<table width="100%">
<tr>
<td align="left"></td><td align="right">Designed for use with <a href="http://www.junit.org">JUnit</a> and <a href="http://ant.apache.org/ant">Ant</a>.</td>
</tr>
</table>
<table width="95%" cellspacing="2" cellpadding="5" border="0" class="details">
<tr valign="top">
<th width="80%">project-name</th><th>build-result</th>
</tr>
<html xmlns:stringutils="xalan://org.apache.tools.ant.util.StringUtils" xmlns:lxslt="http://xml.apache.org/xslt">
<head>
<META http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Build Information.</title>
<style type="text/css">
body {
font:normal 68% verdana,arial,helvetica;
color:#000000;
}
table tr td, table tr th {
font-size: 68%;
}
table.details tr th{
font-weight: bold;
text-align:left;
background:#a6caf0;
}
table.details tr td{
background:#eeeee0;
}
p {
line-height:1.5em;
margin-top:0.5em; margin-bottom:1.0em;
}
h1 {
margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
}
h2 {
margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
}
h3 {
margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
}
h4 {
margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
}
h5 {
margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
}
h6 {
margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
}
.Error {
font-weight:bold; color:red;
}
.info {
font-weight:bold; color:purple;
}
.Properties {
text-align:right;
}
</style>
</head>
<body>
<a name="top"></a>
<h1>Build Information. about more information please see the attachment's builddetail.html</h1>
<table width="100%">
<tr>
<td align="left"></td><td align="right">Designed for use with <a href="http://www.junit.org">JUnit</a> and <a href="http://ant.apache.org/ant">Ant</a>.</td>
</tr>
</table>
<table width="95%" cellspacing="2" cellpadding="5" border="0" class="details">
<tr valign="top">
<th width="80%">project-name</th><th>build-result</th>
</tr>