C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Building .NET Applications using NAnt


Posted Date: 08 Nov 2004    Resource Type: Articles    Category: .NET Framework
Author: Nirmala PMember Level: Bronze    
Rating: 1 out of 5Points: 10



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 :-)





Responses

Author: Narayana Swamy     17 Nov 2004Member Level: Bronze   Points : 0
Hi

I feel so happy after reading in this article. I need some more details regarding in this tool. How to build the Web Based applications.

with regards
Y.Narayana Swamy
9885200480


Author: v chander rao    10 Feb 2005Member Level: Gold   Points : 0
your article is good for me to builkd windows application. but i have a problem in building ASP.NET application. Could you please help me in this regard

thanking you
regards


Author: Ashwani Kumar    03 May 2005Member Level: Bronze   Points : 0
I am working on complex web based application.
It has 2 solution and each one containing 25 and 30 projects.
I tried to build solution using NANT but i could not so i wrote a batch file which build on dotnet. It has constraint like it will work only if Visual studio is installed on the build machine.

Hope u got it.

Ashwani




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: .NET Exception Handling Part 2 (Creating and Throwing Custom Exceptions)
Previous Resource: How to upload a file with maximum size (MB)
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use