If you’ve ever upgraded a Sitecore project, you’ve experience the joy of trying to search and replace all of the nuget package versions in the Package references of csproj files. Invariably these are scattered throughout the tens (hundreds?!) of projects in “Helix” based solutions, so can be time consuming and error prone to repetitively upgrade the package versions. Wouldn’t it be great if all package versions could be managed in one place?
There has been a solution to this floating around in .NET projects for a while, but I only first noticed it used in Sitecore solutions when I was looking at the docker-examples repo to see how the custom-images example was upgraded from Sitecore 10.0 to 10.1. This commit https://github.com/Sitecore/docker-examples/commit/1c2eee33685d317d6cdd7cad7d236cb97da95882 is where the magic happens. What jumped out at me was the Packages.Props file and the ease of simply changing the versions in this single file at a solution level, which will then flow down into all projects using the Microsoft.Build.CentralPackageVersions SDK. This is not related to or dependant on using docker, just happens to be a great example of usage in a Sitecore project.
If your solution currently uses PackageReference (it should), you can convert your solution to use Central Package Mangement in a few steps:
- Add a Packages.props file to the root of your solution. You can use the docker-examples file as a guide (https://github.com/Sitecore/docker-examples/blob/develop/custom-images/Packages.props)
- Add a PackageReference Element for each package in your solution
- Ensure you use the “Update” property (rather than Include)
- Add variables for versions that are logically grouped (EG. PlatformVersion) and reuse the values.
- Remove Versions element from all .csproj files
- Add the Microsoft.Build.CentralPackageVersions SDK to your projects via one of these methods below. The first is easiest and applies consistency as it will apply to all projects in your solution. The second gives more granular control, but means new projects may get forgotten about if you are not using a scaffolding solution.
Centrally, by adding Directory.Build.targets to the root of your solution.
<Project> <Sdk Name="Microsoft.Build.CentralPackageVersions" Version="2.0.79" /> </Project>
OR, Individually, just add the SDK element (above) to each of your .csproj Project nodes in the projects you wish to centrally manage.
This is a really quick win for your future self and team, allowing easy upgrades of all of your referenced Nuget packages. Shout out to the contributors to the https://github.com/Sitecore/docker-examples repo, it’s a great reference for developers on setting up your solution structure (even if not using docker!).