Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating local nuget package on post-build event

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?

like image 276
tugberk Avatar asked Feb 23 '11 14:02

tugberk


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.

Should you update NuGet packages?

Well, you should update whenever you are able to cope with it. So you need to think carefully about the regression updating the packages could cause to your application if already in production, or the extra tests you are going to need to carry on in order to verify everything seems to be working as expected.

Does MSBuild restore NuGet packages?

Restore by using MSBuildYou can use msbuild -t:restore to restore packages in NuGet 4. x+ and MSBuild 15.1+, which are included with Visual Studio 2017 and higher. This command restores packages in projects that use PackageReference for package references.

Where do NuGet packages get stored locally?

The global-packages folder is where NuGet installs any downloaded package. Each package is fully expanded into a subfolder that matches the package identifier and version number. Projects using the PackageReference format always use packages directly from this folder.


2 Answers

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.

like image 51
jsturtevant Avatar answered Sep 20 '22 21:09

jsturtevant


Just in case someone else (like me) come across this ancient question - in the current era (VS2017, SDK/NugetReference format/ .NET Core / .NET Standard / Multi-target projects), where creation of NuGet package is an option in the Project Properties - so, only issue of local repository has to be solved:

If you have single-target project, to copy your .nupkg file to local NuGet repository, add Post-build event (Project properties > Build Events > Post-build event command line):

xcopy $(TargetDir)*.nupkg [path to your local nuget repository] /s

Like:

xcopy $(TargetDir)*.nupkg G:\imbVelesOpenSource\LocalNuGet\imbVelesSecondGeneration\ /s


If you have multi-target project, to copy your .nupkg file to local NuGet repository: add Post-build event (Project properties > Build Events > Post-build event command line):

xcopy $(TargetDir)..*.nupkg [path to your local nuget repository] /s

Like:

xcopy $(TargetDir)..*.nupkg G:\imbVelesOpenSource\LocalNuGet\imbVelesSecondGeneration\ /s


Update: Forget post-build events, there is a cleaner way (xcopy approach works strange for multi-target projects), just add this in project XML:

  <Target Name="CopyPackage" AfterTargets="Pack">
    <Copy SourceFiles="$(OutputPath)$(PackageId).$(PackageVersion).nupkg" DestinationFolder="G:\imbVelesOpenSource\LocalNuGet\imbVelesSecondGeneration\" />
  </Target>

Update

For newer NuGet versions, which cut off the last 0 (patch from a 0.0.0.0 version notation), you will need to regex the PackageVersion:

  <Target Name="CopyPackage" AfterTargets="Pack">
    <Copy SourceFiles="$(OutputPath)$(PackageId).$([System.Text.RegularExpressions.Regex]::Replace(&quot;$(PackageVersion)&quot;, &quot;^(.+?)(\.0+)$&quot;, &quot;$1&quot;)).nupkg" DestinationFolder="G:\imbVelesOpenSource\LocalNuGet\imbVelesSecondGeneration\" />
  </Target>
like image 37
hardyVeles Avatar answered Sep 19 '22 21:09

hardyVeles