使用 BeforeTargets=“Build"时目标未运行在构建服务器上 [英] Target not running when using BeforeTargets="Build" on Build Server

查看:12
本文介绍了使用 BeforeTargets=“Build"时目标未运行在构建服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 .targets 文件,我将它导入到我的 C# MVC Web 应用程序的项目文件中.我已经像这样添加了自定义目标:

I have a custom .targets file which I import into my C# MVC web application's project file. I've added custom targets to this like so:

<Target Name="CopyFiles" BeforeTargets="Build"></Target>

这在 Visual Studio 下构建时效果很好,但是当我使用 TeamCity 构建它时,目标永远不会运行,我不知道为什么.

This works fine when building under Visual Studio, but when I use TeamCity to build it, the target never gets run, and I can't work out why.

如果我将目标更改为使用 BeforeTargets="Compile" 然后它会运行.或者,如果我在 .targets 文件中添加一个名为 Build 的附加目标

If I change my target to use BeforeTargets="Compile" then it runs. Alternatively, if I add an additional target with the name Build to the .targets file

<Target Name="Build" />

然后它将运行,但这样做会覆盖现有的构建目标,因此我的应用程序不会构建.我无法完全弄清楚这其中的逻辑——这没有意义.我现在正在使用 Compile 目标,但如果有人能解释为什么在 Build 任务不起作用之前尝试执行它,我将不胜感激.

then it will run, but doing so overrides the existing Build target and thus my application doesn't build. I can't quite make out the logic to this - it doesn't make sense. I'm using the Compile target for now, but if someone could explain why trying to execute it before the Build task doesn't work I'd really appreciate it.

推荐答案

'Build' 是一个特殊的内置目标,因此与大多数其他目标的工作方式不同.绝对不能安全地覆盖它.

'Build' is a special built-in target, so doesn't really work the same way as most other targets. It definitely can't be safely overridden.

最相关的文档在这里:https://msdn.microsoft.com/en-us/库/ms366724.aspx

如果您希望在构建之前运行某些东西,标准方法(如新创建的 .csproj 文件中的注释所推荐)是覆盖 BeforeBuild 目标(如上文所述).

If you want something to run before build, the standard approach (as recommend by the comments in a newly-created .csproj file) is to override the BeforeBuild target (as documented above).

但是,这并不是最可靠的解决方案.如上述文档所述:

However, this isn't the most robust solution. As noted in the documentation above:

覆盖预定义的目标是扩展构建过程的一种简单方法,但是,由于 MSBuild 会按顺序评估目标的定义,因此无法阻止导入您的项目的另一个项目覆盖您已经覆盖的目标.

Overriding predefined targets is an easy way to extend the build process, but, because MSBuild evaluates the definition of targets sequentially, there is no way to prevent another project that imports your project from overriding the targets you already have overridden.

重写 BuildDependsOn 属性并扩展此属性的默认值以包含您要运行的目标会更好(并且稍微复杂一点).

It's better (and only slightly more complex), to override the BuildDependsOn property and extend the default value of this property to include the target you want to run (this is also documented in the link above).

另一种方法是将 BeforeBuild 留空并使用 BeforeTargets="BeforeBuild",这感觉有点奇怪,但非常简单,即使 BeforeBuild 目标被覆盖,它仍然可以工作.

Another approach would be to leave BeforeBuild empty and use BeforeTargets="BeforeBuild", which feels a bit odd but is quite simple and will still work even if the BeforeBuild target gets overridden.

至于为什么 BeforeTargets="Build" 不起作用,我在文档中找不到对此的参考,但我认为这与它的特殊性质有关.它与普通目标不同,最好不要将其视为目标.

As to why BeforeTargets="Build" doesn't work, I can't find a reference for this in the documentation, but I think it's to do with its special nature. It doesn't work the same as ordinary targets and it's probably better not to think of it as a target at all.

这篇关于使用 BeforeTargets=“Build"时目标未运行在构建服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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