Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 841
Trevor Pilley Avatar asked Nov 08 '12 14:11

Trevor Pilley


People also ask

How do I update my local NuGet package?

Invoke the Package Manager dialog (select Tools > NuGet Package Manager > Manage NuGet Packages for Solution). Go to the Updates tab. Select the packages you want to update (or use the Select all packages to update all packages) and click Update.

How do I add Nuspec to Visual Studio?

You can configure Visual Studio to automatically generate the NuGet package when you build the project. In Solution Explorer, right-click the project and choose Properties. In the Package tab, select Generate NuGet package on build.


1 Answers

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.

like image 103
Mike Zboray Avatar answered Oct 10 '22 20:10

Mike Zboray