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="http://schemas.microsoft.com/developer/msbuild/2003">

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

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

Now, in your NANT's Default.build 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"/>
    </if>
    <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}">
      <sources>
        <include name="**/*.cs" />
      </sources>
      <references>
        <include name="${log4net.dir}\log4net.dll"/>
      </references>
    </csc>

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

  </target>

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