.NET: offline NuGet packages
Usually, a .NET project consumes standard and third-party libraries from nuget.org. For context, NuGet is .NET's de facto package manager, like npm for Javascript, PyPI for Python, Maven for Java, Conan for C++, and so on. It's common practice for an organization to own a NuGet repository hosted in a binary hosting service such as Artifactory or Nexus. If the organization develops its own libraries, they will be hosted in that particular repository. This way, libraries can be used across different development teams inside the organization, and also beyond it if libraries are chosen to be made public.
This is all well and good, but sometimes the hosting service goes down, or it is desirable to tinker with the libraries locally before deploying them. In scenarios like these, it's helpful to have an extra NuGet source that lives in the developer's machine and can be used when building and developing .NET applications. This is where the concept of offline packages comes in.
Interacting with NuGet sources can be done in an operating system and IDE-independent way by perusing the dotnet cli. For starters, to list the currently existing sources, in a terminal, run:
dotnet nuget list source
This will print a list of the NuGet sources currently configured for the system, and their Enabled/Disabled status. An example of such output would be:
Registered Sources: 1. nuget.org [Enabled] https://api.nuget.org/v3/index.json 2. Artifactory [Disabled] https://artifactory.organization.domain/artifactory/api/nuget/organization-nuget 3. Microsoft Visual Studio Offline Packages [Enabled] C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\ 4. Artifactory-dev [Disabled] https://artifactory.organization.domain/artifactory/api/nuget/v3/organization-nuget-dev
In this example, there's nuget.org (source 1), two Artifactory sources from the organization (2 and 4), and an offline source that is typically created automatically by Visual Studio upon installation (source 3). That source is bound to a local directory (C:\Program Files\...) instead of a URL like the others, which is a distinctive characteristic of a local source.
If an offline source does not exist, it can easily be created manually.
dotnet nuget add source <path-to-local-dir> --name <source-name>
If the source exists but is disabled, it can easily be enabled.
dotnet nuget enable source <source-name>
Once the local source exists and is enabled, all that is left is to push packages to it. Doing so may require administrative privileges depending on the directory. For the previous example, since the directory is inside C:\Program Files (x86), pushing to it will require admin rights for a limited Windows user. The same may happen in Linux if the local directory is inside a directory owned by the root user. In these cases, in Windows, the push command will need to be executed inside a terminal with admin rights. In Linux systems, sudo might need to be used. With that consideration, the command for pushing a package to a local source is:
dotnet nuget push <library-name-with-version>.nupkg -s <source-name>
It's important to remember that .nupkg binaries are the format that will be pushed. These binaries usually include the version number in their name; keep in mind that both package name and version number are what identifies the exact version to use. Also, using semantic versioning for all version numbers is recommended.
That's it for today! Happy holidays!
Comments
Post a Comment