NuGet and Nexus: Use a Repository Group to Consolidate Multiple Repositories

Author, Damir Arh

Author, Damir Arh

Adding NuGet repositories (both proxy and hosted) to Sonatype Nexus is easy. However, to be able to use these repositories from Visual Studio, each developer needs to add them as package sources in his own environment. As a result, you really don’t want to have too many repositories or change them too often, because it will require developers to change their configuration. Fortunately, Sonatype Nexus offers a solution for this scenario, as well – it is called: repository group.

The idea of a repository group is to merge multiple different repositories into one and give it a single address for clients to use, hiding from them any changes to the repositories included in the group. This allows many different scenarios for abstracting the details of actual repositories and allowing easier management:

  • Have a single repository group for developers, containing the official NuGet feed proxy, proxies of third party public feeds, and any number of private hosted repositories.
  • Have different repository groups for different branches of development (releases, release candidates, development branches).
  • Have different repository groups for different projects (e.g. some of them allow usage of specific third party packages, others don’t).

All of this gives a lot of flexibility to policies for using different sets of packages in different circumstances, without burdening developers with details.

Create a Repository Group

Creating repository groups in Sonatype Nexus is very simple. You start by clicking on Add… > Repository Group in the menu above the repository list (you need to be logged into Nexus as an administrator).

NuGet - Multiple Repositories in a Repository Group

As with any repository, you then need to fill out the basic data about the feed:

  • Repository ID: nuget-group
  • Repository Name: NuGet Repository Group
  • Provider: NuGet

Of course, the most important part is selecting which of the previously configured NuGet repositories you want to include into your new group repository.

NuGet - Multiple Repositories in a Repository Group

The feed will contain all the packages from all the repositories that you have selected. Keep in mind that you need to create a proxy repository for any third party public repositories you want to include.

An Alternative Solution

While adding repositories as NuGet package sources from within Visual Studio works fine for developers, it’s not a perfect solution for build servers. NuGet settings, configured from Visual Studio, are stored per user (the configuration file can be found in %AppData%\NuGet). There are alternative approaches to configuring NuGet which are user agnostic and can even be checked into source control, making them much more suitable for build servers.

Typically in the automatic build process NuGet is only used for restoring the packages which are used by the project or solution, because they are not checked into source control along with the source code. When “NuGet.exe restore MyProject.sln” is called to achieve that, NuGet scans the directories from the current one up to the root, searching for NuGet.config file. You can find details about configuration file hierarchy in NuGet documentation, but in our case, it is enough to override the package source settings by using the following NuGet.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <packageSources>
      <clear />
      <add key="Nexus Repository Group"
      value="http://localhost:8081/nexus/service/local/nuget/nuget-group/" />
 </packageSources>
</configuration>

Notice the clear element has been used to remove any package source defined elsewhere. Also, replace the URL in the value attribute to match your environment. Except for testing purposes, you will want to use the actual server name instead of localhost.

Put the file in the root of your source control repository, usually alongside with the solution file. When the source is checked out on the build server and NuGet starts restoring the packages, it will read this configuration file and use the listed package source instead of the default one.

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

*

Top