构建没有依赖项的 MSBuild 目标
有什么方法可以告诉 MSBuild 4.0 构建目标,但忽略任何依赖项?我只想建立这个目标本身,没有别的。
Is there any way I can tell MSBuild 4.0 to build a target, but ignore any dependencies? I just want to build that target itself, nothing else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想重申 @EMP 的解决方案(由于我的名声微薄,我不能投票给他)。
避免 MSBuild 重建项目文件中列出的所有依赖项的默认行为的正确方法是将
BuildProjectReferences
属性设置为 false。在他的回答中,他从 MSBuild 脚本中调用 MSBuild;这是命令行的示例:
I would like to reiterate @EMP's solution (and I can't vote him up due to my puny reputation).
The correct way to avoid MSBuild's default behavior of rebuilding all the dependencies listed in the project file is to set the
BuildProjectReferences
property to false.In his answer he invokes MSBuild from within an MSBuild script; here's an example from the command line:
事实证明,内置的
Build
目标检查名为BuildProjectReferences
的属性来判断是否构建引用。我确实需要在项目本身上运行 Build(否则它不起作用),而不是在其依赖项上运行。我最终打电话:It turns out the built-in
Build
target checks a property namedBuildProjectReferences
to tell whether to build references or not. I do need to run Build on the project itself (otherwise it doesn't work), just not on its dependencies. I ended up calling:可以覆盖内置目标,添加您自己的条件,并让您的目标复制原始目标,但这可能会相当复杂。如果您可以追踪单个依赖的内置目标,那么这就是可维护的。有时,这些“核心”目标仅由 DependsOnTargets 列表组成,有时甚至在属性中定义这些目标,因此覆盖它并在缺少它们的情况下添加条件是微不足道的。有时你需要进行大量的剪切和粘贴才能得到正确的结果。
基本上,MSBuild 将仅维护任何给定名称定义的最后一个目标,因此找到您的项目导入包含要覆盖的目标的 .target 文件的位置,然后将您自己的包含覆盖的 .target 文件导入放在其后面。
例如,从 Microsoft.Common.targets 添加条件到“CoreBuild”目标就这么简单,同时保持相同的行为(下面带有 $(SkipCoreBuild) 属性的目标条件)
:目标文件并在标准之后将其导入到 C# 项目中...
...将覆盖默认实现,该实现不具备选择性禁用它所需的条件。
无论如何,如果您发现一个目标不允许您修改条件,我会在 Connect 上提交一个错误,描述您正在尝试执行的操作,MSBuild 人员可以对此类事情非常敏感(随着时间的推移) )。
It may be possible to override the built-in target, add your own condition, and have your target duplicate the original, but this can get rather involved. If you can track down a single dependent built-in target this can be maintainable. Sometimes these "core" targets consist of nothing more than a list of DependsOnTargets, and sometimes even those are defined in a property, so overrideing it and adding a condition where they are missing is trivial. Sometimes though you need to do a big cut-and-paste to get it right.
Basically, MSBuild will only maintain the last target defined of any given name, so find where your project is importing the .target file that includes the target you want to override, then put your own .target file import that contains the override after it.
For example, adding a condition to the "CoreBuild" target from Microsoft.Common.targets is as easy as this, while keeping the same behavior otherwise (the Target condition with the $(SkipCoreBuild) property below):
Placing the above in your own targets file and importing it in a C# project after the standard...
... will override the default implementation that doesn't have the condition you need to selectively disable it.
In any case, if you find a target that doesn't allow you to modify the condition, I would file a bug on Connect describing what you are trying to do, the MSBuild folks can be pretty responsive to this sort of thing (over time).
这取决于您想要构建的目标。
如果目标是内置的并且具有依赖项(例如“构建”),则您无法删除依赖项。
如果它是您自己的目标,您可以轻松实现它:
对于Azure云计算项目中的CorePublish以及任何其他具有可自定义的DependsOn目标,您可以尝试更改项目来修改默认依赖列表:
It depends on the target you want to build.
If the target is builtin and has dependencies (like "Build") you cann't remove dependencies.
If it's your own target you can easily implement it:
For CorePublish in Azure cloud computing project as well as any other with customizable DependsOn targets you can try to change the project to modify default list of dependencies:
通常目标的依赖项是用属性指定的,例如:
在这种情况下,您可以调用 msbuild as
将该属性设置为空,因此没有依赖项。
Usually a target's dependencies are specified with a property, eg:
In such a case you can invoke msbuild as
which sets that property to nothing, and so no dependencies.