Cheatsheet for managing local NuGet packages.


I have finally decided to set up a local NuGet repository for my utilities and libraries. I decided to start small and just create a local repository on my home network.

Below are some quick references for setting up a repository and creating packages.

Set up NuGet for use:

  1. download nuget.exe to a folder in %path%
  2. Create a directory that will be NuGet repository – where all your NuGet packages will be stored (e.g. C:\Programming\NuGet\NuGetRepository)
  3. Within Visual Studio, go to Tools ->Nuget Package Manager Settings -> Package Sources and add the NuGet repository as a source
    • Press +
    • Edit the Name: and Source: fields
    • Press Update

Create a NuGet package:

  1. In Visual Studio, open the solution, right-click the project and select Properties:
    • Set the Assembly name: field the single-word, camel-cased named of the assembly (e.g. ExceptionsLibrary)
    • Select Assembly Information…
      • Set Title: to the text to appear when searching NuGet for the package (e.g. Exceptions Library)
      • Description: text explanation of the purpose of the library (e.g. Exceptions used by the CommonLibraries)
      • Company: to your organization (e.g. C. Pottinger)
      • Assembly Version: Major.Minor.Update.Build
  2. Build in Debug and Release mode
  3. Open cmd prompt in the same directory as the {project}.csproj
>nuget spec {project}.csproj

Edit the {project}.nuspec file:

  1. remove licenseUrl
  2. remove projectUrl
  3. remove iconUrl
  4. set tags
  5. add dependencies (see https://docs.microsoft.com/en-us/nuget/reference/nuspec#dependencies-element)
 >nuget pack

Add the package to the local repository

>nuget add {project}.{version}.nupkg -source {nugetrepositorydirectory} 

One issue I had was that when adding a package with dependencies, NuGet would fail to find the dependencies. For example, when using NuGet to add package ExtensionsLibrary (which depends on ExceptionsLibrary) I would get:

Unable to resolve dependency 'ExceptionsLibrary'. Source(s) used: 'nuget.org', 'Package source', 'Microsoft Visual Studio Offline Packages'.

The solution that worked for me was to, within Visual Studio, go to the Tools ->Nuget Package Manager Settings -> General and change the Default package management format to PackageReference. Thanks to Babu Annamalai who posted this helpful entry.

Rebuilding the package

If you need to make a change to a package that has already been added to the NuGet repository, then remember to delete the package version folder in the NuGet repository first. You cannot add the same version twice.

To use the updated version in any programs making use of NuGet, it will not be enough to remove the existing NuGet package from the project in Visual Studio and add it in again. NuGet maintains a cache of the packages and you will probably be just re-installing the old, cached package, rather than the new one.

First, remove the package from the project. Then, in VS, select Tools -> NuGet Package Manager -> Package Manager Settings -> Clear All NuGet Caches. Now the package can be re-added to the project, and the latest version should load.

Including additional files with the package

If there are ancillary files that are to be distributed with the package (e.g. a README.md file), use the following method:

  • Create a new file in the same directory as the package project’s .csproj file (suggested filename is the package name with an extension of “.targets”)
  • The file contents should be (replace ~filename~ with desired file’s name):
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)\~filename~">
      <Link>~filename~</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>
  • Add the following to the package project’s .csproj file:
  <ItemGroup>
    <None Include="~filename~">
      <Pack>true</Pack>
      <PackagePath>build</PackagePath>
    </None>
    <None Include="~package name~.targets">
      <Pack>true</Pack>
      <PackagePath>build</PackagePath>
    </None>
  </ItemGroup>

(see https://stackoverflow.com/questions/42850493/how-do-i-include-a-text-file-alongside-my-class-library-in-my-nuget-package-whic)

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s