以编程方式生成自定义 MSBuild .Targets 文件

发布于 2024-10-03 10:23:49 字数 220 浏览 2 评论 0原文

我有一个自定义 MSBuild 任务库,我经常对其进行编辑、重构代码、添加新任务以及删除旧的、失效的任务。编辑 .Targets 文件以与库中的实际内容同步变得很痛苦,所以我想知道,自动化此操作的最佳方法是什么?

我想要一个在库成功构建后运行的解决方案,这样在尝试从其他项目调用自定义任务时就不会出现任何讨厌的错误。

我确实有一些关于如何做到这一点的想法,但我想看看其他人首先想出什么。 :-)

I have a library of custom MSBuild tasks which I edit quite regularly, refactoring code, adding new tasks and removing old, defunct ones. It's become a pain to edit the .Targets file to sync with what is actually in the library, so I was wondering, what would be the best way to automate this?

I would like a solution that runs after the library has built successfully, so that I don't get any nasty errors when trying to call my custom tasks from other projects.

I do have a few ideas about how to do this, but I'd like to see what others come up with first. :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧故 2024-10-10 10:23:49

您可能有一个单独的 .targets 文件,仅包含对您的任务的引用。从一侧将其链接到主 .targets 文件中。从另一个覆盖它在任务解决方案的 AfterBuild 目标中执行另一个“主”自定义任务。这个“主”任务将从新建的程序集中获取实现 ITask 的所有类,并将它们写入 .targets 文件。
但说实话,这对我来说似乎有些过分了。无论如何,您都必须编辑 .targets 文件才能使用新任务。编写这样的“主”任务并支持它可能会花费您更多的时间。

我可能会幻想几个更自动化的解决方案,但对“投入的时间”/“自动化节省的时间”的考虑阻止了我的想象力。 =)

You may have a separate .targets file containing only references to your tasks. From one side link it in your main .targets file. From the other overwrite it executing some another "master" custom task in your AfterBuild target of the tasks solution. This "master" task will get all classes implementing ITask from newly built assembly and write them to .targets file.
But to be honest this looks like an overkill to me. You have to edit your .targets file anyway to get use of your new tasks. Writing such a "master" task and supporting it may take you more time.

I may fantasize on several more automated solutions, but considerations of "time invested"/"time saved due to automation" stop the flight of my imagination. =)

清晨说晚安 2024-10-10 10:23:49

好吧,我找到了一种更好的方法来实现此目的,即使用 MSBuild Community TaskTaskSchema 任务:

<TaskSchema Assemblies="@(MyTaskAssembly)" 
            OutputPath="%(MyTaskAssembly.RootDir)%(MyTaskAssembly.Directory)" 
            CreateTaskList="true"
            IgnoreMsBuildSchema="true"
            Includes="Microsoft.Build.Commontypes.xsd"/>

我只需确保已导入 MSBuild.Community.Tasks.Targets 文件,创建一个名为 MyTaskAssemblyItemGroup,其中包含我的所有任务程序集 (令人震惊),然后将上述任务调用粘贴到我的项目的 AfterBuild 目标中。甜的! :)

Well, I've found a nicer way to achieve this by using MSBuild Community Task's TaskSchema task:

<TaskSchema Assemblies="@(MyTaskAssembly)" 
            OutputPath="%(MyTaskAssembly.RootDir)%(MyTaskAssembly.Directory)" 
            CreateTaskList="true"
            IgnoreMsBuildSchema="true"
            Includes="Microsoft.Build.Commontypes.xsd"/>

I simply ensure that I've imported the MSBuild.Community.Tasks.Targets file, create an ItemGroup named MyTaskAssembly containing all of my task assemblies (shocker), then stick the above task call in the AfterBuild target of my project. Sweet! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文