How to make msbuild fail when a target fails in a VS solution?

吃可爱长大的小学妹 提交于 2019-12-01 17:02:49
allen

Add your custom target to the InitialTargets attribute of your project.

InitialTargets="RunTargetAfterBuild"

You are correct that this will not solve your issue. But using the AfterBuild Target I was able to repro and get msbuild on the .sln to fail

You can extend the BuildDependsOn property to include your target as part of what constitutes a successful build:

<PropertyGroup>
  <BuildDependsOn>
      $(BuildDependsOn);
      RunTargetAfterBuild
  </BuildDependsOn>
</PropertyGroup>

More details can be found here: http://msdn.microsoft.com/en-us/library/ms366724.aspx

I believe the Error item should only fail if a condition is met, not every time (which is presumably what you want?)

For instance

<Error Text="Setup Kit Failed to copy!" Condition="'@(FilesList)' == ''" /> 

If the condition is met (in this case a previous copy task copied no files) only then will the Error be raised, which then fails the build

A better implementation of your AfterBuild solution would be to use BeforeTargets, like this:

<Target Name="RunTargetAfterBuild" BeforeTargets="AfterBuild">
  <Error Text="I am a failing target" />
</Target>

This supports multiple such targets, and doesn't depend on inclusion order vs. Microsoft.Cpp.targets.

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