Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

tfs - MSBuild /t:pack Nuget-Package has always the same Version

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...