配置 NuGet 包以在安装时添加构建事件 [英] Configure NuGet package to add a build event when installed
问题描述
有谁知道是否可以向 nuspec 文件中添加一些内容,以便在通过 NuGet 安装包时将生成前或生成后事件发送到项目?
Does anyone know if it's possible to add something to a nuspec file so that when a package is installed via NuGet a pre or post build event to a project?
推荐答案
我认为编辑用户的 PostBuildEvent 属性是将构建后操作添加到项目的错误方法.我相信推荐的方法是将您的自定义操作放入导入到项目文件中的 MSBuild 目标中.从 NuGet 2.5 开始,如果您在包中包含build"文件夹(同时级别作为内容和工具)并且它包含一个 {packageid}.targets 文件或 {packageid}.props 文件,NuGet 会在您安装包时自动将导入添加到项目文件中.
I think editing the user's PostBuildEvent property is the wrong way to go about adding a post-build action to a project. I believe the recommended way is to put your custom action into an MSBuild Target that is imported into the project file. As of NuGet 2.5, if you include a 'build' folder in your package (at the same level as content and tools) and it contains a {packageid}.targets file or {packageid}.props file, NuGet will automatically add an Import to the project file when you install the package.
例如,您有一个名为 MyNuGet 的包.您创建一个文件 build\MyNuGet.targets ,其中包含:
For example you have a package called MyNuGet. You create a file build\MyNuGet.targets containing:
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="MyNuGetCustomTarget" AfterTargets="Build">
<Message Importance="high" Text="Message from MyNuGetCustomTarget. Configuration: $(Configuration)" />
</Target>
</Project>
这会创建一个自定义目标,该目标配置为在标准构建目标之后运行.NuGet 将处理在安装时插入导入并在卸载时删除它.
This creates a custom target that is configured to run after the standard Build target. NuGet will handle inserting the Import on install and removing it on uninstall.
这篇关于配置 NuGet 包以在安装时添加构建事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!