nuget-packagebuild-events

Configure NuGet package to add a build event when installed


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?


Solution

  • 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.