1

프로젝트를 컴파일 할 때 Visual Studio 2008에서 사용하는 msbuild 버전을 변경할 수 있습니까?Visual Studio 2008 재정의 MSBuild 버전

msbuild 4.0을 사용하도록 설정하고 싶습니다.

이 이유는 VS2012 프로젝트에서 사용하는 동일한 .targets 파일을 가져와서 nuget 패키지를 복원 할 수 있기 때문입니다. 프로젝트는 스마트 장치 프로젝트이기 때문에 VS10 +로 업그레이드 할 수 없습니다.

원본 대상 파일을 수동으로 편집하려고 시도했지만 너무 많은 기능이 msbuild 3.5에 없으므로 해결하지 못했습니다.

업데이트 :

원래 .targets 파일도 MSBuild를 3.5에서 지원되지 않는 코드 작업을 사용하여 nuget.exe 파일의 자동 다운로드 기능을 사용하고, 그래서 이것은해야 뭔가 고려되었다.

답변

1

Visual Studio에서 컴파일 할 때 msbuild 대신 devenv를 사용하고 있습니다. devenv가 msbuild를 호출하는 방법을 확인하는 것은 좋을 것입니다 (그러나 VS가 아닌 오픈 소스 도구 인 경우, 우리는 할 수 없습니다). 그래서, 나는 그것을 할 수 있다고 생각하지 않습니다. 어쩌면 당신이하려는 일을하는 또 다른 접근법이있을 수 있습니다.

MSbuild v3.5는 MSbuild 4.0과 같이 동적 작업 생성을 지원하지 않지만 create customized tasksimport them 일 수 있습니다.

먼저, 간단한 클래스 라이브러리를 만들 (nuget.targets에서 촬영) nuget.exe 다운로드 작업을 포함하는 (나는 DownloadNuget2008.dll라고 불렀다) :

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; 
      } 
     } 
    } 
} 

나는 내 NuGet 패키지를 복원하는 데 사용을 아래 Exec에서 작업에 비주얼 스튜디오 2008 (당신의 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에는 많은 차이점이 있으므로 구체적인 접근 방법이 더 쉽습니다.

+0

.targets 파일에서 nuget.exe를 자동으로 다운로드한다는 사실이 아니라면 작동합니다. 그것에 대해 어떤 제안이 있습니까? 이 질문을 적절히 반영하도록 질문을 수정하겠습니다. – julealgon

+0

나는 그것에 대해 생각하고 나의 대답을 업데이트 할 것이다. –

+0

이제 충분히 적합하다고 생각합니다. 나는 커스텀 태스크를 만드는 방법, 굉장한 것을 보지 못했다. 이 작업을 위해 새 프로젝트를 만들어야하는 것은 다소 번거롭지만 실제로 MSBuild 3.5를 사용하는 다른 방법은 없다고 생각됩니다. 또한 toolsversion이나 .targets 파일의 일부를 기반으로 일부 스위치를 만들어 두 스위치를 하나로 병합 할 수 있습니까? 나는 이것이 우리 상황에서 이상적이라고 생각한다. 나는 며칠 동안 그것을 테스트 할 수 없지만 답변으로 귀하의 게시물을 표시합니다. 도움을 많이 주셔서 감사합니다. – julealgon