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:
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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With