11

I am creating a nuget-package with /t:pack in my TFS-Build. I can't the use Nuget-Pack-Step, because I am using

<TargetFramework>netstandard2.0</TargetFramework>

How can I apply my AssemblyVersion on the Nuget-Package? Because my Assembly-Version is right, but my Nuget version always remains 1.0.0.0.

Note I am using a C# file for my assembly information instead of the .csproj file.

Is there any possibility to it?

Want to share that link.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Peter
  • 1,655
  • 22
  • 44
  • 2
    https://github.com/lextm/sharpsnmplib/blob/10.0.2/SharpSnmpLib/SharpSnmpLib.csproj#L26 The package version comes from the `Version` tag of your project, so you should try to modify that. – Lex Li Dec 14 '17 at 16:55
  • I am using an AssemblyInfo.cs instead of the csproj-file. But I cant find the equivalent to it. Should be sth like [assembly: AssemblyVersion("4.0.0.0")). – Peter Dec 14 '17 at 17:14
  • There "should be", but there isn't yet. You can still use a `.nuspec` if you want to (as I have a project using that trick). – Lex Li Dec 14 '17 at 17:14

2 Answers2

17

The MSBuild-integrated Pack target reads its value from MSBuild properties inside the project (PackageVersion to be specific, which is defaulted from Version, which in turn is defaulted to VersionPrefix which in turn may be suffixed by VersionSuffix).

There is out-of-the-box support for reading this value from an assembly attribute since the new project format is meant to generate these assembly attributes from the same configuration that determines NuGet package metadata.

However, you can extend the build by adding a custom target to the csproj file that reads the built assembly's identity during msbuild /t:Pack:

<Project>

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <GenerateNuspecDependsOn>$(GenerateNuspecDependsOn);ReadPackageVersionFromOutputAssembly</GenerateNuspecDependsOn>
  </PropertyGroup>

  <Target Name="ReadPackageVersionFromOutputAssembly" DependsOnTargets="Build">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="PackAssembly" />
    </GetAssemblyIdentity>
    <PropertyGroup>
      <PackageVersion>%(PackAssembly.Version)</PackageVersion>
    </PropertyGroup>
  </Target>

</Project>

Note that this target will only run on the "full MSBuild", that is msbuild.exe on windows (visual studio developer command prompt) or mono 5.2+ on linux/Mac. This currently does not work for dotnet pack (.NET Core version of MSBuild). UPDATE: This will now work in .NET SDKs 2.1.* and higher since the GetAssemblyIdentity task has been added to the cross-platform version of msbuild in 2018.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
9

With current version of MSBuild it is possible to specify PackageVersion parameter:

msbuild ProjectName.csproj -t:Pack -p:PackageVersion=1.2.3
Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59