Configure NuGet package to add a build event when installed

倾然丶 夕夏残阳落幕 提交于 2019-12-17 19:33:23

问题


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?


回答1:


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.

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>

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.




回答2:


This is a slightly cleaner way to set a build event from an install.ps1 powershell script in the NuGet Package:

$project.Properties.Item("PostBuildEvent").Value = "your build event here"



回答3:


Not to the .nuspec file itself, but you can to install.ps1 which you can add to your package using nuspec. Here is what I'm doing (I don't know if it's the best way to do it but it does work):

param($installPath, $toolsPath, $package, $project)

$project.Properties | where { $_.Name -eq "PreBuildEvent" } | foreach { $_.Value = "copy `"`n`$(ProjectDir)Web.`$(ConfigurationName).config`" `"`$(ProjectDir)Web.config`"" }


来源:https://stackoverflow.com/questions/13290860/configure-nuget-package-to-add-a-build-event-when-installed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!