Building .NET Applications using NAnt
Even you don't have VS.NET installed on your machine still you can build .NET application provided you have installed .NET Framework on your machine
This Article contain the following Agenda
•Build Process
•Build Tools
•NAnt Introduction
•Why NAnt
•Advantages of NAnt
•NAnt Usage
•Build Output
•Examples
Common steps in Build Process
Steps required to transform the source into a deployable and useable software solution
•Prepare build area
•Fetch all the code for building
•Set version number for Software
•Compile the source code
•Building compiled code into libraries
•Run the system tests to run the build
•Reporting Build result
•Deploy the solution to some standard location or distributing
•All these steps have individual operations.
•If we try to follow manual process for building our software, we may find it error prone and some repeatable tasks.
•To avoid this we can automate the Build Process by using Build tools
Build Tools
1.Make
2.IBM’s ODE (Open Development Environment)
3.Ant - is a Java based build tool
These support building C/C++/Java Applications, but are not limited to these.
Ant is Java Specific and used Java Runtime
Introduction to NAnt
•NAnt is a .NET implementation of Ant and it is written in C#.
•NAnt can be used to build .NET projects.
NAnt can under .NET Framework/ mono Framework
Prerequisites of NAnt
. should have installed .NET framework
. should have installed NUnit if you are building unit test classes build on NUnit
. NAnt script can be written manually in a notepad or you can use NAnt pad which is a gui application for creating NAnt script
Advantages of NAnt
•NAnt is used to build, compile and run the .NET projects
•NAnt is platform-independent. For this, it used XML format script for building projects
•NAnt can easily handle build, though the different modules of the project are written in different languages like C#, VB etc.,
•NAnt also integrates with unit testing tool NUnit 2.0
•NAnt also can do other tasks like creating/deleting/copying files or directories, sending emails, zipping files.
•You can order the tasks and group the different tasks logically into one called as “Target”.
NAnt Usage
•Download NAnt from http://nant.sourceforge.net
•Unzip the downloaded NAnt
•NAnt file name extension is .build
•Create default.build file for your .NET application in the same Application directory
•Execute command eg:
D:\nant-bin\NANT-0~1.85-\bin> nant -buildfile: D:\Nirmala\ApplicationBlocksTest\prjHelloWorld\default.build
•If you want to execute any one target (group of tasks) then use NAnt
Eg: D:\nant-bin\nant-0.85-nightly\bin> nant build -buildfile: D:\Nirmala\ApplicationBlocksTest\prjHelloWorld\default.build
Examples
•Let us see how to create NAnt script to build and run a sample code written in C#.
public class HelloWorld
{
static void Main()
{
System.Console.WriteLine("Hello world.");
}
}
<?xml version="1.0"?>
<project name="Hello World" default="run" basedir="."> <target name="build">
<csc target="exe" output="HelloWorld.exe"> <sources>
<includes name="HelloWorld.cs"/>
</sources>
</csc>
</target>
<target name="run" depends="build">
<exec program="HelloWorld.exe"/>
</target></project>
Creating Clean Build
<?xml version="1.0"?>
<project name="Hello World" default="run" basedir=".">
<target name="build">
<mkdir dir="bin" />
<csc target="exe" output="bin\HelloWorld.exe"> <sources>
<includes name="HelloWorld.cs"/>
</sources>
</csc>
</target>
<target name="clean">
<delete dir="bin" failonerror="false"/>
</target>
<target name="run" depends="build">
<exec program="bin\HelloWorld.exe"/>
</target>
</project>
Integrating with NUnit 2.0
<?xml version="1.0"?>
<project name="NUnit Integration" default="test">
<property name="build.dir" value="\dev\src\myproject\“ />
<target name="build">
<csc target="library" output="account.dll">
<sources>
<includes name="account.cs" />
</sources>
</csc>
</target>
<target name="test" depends="build">
<csc target="library" output="account-test.dll">
<sources>
<includes name="account-test.cs" />
</sources>
<references>
<includes name="nunit.framework.dll" />
<includes name="account.dll" />
</references>
</csc>
<nunit2>
<test assemblyname="${build.dir}account-test.dll" /> </nunit2>
</target>
</project>
Other Example
<?xml version="1.0"?>
<project name="default" default="build" basedir=".">
<property name="nant.settings.currentframework" value="net-1.1" />
<property name="basename" value="DataAccessTest" />
<property name="debug" value="true" />
<property name="build.dir" value="output" />
<property name="src.dir" value="." />
<property name="filename" value="C:\CruiseLog" />
<target name="clean" description="cleans build directory">
<echo message="Deleting ${build.dir}\${basename}.exe" />
<delete dir="${build.dir}" failonerror="false" verbose="true" />
<mkdir dir="${build.dir}" />
</target>
<target name="build" depends="clean">
<tstamp>
<formatter property="DSTAMP" pattern="yyyy-MM-dd" />
<formatter property="TSTAMP" pattern="HH:mm" />
</tstamp>
<echo message="Build started at : ${DSTAMP} - ${TSTAMP}" />
<solution configuration="debug">
<projects>
<includes name="${basename}.csproj" />
</projects>
</solution>
</target>
<target name="test" depends="test.unit" />
<target name="test.unit" depends="build" description="runs unit tests">
<foreach property="filename" item="File">
<in>
<items>
<includes name="bin/debug/*.exe" />
<includes name="bin/debug/*.dll" />
</items>
</in>
<do>
<exec program="C:\Program Files\NUnit V2.1\bin\nunit-console.exe" commandline="${filename} /xml:${filename}-results.xml" />
</do>
</foreach>
<csc output="" target="">
<references>
<includes name="bin\debug\DataAccessTest.exe" />
<includes name="C:\Program Files\NUnit V2.1\bin\nunit-console.exe" />
</references>
</csc>
</target>
</project>
CRUISE CONTROL
Download Cruise control.net, SubVersion, TortoiseSVN, NUnit 2.0 client from website
Install all softwares
In cruise control.net source code\project\console\bin\Debug there is ccnet.config in which you can specify path for application and NAnt
eg:-
<cruisecontrol>
<project name="DataAccessTest">
<webURL>http://localhost/ccnet</webURL>
<schedule type="schedule" sleepSeconds="60"/>
<sourcecontrol type="svn">
<executable>C:\program files\subversion\bin\svn.exe</executable>
<trunkUrl>file:///C:/MyNewRepository</trunkUrl>
</sourcecontrol>
<build type="nant">
<executable>D:\Nirmala\Nant\nant 0.84\bin\nant.exe</executable>
<baseDirectory>D:\NewBuild\MyNewRepository</baseDirectory>
<buildFile>default.build</buildFile>
<targetList>
<target>build</target>
</targetList>
<buildTimeoutSeconds>300</buildTimeoutSeconds>
</build>
<publishers>
<email from="p.nirmala@celstream.com" mailhost="10.255.10.18" includeDetails="TRUE">
<projectUrl>http://localhost/ccnet</projectUrl>
<users>
<user name="Nirmala" group="celstream1-engg" address="p.nirmala@celstream.com"/>
</users>
<groups>
<group name="celstream1-engg" notification="always"/>
</groups>
</email>
<xmllogger>
<logDir>C:\CruiseLog</logDir>
</xmllogger>
</publishers>
</project>
</cruisecontrol>
Sub Version
Run Subversion command eg:- svnserve -d
then run Cruise control to automatically build Application with NAnt
If you want to manually build application just use NAnt command as shown in above examples
Happy building :-)