从msbuild中的命令行导入.targets文件 [英] Import .targets file from command line in msbuild

查看:562
本文介绍了从msbuild中的命令行导入.targets文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有多个项目正在使用msbuild。我有一个小的自定义到由.targets文件处理的构建。一个解决方案是添加代码段

 < Import Project =MyTargets.targets/> 

到每个项目文件。但是,理想情况下,我不想触摸项目文件,并能够将此信息作为参数传递给msbuild。这样,我可以轻松地控制是否从命令行运行此自定义我不必触摸现有的项目文件。



解决方案

您可以使用MSBuild 4.0轻松做到这一点(通过顶级属性 ToolsVersion =4.0):



有多个属性可用于在加载Common.targets和/或CSharp.targets之前和之后导入目标。



最简单的方法是使用2组自解释属性。
第一组是:
$(CustomBeforeMicrosoftCommonTargets)
$(CustomAfterMicrosoftCommonTargets)



和第二个:

  $(CustomBeforeMicrosoftCSharpTargets)
$(CustomAfterMicrosoftCSharpTargets)

属性名称是非常自我解释的。



只需通过msbuild.exe传递完整的文件名到任何这个属性

  msbuild.exe /p:CustomBeforeMicrosoftCSharpTargets=c:\mytargets\custom.targets 



您可以使用其他ImportByWildcard(Before | After)....多个文件。但在这种情况下,你需要传递更多的参数到命令行。


I currently have multiple projects being build using msbuild. I have a small customisation to the build that is handled by a .targets file. One solution is to add the snippet

<Import Project="MyTargets.targets"/>

to each project file. However, ideally I would like to not touch the project files, and be able to pass this information as a parameter to msbuild. That way I could easily control whether I run this customisation from the command line, and I don't have to touch the existing project files.

Is this possible?

解决方案

You can do that easily with MSBuild 4.0 (check your version by top-level attribute ToolsVersion="4.0"):

There are multiple properties you can use to import your targets before and after Common.targets and or CSharp.targets loaded.

Simplest way is to use 2 sets of self explaining properties. First set is: $(CustomBeforeMicrosoftCommonTargets) $(CustomAfterMicrosoftCommonTargets)

and second one:

$(CustomBeforeMicrosoftCSharpTargets)
$(CustomAfterMicrosoftCSharpTargets)

Property names are pretty self-explained.

Just pass full file name to any of this properties via msbuild.exe e.g.

msbuild.exe /p:CustomBeforeMicrosoftCSharpTargets=c:\mytargets\custom.targets

You can use other "ImportByWildcard(Before|After)...." properties if you need to import multiple files. But in that case you need to pass more parameters to command-line.

这篇关于从msbuild中的命令行导入.targets文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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