views:

354

answers:

2

How do I tell NAnt to use the same VB compiler VS2008 uses when it creates .NET 2.0-targeting applications?

I have switched a web application to VS2008 back-targetted to .NET 2.0. I can run NAnt (0.85rc4, 0.85, and 0.86b1) just fine once I do this. When I try to use some of the VB9 syntax, that still compiles just fine back to a .NET 2.0 binary in VS2008, NAnt gets the kind of compile error you would get if you tried to do the new syntax in VS2005 (where it wasn't supported).

In case it helps, here is a simplified version of what I am attempting, just a simple anonymous delegate that works great until I try to use NAnt to build the 2.0 app instead of VS2008.

Public Class SomeObject
    Public Name As String
End Class
Private SomeList As List(Of SomeObject) = SomeObject.GetAllSomeObjects()
Public Function FindFirstItemWithSimilarName(ByVal x As String) As SomeObject
    Return SomeList.Find(Function(p As SomeObject) p.Name.Contains(x))
End Function

EDIT: Unless someone can think of a reason not to, the current setting in my build file is this (since I do indeed want a .NET 2.0 application, just one generated by a more robust VB compiler):

<property name="nant.settings.currentframework" value="net-2.0"/>
A: 

I wonder if you have to change the framework that NAnt is running under.

Also, look into the NAntContrib project. That has the MSBuild task. We use this to build our 2008 projects, where we use the fancy sytax sugar like what you want, down to 2.0 assemblies. One thing we had to do though was replace the MSBuild.exe in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 with one that forwards the request on to the 3.0 MSBuild.exe

Here is the code we use to replace MSBuild.exe in the 2.0 path. I can't remember where I got this code. I tried to find it online but can't.

Build this to MSBuild.exe and replace your 2.0 one with this one.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace MSBuild
{
    public class Program
    {
        public static int Main(string[] args)
        {
            for (int argIndex = 0; argIndex < args.Length; argIndex++)
            {
                if (args[argIndex].Contains(" "))
                {
                    string quotedArg = string.Format("\"{0}\"", args[argIndex]);
                    args[argIndex] = quotedArg;
                }
            }

            string arguments = string.Join(" ", args);

            // HACK: I should probably change
            //       this to automaticlaly 
            //       determine the path

            Process process = Process.Start("C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\MSBuild.exe",arguments);

            process.WaitForExit();

            return process.ExitCode;

        }
    }
}
I can get it to work by changing it to "net-3.5" while using NAnt 0.86-beta1 but that isn't my goal (yet). I need to take baby steps with this project or my coworkers will probably kill me should something go wrong.
patridge
A: 

I was being a little to literal in my expectations of NAnt. Since I was using NAntContrib to run msbuild on the projects, I did want the net-3.5 framework for NAnt. MSBuild and the project file take care of the back-targeting of the project to .NET 2.0. I was able to take my VB anonymous delegate, compile it to 3.5 and drop the generated DLL right onto a machine with only .NET 2.0 and it ran fine.

Simply set the project to compile to .NET 2.0: Project Properties -> Compile [tab] -> Advanced Compile Options... -> Target framework (all configurations): .NET Framework 2.0

...and tell NAnt to blindly assume net-3.5:

<property name="nant.settings.currentframework" value="net-3.5"/>
patridge
It should be noted that this property can only be set to net-3.5 when using NAnt version > .85.
Scott Saad