当VS解决方案中的目标失败时,如何使msbuild失败? [英] How to make msbuild fail when a target fails in a VS solution?

查看:182
本文介绍了当VS解决方案中的目标失败时,如何使msbuild失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在命令行上使用msbuild来构建包含C ++项目的VS2012解决方案.该项目的目标在构建后运行:

I'm using msbuild on the command line to build a VS2012 solution containing a C++ project. The project has a target that runs after the build:

<Target Name="RunTargetAfterBuild" AfterTargets="Build">
  <Error Text="I am a failing target" />
</Target>

我希望msbuild在构建时返回错误,但是在构建过程中的某处,该错误会丢失并且msbuild报告构建成功".因此,ERRORLEVEL仍设置为0,因此很难检测在自动构建过程中是否出了问题.如何使msbuild将此错误一直传播到顶级项目/解决方案?我知道这是可能的,因为这是发生在编译器错误之类的情况下.

I want msbuild to return an error when building, however somewhere in the process of building, the error gets lost and msbuild reports 'Build succeeded'. Consequently the ERRORLEVEL is still set to 0 so it's pretty hard to detect if something went wrong during automated builds. How do I make msbuild propagate this error all the way to the top level project/solution? I know this is possible since it's what happens for compiler errors and the likes.

以下是输出的相关部分:

Here are the relevant parts of the output:

> msbuild test.sln

...

...: error : I am a failing target  [...test.vcxproj]
Done Building Project "...test.vcxproj" (default targets) -- FAILED.

Done Building Project "...test.vcxproj.metaproj" (default targets).

Done Building Project "...test.sln" (Build target(s)).

Build succeeded.    --> this is NOT what I want

....

0 Warning(s)
1 Error(s)

当出现编译器错误时,输出为:

While for compiler errors the output is this:

> msbuild test.sln

....

...: error C3861: 'HECK': identifier not found [...test.vcxproj]
Done Building Project "...test.vcxproj" (default targets) -- FAILED.

Done Building Project "...test.vcxproj.metaproj" (default targets) -- FAILED.

Done Building Project "....test.sln" (Build target(s)) -- FAILED.


Build FAILED.     --> this is what I want

0 Warning(s)
1 Error(s)

解决方案

起作用的是命名目标AfterBuild,因为这是msbuild的已知目标.但是,这需要在导入Microsoft.Cpp.targets之后定义目标,这在某种程度上容易出错,并且使得定义多个目标以在构建后运行更加困难.在研究这一点时,我发现使用AfterTargets确实可以按预期工作,而不是使用Build目标,但可以使用任何其他目标.不知道为什么,但是现在我正在使用这种解决方案:

as Allen answered, what does work is naming the target AfterBuild since that is a known target to msbuild. However that requires the target is defined after importing Microsoft.Cpp.targets which is somewhat prone to errors, and makes it harder to define multiple targets to run after the build. While researching this I found that using AfterTargets does work as expected when not using the Build target but any other target. No idea why, but it does so now I'm using this solution instead:

<Target Name="RunTargetAfterBuild" AfterTargets="FinalizeBuildStatus">
  <Error Text="I am a failing target" />
</Target>

推荐答案

将自定义目标添加到项目的InitialTargets属性.

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

InitialTargets ="RunTargetAfterBuild"

InitialTargets="RunTargetAfterBuild"

您是正确的,这不会解决您的问题.但是使用AfterBuild Target,我可以复制并在.sln上获取msbuild失败

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

这篇关于当VS解决方案中的目标失败时,如何使msbuild失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆