Pratik
Pratik

Reputation: 755

How to get version of the installed nuget in MSBuild?

I have to show the user version-specific error message (what features would not work based on currently installed nuget version).

Is there a way to detect the version being used of a specific nuget package through MSBuild?

I know a way to search the filesystem for the DLL and detect the version, but this doesn't seem clean solution. Is there something out of the box?

Upvotes: 0

Views: 1473

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100601

There is a target usable for customisations like this that was previously part of the build in 1.* but is still around for compatibility: ResolvePackageDependencies.

You can use it in msbuild like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.*" />
    <PackageReference Include="DasMulli.Win32.ServiceUtils" Version="*" />
  </ItemGroup>

  <Target Name="PrintPackageReferences" DependsOnTargets="RunResolvePackageDependencies">
    <Message Text="Dependencies:%0A    @(PackageDefinitions->'%(Name), Version: %(Version)', '%0A    ')" Importance="High" />
  </Target>

</Project>

Which (at the time of writing) produces:

  > dotnet msbuild -restore -t:PrintPackageReferences -nologo

  Restore completed in 14.56 ms for C:\demos\testcons\testcons.csproj.
  Dependencies:
      DasMulli.Win32.ServiceUtils, Version: 1.2.0
      Newtonsoft.Json, Version: 12.0.2

Upvotes: 2

Related Questions