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