Analytics


Google

Sunday, December 14, 2008

Compiling .Net programs with Nant

Nant is the .Net Version of Ant. It is capable of doing a lot of things as indicaed in the task reference here. However, for the purpose of this article, I am going to focus how to use it to either compile an .net assembly either to .exe or .dll.

In my previous article, I provided an example how we can write a program without using Visual Studio. Assuming you have written your program, you could use the command line vbc (for Visual Basic compiler) and csc (C# compiler) to compile. However, it is tedious if the program references a lot of libraries or there are multiple source file.

To compile using Nant, first step is to download NANT. The home page is found here. Download and unzip the zip file. Assuming you downloaded Nant 8.5, you can unzip everything to c:\nant-0.85. When unzipping, just say use the folder in the zip file then unzip it to c:\. It will automatically create a folder tree c:\nant-0.85\...

Next, create a build file:

Here is an example for compiling .exe:

<?xml version="1.0"?>
<project name="FileLimAlert" default="build" basedir=".">
<property name="debug" value="true" unless="${property::exists('debug')}" />
<property name="verbose" value="true" />
<target name="clean" description="remove all generated files">
<delete file="FileLimAlert.exe" failonerror="false" />
<delete file="FileLimAlert.pdb" failonerror="false" />
</target>
<target name="build" description="compiles the source code">
<vbc target="exe" output="FileLimAlert.exe" debug="${debug}">
<sources>
<include name="FileLimAlert.vb" />
</sources>
<references>
<include name="System.dll" />
<include name="System.Web.dll" />
<include name="System.Web.Services.dll" />
<include name="System.XML.dll" />
</references>
</vbc>
</target>
</project>


Here is a sample to compile code behind (into .dll) - assuming this is called newLdaplogin.build.xml:

<?xml version="1.0"?>
<project name="newLDAPLogin" default="build" basedir=".">
<property name="debug" value="true" unless="${property::exists('debug')}" />
<property name="verbose" value="true" />
<target name="clean" description="remove all generated files">
<delete file="bin\newLDAPLogin.dll" failonerror="false" />
<delete file="bin\newLDAPLogin.pdb" failonerror="false" />
</target>
<target name="build" description="compiles the source code">
<vbc target="library" output="bin\newLDAPLogin.dll" debug="${debug}">
<sources>
<include name="newLDAPLogin.aspx.vb" />
</sources>
<references>
<include name="System.dll" />
<include name="System.Web.dll" />
<include name="System.Data.dll" />
</references>
</vbc>
</target>
</project>


Note
  1. The target build will instruct nant what to do. In the above example to use VBC to compile either.
  2. In the first example above, the vbc target is to exe and the second one is to library.
  3. The references section within the target are for the libraries your program is referencing.
  4. The sources section within the target are for all the source code.
  5. As shown above, each section can include multiple includes, so you could have multiple source code compiling into either an exe or a dll.
  6. Clean basically instruct nant what are the files to delete.
To compile, just type (if you are building for Framework 1.1):

c:\nant-0.85\bin\nant -buildfile:newLdaplogin.build.xml -t:net-1.1

The output will look something like this:

NAnt 0.85 (Build 0.85.2478.0; release; 10/14/2006)
Copyright (C) 2001-2006 Gerry Shaw
http://nant.sourceforge.net

Buildfile: file:///C:/project/vbUtils/clsldap.build.xml
Target framework: Microsoft .NET Framework 1.1
Target(s) specified: build


build:

[vbc] Compiling 1 files to 'C:\project\vbUtils\bin\
newLDAPLogin'.

BUILD SUCCEEDED

Total time: 3.7 seconds.


If you want to build for framework 2.0 then change -t:net-1.1 to -t:net-2.0.

The build will detect is a recompilation is necessary and will not compile if the assembly is already up to date. Each version of nant will have a list of framework that is supported.

The latest version 0.86-Beta 1 supports the following framework:

FrameworkTargetRuntime
.NET Framework 1.0**
.NET Framework 1.1**
.NET Framework 2.0**
.NET Framework 3.5**
.NET Compact Framework 1.0*
.NET Compact Framework 2.0*
Mono 1.0 Profile**
Mono 2.0 Profile**
Mono 3.5 Profile**
Moonlight 2.0*
Shared Source CLI 1.0*
Silverlight 2.0*

You can check the version supported by looking into the nant.exe.config file.

No comments: