question

Kasrak-1517 avatar image
0 Votes"
Kasrak-1517 asked DylanZhu-MSFT answered

Read a nuget package library version in the target assembly during installation

How to find the installing package library version in the target assembly?

Assume that:

We have NugetLib and ConsumerApp (which will install NugetLib).

I am using .targets file in NugetLib named the same, and it will run on the target when installed.

Inside NugetLib.targets file:

   <Target Name="Main" AfterTargets="Build">
     <Message Text="$(NugetLibVersion)" Importance="high" />
   </Target>

Currently I don't have the correct value of NugetLibVersion, and it needs to be set somehow.

Using $(PackageVersion)" will not help, cause it doesn't read and provide NugetLib version, it will provide ConsumerApp version (the target assembly, which will always be 1.0.0.0).










vs-msbuild
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DylanZhu-MSFT avatar image
0 Votes"
DylanZhu-MSFT answered

Hi Kasrak-1517,

You need to use *GetAssemblyIdentity* task to read information of the assembly, which is like this:
17193-annotation-2020-08-12-185246.jpg

You could refer to more details from this thread.

In addtion, if the nuget package is referenced in ItemGroup section, which is like this:

   <ItemGroup>
     <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
       <HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
     </Reference>
   </ItemGroup>

You could also directly read Hintpath value, which contains the version of nuget package. The code is like this:

 <Message Text ="%(Reference.HintPath)"/>

Best Regards,
Dylan



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.