Nexus and NuGet: Create a Hosted Repository

Author, Damir Arh

Author, Damir Arh

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.

An Overview

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.
  • Etc.

First party options for creating a proper package source are not very impressive:

Using Nexus

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).

Nexus and NuGet - Setup a Repository

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.

Nexus and NuGet - Setup a Repository

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.

Nexus and NuGet - Setup a Repository

Enable Publishing

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.

Nexus and NuGet - Setup a Repository

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.

Nexus and NuGet - Setup a Repository

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.

Blog: http://www.damirscorner.com
Twitter: http://twitter.com/DamirArh

The following two tabs change content below.

Damir Arh

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.
Authors

Related posts

7 Comments

  1. Rajendra said:

    How one can create folder in Nexus Nuget repository? Is there any command line option for the same?

  2. Damir Arh said:

    Rajendra, can you explain in a little more detail what you mean by creating a folder in NuGet repository?

    There’s no concept of hierarachical organization of packages in NuGet – the packages are searchable by their id tags and other properties. In the file system Nexus organizes the files first by package id and then by package version, just like they are stored locally after installation. The folder where NuGet puts all the packages can be defined in Nexus administration for the feed in the Configuration tab (Local Storage Location).

  3. Khue Pham said:

    Hi Damir,

    I want to write batch script to automatically create Nexus NuGet repository (for automation) using Nexus REST API.

    Preferably using JASON (but XML is OK).

    Do you happen to know how ?

    Many thanks for all your help in advance.

    Best regards,

    Khue Pham

  4. Balu said:

    Hi Damir,

    Thanks so much. This helps me lot.

    How do we download nuget package from nexus using command line ? I am looking for automate this process ( Upload and download ) through series of commands.

    Thanks in advance .

  5. Rohith said:

    Hi Damir,
    I am trying to push my packages to nexus by giving Api key, but it throws be this error
    Response status code does not indicate success: 403 (Forbidden).

*

Top