MSBuild task to Build other solution projects fails in VS but works with MSBuild.exe command line

痞子三分冷 提交于 2020-01-03 03:43:06

问题


I have a custom MSBuild task which among other things adds embedded resources to other projects in the solution. After adding the resources I'd like to then build those projects, but found I can't get this working within Visual Studio.

To test, I stripped out the custom task entirely and redefined a simple AfterBuild target in the web project of a Silverlight solution. The target uses the MSBuild task to build the Silverlight application project in the solution, and looks like this:

<Target Name="AfterBuild">
  <PropertyGroup>
    <LinkedProject>..\SilverlightApplication1\SilverlightApplication1.csproj</LinkedProject> 
  </PropertyGroup>

  <MSBuild Condition="'$(LinkedProject)' != '' "
    Projects="$(LinkedProject)"
    Targets="Build"
    Properties="CustomFlag=true" >
  </MSBuild>
</Target>

The odd thing is that this works perfectly when using MSBuild from the command line, yet does not work in Visual Studio when building the web project. I thought this might be some sort of Silverlight problem, and had the task build a .NET class library project instead, but the result was the same - it worked from the command line but not within VS. In VS there's no actual error - it's just that the Csc task does not compile the assembly and generates no output.

What do I need to do to get this working within Visual Studio?


回答1:


Pass the 'UseHostCompilerIfAvailable=false' property to the MSBuild task.

It looks like Visual Studio breaks badly if csc is invoked from a MSBuild task as it reuses the initial project build settings for its in-process host compiler. In my case, I was building the same project twice - default build was using target framework v3.5, with a AfterBuild MSBuild task specifying v4.0. I ended up with the same issue - csc appeared to run but produced no output. I think what was happening was that with the UseHostCompilerIfAvailable property set to true, csc was calling the hosted compiler which reused my initial project settings, so even though the command line showed csc "building" my v4.0 assembly, the host compiler was simply overwriting the v3.5 one I had just built!




回答2:


Change Visual Studio verbosity to detailed and check build log. I think that CoreBuild is not executed if your files have not changed, so you could try to use AfterCompile instead of AfterBuild.



来源:https://stackoverflow.com/questions/8070593/msbuild-task-to-build-other-solution-projects-fails-in-vs-but-works-with-msbuild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!