Thursday, May 23, 2013

Using NANT with Visual Studio 2012

I have used Apache's Ant for quite a while. Now in there is NANT in the C# and .NET world. However Visual Studio only uses MSBUILD. Here's how you make it so that Visual Studio will use NANT instead:

First, open up the .csproj file and modify the DefaultTargets to "Nant" as follows:
<Project ToolsVersion="4.0" DefaultTargets="Nant" xmlns="">

Next, add the following at the bottom of the file:

<Target Name="Nant">
  <Exec Command="CALL nant.exe -D:buildtype=$(Configuration)" />

Now, in your NANT's file, add this as the build target:

  <target name="build" description="Compiles the source code">

    <echo message="Build Type: ${buildtype}"/>
    <property name="debug" value="false"/>
    <if test="${buildtype == 'Debug'}">
      <property name="debug" value="true"/>
    <echo message="Debug = ${debug}"/>

    <!-- Make sure the build directory is present -->
    <mkdir dir="${build.dir}" unless="${directory::exists('build')}" />

    <!-- Compile sources -->
    <csc target="library" output="${build.dir}\${project::get-name()}.dll" debug="${debug}">
        <include name="**/*.cs" />
        <include name="${log4net.dir}\log4net.dll"/>

    <!-- Copy log4net over. -->
    <copy file="${log4net.dir}\log4net.dll" tofile="${build.dir}\log4net.dll"/>


This will use whatever build configuration used in Visual Studio. It will also set debug mode if you build it in Debug (to generate those .pdb files).