{"id":2361,"date":"2022-04-12T09:37:51","date_gmt":"2022-04-12T16:37:51","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=2361"},"modified":"2022-04-12T09:37:51","modified_gmt":"2022-04-12T16:37:51","slug":"introducing-central-package-management","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/introducing-central-package-management\/","title":{"rendered":"Introducing Central Package Management"},"content":{"rendered":"<h2>Central Package Management<\/h2>\n<p>Dependency management is a core feature of NuGet. Managing dependencies for a single project can be easy. Managing dependencies for multi-project solutions can prove to be difficult as they start to scale in size and complexity. In situations where you manage common dependencies for many different projects, you can leverage NuGet&#8217;s central package management features to do all of this from the ease of a single location.<\/p>\n<p>Historically, NuGet package dependencies have been managed in two main locations:<\/p>\n<ul>\n<li><code>packages.config<\/code> &#8211; An XML file used in older project types to maintain the list of packages referenced by the project.<\/li>\n<li><code>&lt;PackageReference \/&gt;<\/code> &#8211; An XML element used in new project types that manages NuGet dependencies directly within project files.<\/li>\n<\/ul>\n<p>Starting with <a href=\"https:\/\/learn.microsoft.com\/en-us\/nuget\/release-notes\/nuget-6.2\">NuGet 6.2<\/a>, you can centrally manage your dependencies in your projects with the addition of a <code>Directory.Packages.props<\/code> file.<\/p>\n<p>The feature is available across all NuGet integrated tooling.<\/p>\n<ul>\n<li><a href=\"https:\/\/visualstudio.microsoft.com\/downloads\/\">Visual Studio 2022 17.2 and later<\/a><\/li>\n<li><a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet\/6.0\">.NET SDK 6.0.300 and later<\/a><\/li>\n<li><a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet\/7.0\">.NET SDK 7.0.0-preview.4 and later<\/a><\/li>\n<li><a href=\"https:\/\/www.nuget.org\/downloads\">nuget.exe 6.2.0 and later<\/a><\/li>\n<\/ul>\n<p>Older tooling will ignore Central Package Management configurations and features. To use this feature to the fullest extent, ensure all your build environments use the latest compatible tooling versions.<\/p>\n<p>Central Package Management will apply to all <code>&lt;PackageReference&gt;<\/code> projects \u2013 including <a href=\"https:\/\/github.com\/dotnet\/project-system\/blob\/main\/docs\/feature-comparison.md\">legacy .csproj<\/a> \u2013 as long as compatible tooling is used.<\/p>\n<h2>Enabling Central Package Management<\/h2>\n<p>To get started with central package management, you can create a <code>Directory.Packages.props<\/code> file at the root of your solution and set the MSBuild property <code>ManagePackageVersionsCentrally<\/code> to <code>true<\/code>.<\/p>\n<p>Inside, you can define each of the respective package versions required of your solution using <code>&lt;PackageVersion \/&gt;<\/code> elements that define the package ID and version.<\/p>\n<pre><code class=\"xml\">&lt;Project&gt;\n  &lt;PropertyGroup&gt;\n    &lt;ManagePackageVersionsCentrally&gt;true&lt;\/ManagePackageVersionsCentrally&gt;\n  &lt;\/PropertyGroup&gt;\n  &lt;ItemGroup&gt;\n    &lt;PackageVersion Include=\"Newtonsoft.Json\" Version=\"13.0.1\" \/&gt;\n  &lt;\/ItemGroup&gt;\n&lt;\/Project&gt;\n<\/code><\/pre>\n<p>Within a project of the solution, you can then use the respective <code>&lt;PackageReference \/&gt;<\/code> syntax you know and love, but without a <code>Version<\/code> attribute to infer the centrally managed version instead.<\/p>\n<pre><code class=\"xml\">&lt;Project Sdk=\"Microsoft.NET.Sdk\"&gt;\n\n  &lt;PropertyGroup&gt;\n    &lt;TargetFramework&gt;net6.0&lt;\/TargetFramework&gt;\n  &lt;\/PropertyGroup&gt;\n\n  &lt;ItemGroup&gt;\n    &lt;PackageReference Include=\"Newtonsoft.Json\" \/&gt;\n  &lt;\/ItemGroup&gt;\n&lt;\/Project&gt;\n<\/code><\/pre>\n<p>Now you&#8217;re using central package management and managing your versions in a central location!<\/p>\n<h2>Central Package Management rules<\/h2>\n<p>The <code>Directory.Packages.props<\/code> file has a number of rules with regards to where it&#8217;s located in a repository&#8217;s directory and its context. For the sake of simplicity, only one <code>Directory.Packages.props<\/code> file is evaluated for a given project.<\/p>\n<p>What this means is that if you had multiple <code>Directory.Packages.props<\/code> files in your repository, the file that is closest to your project&#8217;s directory will be evaluated for it. This allows you extra control at various levels of your repository.<\/p>\n<p>Here&#8217;s an example, consider the following repository structure:<\/p>\n<pre><code>Repository\n |-- Directory.Packages.props\n |-- Solution1\n     |-- Directory.Packages.props\n     |-- Project1\n |-- Solution2\n     |-- Project2\n<\/code><\/pre>\n<ul>\n<li>Project1 will evaluate the <code>Directory.Packages.props<\/code> file in the <code>Repository\\Solution1\\<\/code> directory.<\/li>\n<li>Project2 will evaluate the <code>Directory.Packages.props<\/code> file in the <code>Repository\\<\/code> directory.<\/li>\n<\/ul>\n<h2>Get started<\/h2>\n<p>To fully onboard your repository, consider taking these steps:<\/p>\n<ol>\n<li>Create a new file at the root of your repository named <code>Directory.Packages.props<\/code> that declares your centrally defined package versions in.<\/li>\n<li>Declare <code>&lt;PackageVersion \/&gt;<\/code> items in your <code>Directory.Packages.props<\/code>.<\/li>\n<li>Declare <code>&lt;PackageReference \/&gt;<\/code> items without <code>Version<\/code> attributes in your project files.<\/li>\n<\/ol>\n<p><!--For an idea of how central package management may look like, refer to our [samples repo](https:\/\/github.com\/NuGet\/Samples\/tree\/main\/CentralPackageManagementExample).--><\/p>\n<h2>Transitive pinning<\/h2>\n<p>You can automatically override a transitive package version even without an explicit top-level <code>&lt;PackageReference \/&gt;<\/code> by opting into a feature known as transitive pinning. This promotes a transitive dependency to a top-level dependency implicitly on your behalf when necessary.<\/p>\n<p>You can enable this feature by setting the MSBuild property <code>CentralPackageTransitivePinningEnabled<\/code> to <code>true<\/code> in a project or in a <code>Directory.Packages.props<\/code> or <code>Directory.Build.props<\/code> import file:<\/p>\n<pre><code class=\"xml\">  &lt;PropertyGroup&gt;\n    &lt;CentralPackageTransitivePinningEnabled&gt;true&lt;\/CentralPackageTransitivePinningEnabled&gt;\n  &lt;\/PropertyGroup&gt;\n<\/code><\/pre>\n<h2>Overriding package versions<\/h2>\n<p>You can override an individual package version by using the <code>VersionOverride<\/code> property on a <code>&lt;PackageReference \/&gt;<\/code> item. This overrides any <code>&lt;PackageVersion \/&gt;<\/code> defined centrally.<\/p>\n<pre><code class=\"xml\">&lt;Project&gt;\n  &lt;ItemGroup&gt;\n    &lt;PackageVersion Include=\"PackageA\" Version=\"1.0.0\" \/&gt;\n    &lt;PackageVersion Include=\"PackageB\" Version=\"2.0.0\" \/&gt;\n  &lt;\/ItemGroup&gt;\n&lt;\/Project&gt;\n<\/code><\/pre>\n<pre><code class=\"xml\">&lt;Project Sdk=\"Microsoft.NET.Sdk\"&gt;\n  &lt;PropertyGroup&gt;\n    &lt;TargetFramework&gt;net6.0&lt;\/TargetFramework&gt;\n  &lt;\/PropertyGroup&gt;\n  &lt;ItemGroup&gt;\n    &lt;PackageReference Include=\"PackageA\" VersionOverride=\"3.0.0\" \/&gt;\n  &lt;\/ItemGroup&gt;\n&lt;\/Project&gt;\n<\/code><\/pre>\n<p>You can disable this feature by setting the MSBuild property <code>CentralPackageVersionOverrideEnabled<\/code> to <code>false<\/code> in a project or in a <code>Directory.Packages.props<\/code> or <code>Directory.Build.props<\/code> import file:<\/p>\n<pre><code class=\"xml\">  &lt;PropertyGroup&gt;\n    &lt;CentralPackageVersionOverrideEnabled&gt;false&lt;\/CentralPackageVersionOverrideEnabled&gt;\n  &lt;\/PropertyGroup&gt;\n<\/code><\/pre>\n<p>When this feature is disabled, specifying a <code>VersionOverride<\/code> on any <code>&lt;PackageReference \/&gt;<\/code> item will result in an error at restore time indicating that the feature is disabled.<\/p>\n<h2>Disabling Central Package Management<\/h2>\n<p>If you&#8217;d like to disable central package management for any reason, you can disable this feature by setting the MSBuild property <code>ManagePackageVersionsCentrally<\/code> to <code>false<\/code>:<\/p>\n<pre><code class=\"xml\">\n&lt;PropertyGroup&gt;\n  &lt;ManagePackageVersionsCentrally&gt;false&lt;\/ManagePackageVersionsCentrally&gt;\n&lt;\/PropertyGroup&gt;\n<\/code><\/pre>\n<h2>Warning when using multiple package sources<\/h2>\n<p>When using central package management, you will see a <code>NU1507<\/code> warning if you have more than one package source defined in your configuration. To resolve this warning, map your package sources with <a href=\"https:\/\/aka.ms\/nuget-package-source-mapping\">package source mapping<\/a> or specify a single package source.<\/p>\n<pre><code>There are 3 package sources defined in your configuration. When using central package management, please map your package sources with package source mapping (https:\/\/aka.ms\/nuget-package-source-mapping) or specify a single package source.\n<\/code><\/pre>\n<h2>Closing<\/h2>\n<p>We hope that these features will help improve your productivity of managing your dependencies as they scale with your projects, promote healthy security best practices, and empower your team to achieve more with the ease of a single location. There is currently no support in Visual Studio or the .NET CLI for Central Package Management.<\/p>\n<h2>Feedback<\/h2>\n<p>This feature is considered in preview and is under active development. Your feedback is important to us. If there are any problems with this feature, check our <a href=\"https:\/\/github.com\/NuGet\/Home\/issues\/\">GitHub Issues<\/a>. For new issues within this experience, please <a href=\"https:\/\/github.com\/NuGet\/Home\/issues\/new\/choose\">report a GitHub Issue<\/a>. To help us with documentation, please feel free to <a href=\"https:\/\/docs.microsoft.com\/nuget\/consume-packages\/Central-Package-Management\">contribute and ask questions<\/a> on how we can explain the feature further.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Central Package Management Dependency management is a core feature of NuGet. Managing dependencies for a single project can be easy. Managing dependencies for multi-project solutions can prove to be difficult as they start to scale in size and complexity. In situations where you manage common dependencies for many different projects, you can leverage NuGet&#8217;s central [&hellip;]<\/p>\n","protected":false},"author":88861,"featured_media":2369,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7933,7874],"tags":[7970,7971,7972,7973],"class_list":["post-2361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-feature-announcement","category-nuget","tag-central-package-management","tag-central-package-version-management","tag-cpm","tag-cpvm"],"acf":[],"blog_post_summary":"<p>Central Package Management Dependency management is a core feature of NuGet. Managing dependencies for a single project can be easy. Managing dependencies for multi-project solutions can prove to be difficult as they start to scale in size and complexity. In situations where you manage common dependencies for many different projects, you can leverage NuGet&#8217;s central [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/88861"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=2361"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/2361\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/2369"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=2361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=2361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=2361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}