Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet Limit max major version of a package

Is it possible to tell NuGet that you want to use the latest version of package with particular major version and never above this major version?

For instance, let's take the example with jQuery: we can't use 2.x version if we need support of the old browsers. We're limited to use latest 1.x version.

We know that the currently available (and offered by default in Manage Nuget Packages dialog) version of jQuery is 2.1.1. Rather than using this version, we need to use the currently latest major version of the jQuery 1.x branch, currently, 1.11.1.

I know how to initially install the 1.11.1 version. One simple solution is to manually edit the packages.config file to replace the value of the version attribute with 1.11.1:

<packages>
  <package id="jQuery" version="1.11.1" targetFramework="net451" />
</packages>

and build the project.

What I don't know is how to tell NuGet to keep track of releasing new versions of the 1.x branch to offer me updating to them the same way as it would do this when a new 2.x version is released:

How NuGet Add-In notifies of a new version of a package

By default, NuGet will always try to update to the latest 2.x version, not the latest 1.x.

In the other words, when using packages with old major versions, you need to manually check for releases and make NuGet installing them every time.

I've tried to use an asterisk in the package version attribute:

<packages>
  <package id="Package1" version="1.*" targetFramework="net40" />
</packages>

But NuGet throws an exception:

NuGet Package restore failed for project Project1: System.IO.InvalidDataException: Unable to parse version value '1.*' from 'packages.config'.
   at NuGet.PackageReferenceFile.<GetPackageReferences>d__0.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at NuGet.VsEvents.PackageRestorer.RestorePackages(String packageReferenceFileFullPath, IFileSystem fileSystem)
   at NuGet.VsEvents.PackageRestorer.PackageRestore(ProjectPackageReferenceFile projectPackageReferenceFile).

So, how do you achieve the above goal?

like image 765
Alexander Abakumov Avatar asked Sep 19 '14 18:09

Alexander Abakumov


1 Answers

I found a solution. You need to use a NuGet version constraint.

Continuing with the jQuery example, if you need to update jQuery to the latest 1.x branch version, but not to 2.x, add the following constraint to the packages.config:

<packages>
  <package id="jQuery" version="1.11.0" allowedVersions="[1,2)" targetFramework="net451" />
</packages>

This lets you be notified about new releases of the 1.x branch only and never be offered to update to inappropriate for you 2.x version:

enter image description here

like image 164
Alexander Abakumov Avatar answered Sep 19 '22 12:09

Alexander Abakumov