Ryan D'Baisse
Ryan D'Baisse

Reputation: 863

How to Sync NuGet Packages Across Multiple Shared Projects & Solutions?

What is the proper technique for keeping NuGet packages in sync across multiple shared projects and solutions within Visual Studio? We share 200+ projects across multiple solutions. Each TRACK has a handful of solutions which have some or all of the track's projects. We also have several PLATFORM solutions which pull in multiple tracks as well as any number of the track's projects. Unfortunately, we have a few vendors that REGULARLY push out updates to their packages and developers inadvertently install / update packages while working in different solutions. We need to ensure the packages are updated regardless of what solution uses them.

Unfortunately, NuGet's site discusses how to perform the actual update, but not how to synchronize everything... http://docs.nuget.org/docs/start-here/Managing-NuGet-Packages-Using-The-Dialog#Updating_a_Package

Upvotes: 9

Views: 4781

Answers (4)

Michael Freidgeim
Michael Freidgeim

Reputation: 28511

To synchronise projects within solution starting from VS 2017 you can use Consolidate tab

or NuGet CLI command Sync-Package as an alternative to consolidate tab https://learn.microsoft.com/en-us/nuget/reference/ps-reference/ps-ref-sync-package.

Example

# Sync the Elmah package installed in the default project into the other projects in the solution
 Sync-Package Elmah

To synchronise projects across multiple solutions is not supported out of the box( it makes sense as nothing wrong to keep different versions in independent solutions) But you can do a hack- create a huge solution that includes projects from all solutions that you want to upgrade and sync them.

Upvotes: 0

wmorian
wmorian

Reputation: 446

It is possible to maintain all Nuget versions within a solution in a single .props file. However, this procedure requires some steps to be performed manually. I have seen this approach partially used in the dotnet/cli project on github:

  1. Create a new file with the extension .props, for example: Versions.props

  2. For each package in your solution (in all projects), create a property with the corresponding version (use of version ranges is possible)

<Project>
  <PropertyGroup>
    <log4netVersion>2.0.8</log4netVersion>
    <UnityVersion>5.9.*</UnityVersion>
  </PropertyGroup>
</Project>
  1. Import the .props file in every .cspoj file
<Import Project=".\version.props" />
  1. Replace version numbers in .cspoj files with the corresponding property form the .props file
<ItemGroup>
    <PackageReference Include="Unity" Version="$(UnityVersion)" />
    <PackageReference Include="log4net" Version="$(log4netVersion)" />   
</ItemGroup>

As described above, this procedure musst be done manually. Installing nuget packages with the nuget package manager will overwrite the version properties!

  1. Run dotnet restore on the solution to update all packages and dotnet list package to list all requested and resolved packages per project!

*enter image description here*

Upvotes: 3

chris smith
chris smith

Reputation: 171

This isn't technically correct that you can't do this. You can use the built-in tools in Visual Studio 2010 / 2012 to do this for you.

In order to update packages in all the projects at the same time, you can launch the Manage NuGet Packages dialog at solution level.

So right-click the solution, then choose "Manage NuGet Packages for Solution". And presto! You're off and away!

Now, while it won't "sync" them automatically, it does allow you to see where you have duplicates and help you to move each of your projects to the proper versions of nuget packages you want to use.

Not a silver bullet, but it will help.

Upvotes: 8

XeroxDucati
XeroxDucati

Reputation: 5200

There's no really good way to do this unfortunately.. What we do is run our own NuGet repository. When we want to force an update of a package, we delete the old version entirely, and upload the new one.

This doesn't guarantee that the developers pull the latest package of course, so we've made it part of our CI process to automatically pull all the NuGet packages into the solution prior to building, so if some package is updated and the developers haven't caught up, their next commit will fail the CI build, forcing them to go fix it.

It's kind of a workaround, but it's the best we've been able to come up with..

Upvotes: 3

Related Questions