MSBuild 中的 PreBuildEvent、BeforeBuild 目标和 BeforeCompile 目标之间有什么区别?
我最近不得不从 将 Visual Studio 中的 PreBuildEvent 放入 BeforeBuild 目标中,使其在 AppHarbor 上运行。在这样做的同时,我还注意到了一个 BeforeCompile 目标。
这三个看似相似的事件:PreBuildEvent、BeforeBuild Target、BeforeCompileTarget 有什么区别?
每种方法可以做什么/不可以做什么?为什么你会选择其中一种而不是另一种?
I recently had to move some code from a PreBuildEvent in Visual Studio into the BeforeBuild target to make it work on AppHarbor. While doing so, I also noticed a BeforeCompile target.
What is the difference between these three seemingly similar events: PreBuildEvent, BeforeBuild Target, BeforeCompileTarget?
What can/can't be done with each, and why would you pick one over another?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题的答案可以在
Microsoft.Common.targets
文件中找到(取决于您使用的是 64 位还是 32 位框架):C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.target
对于 64 位和C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets
对于 32 位运行时。该文件定义了项目构建所经历的所有步骤。引用源代码:代码足够好,可以在两个目标的注释中解释
BeforeBuild
和AfterBuild
目标的使用。接下来是
CoreBuild
目标的定义:因此
Build
目标只是CoreBuild
目标的包装器,使您能够执行自定义CoreBuild
目标之前或之后的步骤。如上所示,PreBuildEvent
和PostBuildEvent
被列为CoreBuild
目标的依赖项。Compile
目标的依赖项定义如下:代码中再次注释了
BeforeCompile
和AfterCompile
:鉴于此信息,我不知道为什么AppHarbor 不支持
Pre-, PostBuildEvent
,而Build
可以使用Before-, AfterBuild
进行修改。选择要覆盖哪个场景的
目标
取决于构建期间您希望执行给定任务的时刻。这些目标对其能够实现的目标没有具体的限制和/或好处。除此之外,他们还可以调整由前面的步骤定义/填充的ItemGroup
或属性。在构建尝试解决项目依赖关系之前,最好使用 nuget 引入包。因此,BeforeCompile 不适合执行此类操作。
我希望这能让我们对此事有所了解。在 MSDN 上找到了另一个很好的解释
The answer to this question can be found in the
Microsoft.Common.targets
file which can be found (depending on wether you're using the 64-bit or 32-bit framework) at:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.target
for 64-bit andC:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets
for the 32-bit runtime. This file defines all the steps a build of your project undergoes. Quoting the source:The code is nice enough to explain the use of the
BeforeBuild
andAfterBuild
target in the comments for both targets.This is followed by the definition of the
CoreBuild
target:So the
Build
target is just a wrapper around theCoreBuild
target to enable you to perform custom steps just before or after theCoreBuild
target. As can be seen above thePreBuildEvent
andPostBuildEvent
are listed as dependencies of theCoreBuild
target. The dependencies of theCompile
target are defined as follows:Again
BeforeCompile
andAfterCompile
are commented in the code:Given this information I do not know why AppHarbor does not support
Pre-, PostBuildEvent
while theBuild
can be modified usingBefore-, AfterBuild
.Choosing which
Target
to override for which scenario depends on the moment during the build at which you wish to perform your given task. The targets do not have specific restrictions and/or benefits as to what they can accomplish. Apart from the fact that they can adaptItemGroup
's or properties that were defined/filled by previous steps.Using nuget to bring in packages is probably best performed before the build tries to resolve the projects dependencies. So
BeforeCompile
is not a good candidate for this kind of action.I hope this sheds some light on the matter. Found another nice explanation on MSDN