简体   繁体   English

是否可以编写依赖于 NuGet 包的嵌入式 C# 自定义 msbuild 任务?

[英]Is it possible to write an embedded C# custom msbuild task that depends on a NuGet package?

I do not want to package my msbuild task in a dll.我不想将我的 msbuild 任务打包在 dll 中。 Instead, I would like to provide its C# code in a standalone source file and define like this:相反,我想在一个独立的源文件中提供它的 C# 代码并像这样定义:

<UsingTask TaskName="MyTask" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
  <Task>
    <Code Source="$(MSBuildThisFileDirectory)\MyTask.cs" Language="cs" />
  </Task>
</UsingTask>

The reason - I do not want to commit my task as a DLL in the version control, because if I change it devs may not be able to pull the changed DLL if the original one is loaded by some msbuild node.原因 - 我不想在版本控制中将我的任务作为 DLL 提交,因为如果我更改它,如果某个 msbuild 节点加载了原始 DLL,则开发人员可能无法提取更改后的 DLL。 That would cause all kind of friction I prefer to avoid.这会引起我宁愿避免的各种摩擦。

But then the question is - what if my task must depend on a 3rd party DLL?但问题是——如果我的任务必须依赖于第 3 方 DLL 怎么办? I can probably commit it, because I am not going to change it, but then what?我可能会提交它,因为我不会改变它,但那又如何呢?

So my question - is there a way for a custom msbuild task that is distributed as a C# source file to reference a 3rd party DLL, like a NuGet package for example?所以我的问题 - 有没有办法让作为 C# 源文件分发的自定义 msbuild 任务引用第 3 方 DLL,例如 NuGet 包?

The type of task you are creating is called an 'inline task' in the MSBuild documentation.您正在创建的任务类型在 MSBuild 文档中称为“内联任务”。 It makes sense to use inline tasks to remove dependencies in the build script.使用内联任务来删除构建脚本中的依赖项是有意义的。

Unfortunately inline tasks don't support NuGet.不幸的是,内联任务不支持 NuGet。 There is no PackageReference for inline tasks.内联任务没有PackageReference You could have a NuGet step that restores a given package and you could add a Reference in your inline task to a known location for the package but that starts getting messy and creates some of the dependency and chicken/egg issues that I think you are trying to avoid.你可以有一个 NuGet 步骤来恢复给定的包,你可以在你的内联任务中添加一个Reference到包的已知位置,但这开始变得混乱并产生一些我认为你正在尝试的依赖关系和鸡/蛋问题避免。

If you can't avoid the NuGet package dependency and you have a private and/or internal NuGet server, you might consider moving the custom task into its own DLL with its own build and NuGet package.如果您无法避免 NuGet 包依赖关系并且您拥有私有和/或内部 NuGet 服务器,则可以考虑将自定义任务移动到其自己的 DLL 中,并使用其自己的构建和 NuGet 包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM