Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Using Nant to build a Win Forms Example

For my sample, I've chosen to expose my obviously dated CD collection to the world. I've written a simple Win Forms client that connects to SQL Server and returns a list of tracks that match your artist name criteria. In a later article, I'll show you how you can build and populate SQL Server, assuming I'm not derided too much with my choice of subject matter.

For the database-fascinated among us, I included my data model in Figure 3.

Figure 3Figure 3 Physical data model.

My finished sample should look like Figure 4.

Figure 4Figure 4 Win Forms Music Library sample.

Win Forms Build File

Nant will build my C# source files in the current directory, compile, link, and then run my application. (I've numbered my build file for ease of explanation—click here to download the source files for this article).

Here is my build file:

1 <?xml version="1.0"?>
2 <project name="MusicLib" default="run" basedir=".">
3   <property name="basename" value="MusicLib"/>
4   <property name="debug" value="true"/>
5   <property name="build.dir" value="build"/>
6  <target name="clean" description="cleans build directory">
7     <delete dir="${build.dir}" verbose="true" failonerror="false"/>
8   </target>
9   <target name="debug" depends="clean">
10     <property name="debug" value="true"/>
11   </target>
12   <target name="release" depends="clean">
13     <property name="debug" value="false"/>
14   </target>
15   <target name="build" depends="clean">
16     <mkdir dir="${build.dir}"/>
17     <csc target="exe" output="${build.dir}\${basename}.exe"
18         imports="System,System.Data,System.Drawing,System.Windows.Forms,System.XML">
19       <sources>
20         <includes name="*.cs"/>
21       </sources>
22       <references>
23         <absolute file="System.dll"/>
24         <absolute file="System.Data.dll"/>
25         <absolute file="System.Drawing.dll"/>
26         <absolute file="System.Windows.Forms.dll"/>
27         <absolute file="System.XML.dll"/>
28       </references>
29     </csc>
30   </target>
31   <target name="run" depends="build">
32     <exec program="${build.dir}/${basename}.exe" basedir="${build.dir}"/>
33   </target>
34 </project>

Now for a blow-by-blow description of the build file:

  • Line 2 is where I set the name of the project. Notice that I've set the default target to "run".

  • In line 3, I set a property called "basename" to the name of my library.

  • The clean target on line 6 includes a built-in task called "delete", which removes the output folder.

  • Line 15 is where the fun starts. This is the build target, and it illustrates how a target can include one or more tasks. The build target has a task (mkdir) on line 16, which creates my target folder. Line 17 is the C# compiler task that compiles to exe. The target, csc, contains a collection of source files and references. You will note that I used the property "build.dir", which I defined at the top of my file. Surround the property with ${ ...} to use its value.

  • Finally, line 31 is my run target, and shows me calling the exec task run my application. You're unlikely to want to do this in the real world; a more likely next step after compile is to run your NUnit test suite. You can use the exec task to run any Windows executable.

To run the build, I simply execute Nant from the command prompt, as shown in Figure 5. As Nant runs, it displays each target as it is being called (clean, build, and run). Within the target, each task is displayed in [] brackets, including any error or informational messages.

Figure 5Figure 5 Building the Win Forms Music Library sample.

  • + Share This
  • 🔖 Save To Your Account