Delphi的IDE在一般工程的编译、部署是非常方便的,但是当工程的规模比较庞大时,使用IDE来进行工程的构建就比较费劲了。这时我们可以使用shell脚本来完成这个任务。但是脚本受限制于操作系统平台,管理起来不方便。如果能把所有的项目的构建统一在Ant和NAnt环境下,那么项目的管理效率就可以大大提升,成本就可以大大下降。
每日构建的实际内容应该包括:代码更新、编译、单元测试、打包、冒烟测试等基本内容。下面的构建文件并不包括所有这些内容。
本系列包括用Ant来进行Delphi、Java的每日构建,用NAnt进行.NET的每日构建。
Ant的构建文件Build.xml,使用时可以用实际的工程名替代*****。
<project default="usage" basedir=".">
<!-- =================================================================== -->
<!-- Initialization target -->
<!-- =================================================================== -->
<target name="init">
<tstamp/>
<buildnumber/>
<property environment="env" />
<property file="build.number"/>
<!-- You have to fill the ***** space with your project label. -->
<property name="Name" value="*****"/>
<property name="name" value="*****"/>
<property name="pname" value="*.dpr"/>
<property name="infofile" value="compiledinfo.txt"/>
<property name="buildname" value="*****build"/>
<property name="packname" value="*****"/>
<property name="dbpackname" value="*****DB"/>
<property name="year" value="2004"/>
<echo message="----------- ${Name} Build ${build.number}[${year}] ------------"/>
<property name="build.compiler" value="modern"/>
<property name="debug" value="on"/>
<property name="optimize" value="on"/>
<property name="deprecation" value="on"/>
<property name="s.home" value=".."/>
<property name="s.src" value="${s.home}/src"/>
<property name="s.srcdelphi" value="${s.home}/src/delphi"/>
<property name="s.gohome" value="../"/>
<property name="s.lib" value="${s.home}/lib"/>
<property name="s.res" value="${s.home}/res"/>
<property name="s.build" value="${s.home}/build"/>
<property name="s.run" value="${s.home}/run"/>
<property name="s.build.assemble" value="${s.build}/assemble"/>
<property name="s.build.dcu" value="${s.build.assemble}/dcu"/>
<property name="s.build.bin" value="${s.build.assemble}/bin"/>
<property name="s.build.gensrc" value="${s.build.assemble}/gen-src"/>
<property name="s.build.delphidocs" value="${s.build}/apidocs"/>
<property name="s.build.lib" value="${s.home}/res"/>
<property name="s.build.release" value="${s.build}/release"/>
<property name="s.build.web" value="${s.build}/web"/>
<property name="s.build.dist" value="${s.build}/dist"/>
<property name="s.main" value="com"/>
<property file="${s.build}/build.properties" />
<filter token="year" value="${year}"/>
<filter token="version" value="${version}"/>
<filter token="date" value="${TODAY}"/>
<filter token="log" value="true"/>
<filter token="verbose" value="true"/>
<path id="s.classpath">
<fileset dir="${s.lib}">
<include name="**/*.jar"/>
<include name="**/*.zip"/>
</fileset>
</path>
<path id="buildtools.classpath">
<fileset dir="${s.build.lib}">
<include name="**/*.jar"/>
<include name="**/*.zip"/>
</fileset>
<fileset dir="${s.lib}">
<include name="**/*.jar"/>
<include name="**/*.zip"/>
</fileset>
</path>
<tstamp>
<format property="TODAY" pattern="d-MM-yy"/>
</tstamp>
</target>
<!-- =================================================================== -->
<!-- Help on usage -->
<!-- =================================================================== -->
<target name="usage">
<echo message=""/>
<echo message=""/>
<echo message="***** Build file"/>
<echo message="-------------------------------------------------------------"/>
<echo message=""/>
<echo message=" available targets are:"/>
<echo message=""/>
<echo message=" package --> generates the *****.zip file (default)"/>
<echo message=" compile --> compiles the source code"/>
<echo message=" test --> units test"/>
<echo message=" release --> build the installation package"/>
<echo message=" clean --> cleans up the directory"/>
<echo message=""/>
<echo message=" See the comments inside the build.xml file for more details."/>
<echo message="-------------------------------------------------------------"/>
<echo message=""/>
<echo message=""/>
</target>
<!-- =================================================================== -->
<!-- Compiles the source directory -->
<!-- =================================================================== -->
<target name="compile" depends="init">
<mkdir dir="${s.build.dcu}"/>
<mkdir dir="${s.build.bin}"/>
<echo message=""/>
<echo message="Compiling application main source..."/>
<!-- You may change the compile tasks here. -->
<exec dir="${s.srcdelphi}" executable="dcc32.exe" os="Windows 2000"
output="${infofile}">
<arg line=" -B ${pname}
-N${s.gohome}${s.build.dcu} -E${s.gohome}${s.build.bin}
-U${s.gohome}${s.build.lib}"/>
</exec>
<loadfile property="compiled-info"
srcFile="${infofile}"/>
<condition property="compile-success">
<and>
<contains string="${compiled-info}" substring="seconds"/>
</and>
</condition>
<fail unless="compile-success"
message="${compiled-info}"/>
</target>
<!-- =================================================================== -->
<!-- Creates the zip package -->
<!-- =================================================================== -->
<target name="zip" depends="compile">
<!-- You may change the package tasks here. -->
<mkdir dir="${s.build.assemble}/zip/${buildname}${DSTAMP}/${packname}"/>
<mkdir dir="${s.build.assemble}/zip/${buildname}${DSTAMP}/${dbpackname}"/>
<copy todir="${s.build.assemble}/zip/${buildname}${DSTAMP}/${packname}">
<fileset file="${s.build.bin}/*.*"/>
<fileset dir="${s.lib}" />
</copy>
<copy todir="${s.build.assemble}/zip/${buildname}${DSTAMP}/${dbpackname}">
<fileset file="${s.src}/database/*.*"/>
<fileset file="${s.src}/sql/*.*"/>
</copy>
<zip destfile="${s.build.assemble}/zip/${buildname}${DSTAMP}.zip"
basedir="${s.build.assemble}/zip/${buildname}${DSTAMP}"
update="true"
/>
</target>
<!-- =================================================================== -->
<!-- Creates the test -->
<!-- =================================================================== -->
<target name="test" depends="compile">
<!-- You may fill the tasks here. -->
</target>
<!-- =================================================================== -->
<!-- Creates the deploy -->
<!-- =================================================================== -->
<target name="deploy" depends="">
<!-- You may fill the tasks here. -->
</target>
<!-- =================================================================== -->
<!-- Build the installation packge -->
<!-- =================================================================== -->
<target name="release" depends="clean">
<!-- You may fill the tasks here. -->
</target>
<!-- =================================================================== -->
<!-- Clean targets -->
<!-- =================================================================== -->
<target name="clean" depends="init">
<delete dir="${s.build.assemble}"/>
<delete dir="${s.build.release}"/>
<delete >
<fileset dir="." includes="**/compile*.txt"/>
</delete>
<!-- You may fill the tasks here. -->
</target>
</project>
<!-- End of file -->
下面是过去写的一个建立Delphi工程自动化每日构建的帮助文档,内容是英文的。
Start a new Delphi project by using Ant
This document has 5 sections:
Brief
System requirement
Directories and files
The Build file
Run Ant
Brief
Start a new Delphi project by using Ant, so we can make Daily Building. All have to do list below:
1.Install Delphi SDK1.4 and new version Ant, and make Ant works.
2.Copy Directories and files from dproject(d for Delphi) template library.
3.Make project files stay in the right directory.
4.Edit the Build.xml file. Let it fit the project.
5.Run Ant.
System requirement
Delphi SDK1.4 or higher
Ant 1.6 or higher
Delphi 6.0 or higher
Directories and files
You can make directories yourself, or copy from template. But make sure the directory tree like this:
Every directory is made for a certain kind of files. Directory names and what kind of files should be put in list below:
Directory Files should be put in
Build building files
Data project’s data files
Doc project‘s documents for installation and deploying
Lib libraries project depending on
Res resources project using
Install resources for installation
Src project’ source files
Config project’s configuration files
Database project’s database files
Delphi project’s Delphi source code files
Docs project’s documents for manager, developer, tester
Scripts project’s script files
Sql project’s script files for database
Now put the Delphi files into the /src/Delphi directory.
The Build file
The build file is /build/build.xml. In the template we already have a default build file. Because every project has different name and different configuration, so we must edit the build file to suit project. We must edit the project name and path in the build file.
Run Ant
After doing that, now start a command-line prompt, change path to ./build, and type ant. We can see a function list like this:
Now Ant runs. We can EDIT the build file to add new features: unit testing, packing, deploying, etc, as you like.