NuGet Central Package Management (CPM)
Have you been working in a large code base and feel the pain of updating NuGet package version across many projects? Not only is it painful to update all the version in every projects, it is especially painful when you try to review a pull request with 99 files updating the .csproj
and only 3 files with actual code changes. We solve the issue with NuGet Central Package Management (CPM) and I am going to show you how.
Why shouldn’t I just follow the official documentation?
Yes, you can. As a matter of fact, we did it at first. It is just very labor intensive to do it by hand and I want to show you some tools and tips. I still highly suggest you give the official document a read and reference it in case you are stuck at any point. Also, the following step assume you are using SDK style .csproj
and the same package version is used across solution. Refer to documentation if your situation is more complex then that.
CentralisedPackageConverter
CentralisedPackageConverter is the tool that do the manual work and super helpful. It basically automate the steps in the official document. All you need to do is download the repo and run:
dotnet tool install CentralisedPackageConverter --global
After that, run:
central-pkg-converter C:\Projects\YourSolution
C:\Projects\YourSolution
being the actual path to your solution.
Congratulation! You are basically done at this point. The rest are optional.
Nuget.Config
If you are only using public packages, you can skip this section. If you have internal packages from custom package sources other than nuget.org, you need to add the following to Nuget.Config
file for the solution.
<packageSourceMapping>
<packageSource key="nuget.org">
<package pattern="*" />
</packageSource>
<packageSource key="packages.mysource.com">
<package pattern="Mysource.*" />
</packageSource>
</packageSourceMapping>
This tells NuGet which source to grab the NuGet packages from based on the package name pattern. You don’t need this straightly speaking but you will get a warning and it is generally not good security practice to lose control of your package sources.
Directory.Packages.Props
Directory.Packages.Props
is the file that now contain all the packages version. A handy thing you can do is put Directory.Packages.Props
in your solution items folder. This make Directory.Packages.Props
shows up in your IDE under Solution Items
and make it easy to access. You do this by adding the following to your .sln
file:
Project("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}") = "Solution Items", "Solution Items", "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"
ProjectSection(SolutionItems) = preProject
Directory.Packages.props = Directory.Packages.props
EndProjectSection
EndProject
Conclusion
CPM is a handy NuGet feature that can streamline package management. Give it a try and see if you like it.