Using NueGet packages and keep the versions upto date is a requirement as well as a challange if the versions are defined all over in many projects, in a given dotnet solution. We can limit the reference to packages to refer only in one project for a solution as consuming projects will inherently get the references. However, when it comes to using unit test projects we must reference the packages in all test projects to ensure test dicoverability in pipelines as well as in Test Explorer of the Visual Studio. Therefore, centralize Nuget Package management is essential in a complex and large solutions.
The Directory.Packages.props is the saviour for this requirement which allows us to define all the package versions centrally in the solution, by adding a Directory.Packages.props to the root of the repo, similar to using nuget.config.
We can define a Directory.Packages.props and setup central version management as shown below.
Once we setup Directory.Packages.props file with the above setup in the root of the solution repo, the version management of NuGet packages would be via the defined central Directory.Packages.props. There is option to place another Directory.Packages.props in a subfolder, which will get applied to projects in that subfolder or folders under it, intead of refering versions from the root Directory.Packages.props. It is possible to refer to root Directory.Packages.props from a subfolder Directory.Packages.props file. Learn more here. However, keeping a single Directory.Packages.props can be recommended to have simplicity of management of NuGet package versions.
After the Directory.Packages.props is added with <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> any NuGet package added to a project either via NuGet package manager window in Visual Studio or using command dotnet add package <PACKAGE_NAME>, will get added as shown below.
Let's say we are adding xUnit related packages to a project in the solution, while having a Directory.Packages.props in the repo root.
The packages are gettting added to the project as shown below without specifying the versions.
The Directory.Packages.props file will be automatically updated to below.
This essentialy allow you to control the versions of nuget packages used across all projects in your solution in a centaralized manner, which would be helpful for maintaining the versions of NuGet packages. You can nicely organize them as shown in the first picture in this post.
If you are using dockerfile to define how to build the container for your project. now you have to include the Directory.Packages.props file as well alongside the nuget.config file, so that required versions of NuGet packages can be restored in a docker build.
No comments:
Post a Comment