Updating local nuget package on post-build event

tugberk picture tugberk · Feb 23, 2011 · Viewed 15.2k times · Source

I have my local nuget library repository separately both for my personal and work releted class libraries.

I have created some of the nuget packages for the libraries which are no longer in development. I did this only for them because I do not know how to update them automatically as soon as my project builds.

I have figured that all the work is being done by nuget command line with Visual Studio Command Prompt. So I can easily do the work I needed (of course I would know commands perfectly and I do not !)

Basically I want the following tasks to execute on the post-build event of my project.

On project build:

  1. copying project dll into a specific folder (lib folder of the nuget package)
  2. updating nuspec file for new file version (my project is increasing the file version on every build)
  3. creating new nupkg file with new file version

Phil Haack shows some of this feature but it is still a prototype as far as I can tell.

So my requirement is the above. Has anyone else accomplished this?

Answer

jsturtevant picture jsturtevant · Jan 23, 2013

The selected solution looks like it would work but it seems like there is a simpler solution for your requirements.

You can create a nuspec file that will read data from the project's metadata. You only need to do this once with this command:

C:\<Path to project>\nuget spec

This creates 'tokens' in the spec file that will will be replaced by the project's metadata when you create the nuget package. This includes the file version. You will want to replace and because all projects are technically suppose to have them.

More details can be found here: http://docs.nuget.org/docs/creating-packages/Creating-and-Publishing-a-Package#From_a_project

Then...

For .Net Framework (old-school) projects, in you project's Post build events you can do this:

nuget pack "$(ProjectPath)"  
xcopy "$(TargetDir)*.nupkg" "<path where you are hosting your local nuget repo>" /C /Y

(assuming nuget.exe is available on your system PATH).

For .Net Core and Standard projects, nuget can't pack them (see https://github.com/NuGet/Home/issues/4491). Instead, use this as your post-build step:

dotnet pack "$(ProjectPath)" --no-build --include-source --include-symbols --output "<path where you are hosting your local nuget repo>"

Of course you can adjust the options to meet your needs. See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-pack?tabs=netcore2x for dotnet pack command options.