Once you get used to adding assembly references by installing NuGet packages instead of pointing directly to assembly files on disk, you’ll soon want to do the same with your own reusable class libraries, as well. Since all packages in the official NuGet gallery are free and available to everyone, you’ll have to publish them to a different package source to restrict them to internal use.
The simplest way to set up your own NuGet package source would be to create a folder or file share and put the NuGet packages in it. While this works in simple scenarios, you will soon encounter many limitations:
- The lack of separate metadata storage affects performance once the number of packages increases.
- There are almost no management features, since the package source is just a file folder.
- You can’t create a proxy repository for it in Nexus.
First party options for creating a proper package source are not very impressive:
- You can create your own web application, based on NuGet.Server NuGet package.
- You can host your own instance of the official NuGet gallery.
For a more out-of-the-box solution you’ll need to take a look at third party offerings, such as Sonatype Nexus. Inside it, such package sources are called hosted repositories. This article will walk you through the process of creating one, adding packages to it and using them from Visual Studio.
Similar enough to adding a proxy repository, you can create a hosted repository by clicking on Add… > Hosted Repository from the menu above the list of repositories (don’t forget to log into Nexus, otherwise you won’t see the command).
There’s even less data to enter than for a proxy repository:
- Repository ID: nuget-internal
- Repository Name: NuGet Internal Feed
- Provider: NuGet
Once you’ve created it, you can add it as a package source to NuGet Package Manager by following the instructions for a proxy repository. Of course there aren’t going to be any packages yet in it; we’ll have to add them ourselves.
Creating a NuGet package is out of the scope of this article. To follow this tutorial you can use any valid NuGet package; either created by you, or downloaded from the official NuGet gallery. To find one, you can always check the contents of packages folder next to the Visual Studio solution file, containing a project with a package installed. They are always placed inside a subfolder, named according to the package ID and version (e.g. packages\MvvmCross.3.1.1\MvvmCross.3.1.1.nupkg for version 3.1.1 of MvvmCross).
Manually Add a Package to the Repository
If you need to manually add a package to the repository, you can upload it directly from the Repositories page. Just select the correct repository from the list and click on the NuGetPkg Upload tab below the list. Multiple packages can be uploaded at the same time by selecting their *.nupkg files one by one and adding them to the list box below.
Once the upload is completed, you can switch to the Browse Storage tab to see all the packages in the repository. Click on Refresh to see the latest data if a recently uploaded package is missing, because you haven’t reloaded the page. Here you can search through the packages by their name, and check some basic information about them. You can also Delete a previously uploaded package or Download it without a NuGet client. Too bad that not all metadata is extracted from the package and displayed here.
This is not the usual way for publishing packages, though. You’ll probably be creating your NuGet packages as part of the build process, therefore you’ll want to publish them using NuGet.exe, as well. This will work almost the same, as if you were publishing to the official NuGet gallery.
Since this functionality is disabled by default, you’ll first need to enable it. In the menu on the left open the Administration group and navigate to the Server page. Scroll down to Security Settings and add NuGet API-Key Realm from the Available Realms to the Selected Realms list. Don’t forget to save the changes afterwards.
Navigate back to Repositories, select your hosted NuGet repository and click on the NuGet tab below the list. Here’s all the data you need to configure NuGet.exe for package publishing to this repository. Click on the View API Key button to see the API key, as well. Each user has his own API key which can be reset at any time in case it was compromised. The API key si the only authentication necessary for publishing the packages.
Now open a command prompt and make sure NuGet.exe is in your path (it’s not distributed with NuGet Package Manager; you will need to download it separately). You can associate the API key with the package source, so that you don’t need to enter it every time:
nuget setApiKey 626d5939-965f-30bc-858e-a324672acf54 -Source http://localhost:8081/nexus/service/local/nuget/nuget-internal/
Until the API key changes, you will be able to publish new packages by only supplying the selected package source:
nuget push .\MyPackage.1.0.0.nupkg -Source http://localhost:8081/nexus/service/local/nuget/nuget-internal/
This command can be safely included in the build server configuration without insecurely storing the API key there, as well. The build process can now completely automatically build the assemblies, run the tests, create the package, and upload it to the repository. If all of this passes without errors, a new version of the package will be published and ready to use, without any human interaction.
About the Author
Damir Arh has been involved with Microsoft technologies, since he has been a student. He has many years of experience with development and maintenance of complex enterprise software in the field of document management, insurance, and process management. Recently he has shown increased interest in the heterogeneous ecosystem of mobile devices and cross-platform development.
Damir is always looking for tools and processes to make development easier and more manageable, making him a big proponent of test driven development, continuous integration and continuous deployment. He coauthored a book on NuGet – the package manager for .NET framework.
He likes to share his knowledge with others by answering questions on Stack Overflow, blogging and regularly speaking at local user groups and conferences. For his contributions to the community he is an awarded Microsoft MVP since 2012.
Latest posts by Damir Arh (see all)
- NuGet Is Down. Now What? - December 15, 2014
- NuGet and Nexus: Use a Repository Group to Consolidate Multiple Repositories - November 11, 2014
- Nexus and NuGet: Create a Hosted Repository - October 17, 2014
- Get Started with Nexus and NuGet - October 6, 2014
- Nexus for .NET Developers: New Series - September 9, 2014