Question

Is it possible to change the msbuild version that is used by Visual Studio 2008 when compiling projects?

I would like to set it to use msbuild 4.0.

The reason behind this is to be able to import the same .targets file used by our VS2012 projects for nuget packages restore. The projects cannot be upgraded to VS10+ because they are Smart Device projects.

I tried manually editing the original targets file but too many features are missing in msbuild 3.5 and I couldn't work around them.

Update:

The original .targets file is also using the automatic download feature for the nuget.exe file, using a code task that is unsupported in the MSBuild 3.5, so this is something that should be taken into consideration.

Was it helpful?

Solution

When you compile from Visual Studio you're using devenv instead of msbuild. It would be great to see how devenv calls msbuild (but being VS a non open-source tool, we just can't). So, I don't think it is possible to do that. Maybe there's another approach to do what are you're trying to do.

MSbuild v3.5 does not support dynamic task creation as MSbuild 4.0, but you can create customized tasks and import them.

First, create a simple class library (I called it DownloadNuget2008.dll) containing the task to download nuget.exe (taken from nuget.targets):

using System;
using System.IO;
using System.Net;
using Microsoft.Build.Utilities;

namespace DownloadNuget2008
{
    public class DownloadNuget2008Task : Task
    {
        public string OutputFilename { get; set; }

        public override bool Execute()
        {
            try
            {
                OutputFilename = Path.GetFullPath(OutputFilename);

                Log.LogMessage("Downloading latest version of NuGet.exe...");
                var webClient = new WebClient();
                webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);

                return true;
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                return false;
            }
        }
    }
}

I used to restore my NuGet packages on Visual Studio 2008 with the Exec Task below (edit your csproj/vbproj):

  <UsingTask AssemblyFile="$(SolutionDir)DownloadNuget2008.dll" TaskName="DownloadNuget2008Task" />
  <!-- Download NuGet.exe if it does not already exist -->
  <PropertyGroup>
    <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(SolutionDir)nuget.exe</NuGetExePath>
    <DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">true</DownloadNuGetExe>
  </PropertyGroup>
  <Target Name="_DownloadNuGet">
    <Message Text="Downloading nuget..." />
    <DownloadNuget2008Task OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
    <Message Text="Downloading nuget - done." />
  </Target>
  <!-- NuGet Packages Installation (Begin)  -->
  <Target Name="Install-Packages">
    <Exec Command="$(SolutionDir)nuget install $(ProjectDir)packages.config -o $(SolutionDir)packages" />
  </Target>
  <!-- NuGet Packages Installation (End)  -->
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Target Name="BeforeBuild">
    <CallTarget Targets="_DownloadNuGet" />
    <CallTarget Targets="Install-Packages" />
  </Target>

Then you will see on the output:

Target BeforeBuild:
  Task "CallTarget"
    Target "_CheckForInvalidConfigurationAndPlatform" skipped. Previously built successfully.
    Target _DownloadNuGet:
      Task "Message"
        Downloading nuget...
      Done executing task "Message".
      Using "DownloadNuget2008Task" task from assembly "C:\marcos\Testes\NuGet2008\ConsoleApplication1\DownloadNuget2008.dll".
      Task "DownloadNuget2008Task"
        Downloading latest version of NuGet.exe...
      Done executing task "DownloadNuget2008Task".
      Task "Message"
        Downloading nuget - done.
      Done executing task "Message".
  Done executing task "CallTarget".
  Task "CallTarget"
    Target "_CheckForInvalidConfigurationAndPlatform" skipped. Previously built successfully.
    Target Install-Packages:
      Task "Exec"
        Command:
        C:\marcos\Testes\NuGet2008\ConsoleApplication1\nuget install C:\marcos\Testes\NuGet2008\ConsoleApplication1\ConsoleApplication1\packages.config -o C:\marcos\Testes\NuGet2008\ConsoleApplication1\packages
        Successfully installed 'elmah 1.2.2'.
      Done executing task "Exec".
  Done executing task "CallTarget".

I understand you wish to use the same .targets file to both VS2012 and VS2008, but (as you said) there are many differences between MSBuild 3.5 and 4.0 so a specific approach is easier to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top