如何在Visual Studio 2017中使用BeforeBuild和AfterBuild目标?

时间:2017-05-11 17:23:08

标签: .net msbuild .net-core visual-studio-2017 csproj

升级到csproj后使用Visual Studio 2017和Microsoft.NET.Sdk,我的" BeforeBuild"和" AfterBuild"目标不再运行。我的文件看起来像这样:

[:"guest.user.phone_numbers.number", "is an invalid number"]

3 个答案:

答案 0 :(得分:13)

associated MSBuild git issue建议使用BeforeBuild / AfterBuild作为任务名称,而是相应地命名任务并与目标进行连接

<Project Sdk="Microsoft.NET.Sdk"> 
  <PropertyGroup>
    <TargetFramework>net46</TargetFramework>
  </PropertyGroup>

  <!-- Instead of BeforeBuild target -->
  <Target Name="MyCustomTask" BeforeTargets="CoreBuild" >
      <Message Text="Should run before build" Importance="High" />
  </Target>

  <!-- Replaces AfterBuild target -->
  <Target Name="AnotherCustomTarget" AfterTargets="CoreCompile">
      <Message Text="Should run after build" Importance="High" />
  </Target>    
</Project>

这会让你获得一个惯用的VS 2017项目文件,但是你之前/之后触发的目标仍然是此时的一些争论

答案 1 :(得分:7)

当您指定<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net46</TargetFramework> </PropertyGroup> <!-- my targets that don't run --> <Target Name="BeforeBuild"> <Message Text="Should run before build" Importance="High" /> </Target> <Target Name="AfterBuild"> <Message Text="Should run after build" Importance="High" /> </Target> </Project> 时,您正在使用&#34;隐式顶部和底部导入&#34;。这意味着在csproj文件的底部有一个不可见的导入到Microsoft.NET.Sdk / Sdk.targets,它覆盖了&#34; BeforeBuild&#34;和&#34; AfterBuild&#34;目标

您可以使用显式导入来解决此问题,以便控制导入顺序。

Project Sdk="Microsoft.NET.Sdk"

答案 2 :(得分:0)

在已经mentioned GitHub issue Rainer Sigwald中提供了更短而优雅的解决方案:

<Target Name="CustomBeforeBuild" BeforeTargets="BeforeBuild"> ... </Target>
<Target Name="CustomAfterBuild" AfterTargets="AfterBuild"> ... </Target>

看起来很奇怪,但是效果很好。

相关问题