从 Visual Studio 编译时,您使用的是 devenv 而不是 msbuild。很高兴看到 devenv 如何调用 msbuild(但作为 VS 一个非开源工具,我们不能)。所以,我认为不可能做到这一点。也许还有另一种方法可以做你想做的事情。
MSbuild v3.5 不像 MSbuild 4.0 那样支持动态任务创建,但您可以创建自定义任务并导入它们。
首先,创建一个简单的类库(我称之为 DownloadNuget2008.dll),其中包含下载 nuget.exe 的任务(取自 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;
}
}
}
}
我曾经使用下面的 Exec 任务在 Visual Studio 2008 上恢复我的 NuGet 包(编辑你的 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>
然后你会在输出中看到:
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".
我了解您希望对 VS2012 和 VS2008 使用相同的 .targets 文件,但是(如您所说)MSBuild 3.5 和 4.0 之间存在许多差异,因此更容易使用特定方法。