Setting the version number for .NET Core projects - CSPROJ - not JSON projects

ε祈祈猫儿з 提交于 2019-11-27 17:36:35
Martin Ullrich

You can override any property from the command line by passing /p:PropertyName=Value as arguments to dotnet restore, dotnet build and dotnet pack.

Currently, Version composition works as this: If Version is unset, use VersionPrefix (defaults to 1.0.0 if unset) and - if present - append VersionSuffix.

All other version are then defaulted to whatever Version is.

So for example you can set <VersionPrefix>1.2.3</VersionPrefix> in your csproj and then call dotnet pack --version-suffix beta1 to produce a YourApp.1.2.3-beta1.nupkg (if you have project reference that you want the version suffix to be applied to as well, you need to call dotnet restore /p:VersionSuffix=beta1 before that - this is a known bug in the tooling).

Of course, you can use custom variables as well, see this GitHub issue for a few examples.

For a complete reference of supported assembly attributes, i suggest looking at the source code of the build logic here (the values surrounded with $() are the properties used). And since i'm already talking about the source, this is the logic that composes the version and a few other properties.

dotnet build /p:AssemblyVersion=1.2.3.4

Does that work for you?

To answer your question straight: The new SDK for msbuild is auto generating an assembly info file. You can suppress that using msbuild directives (to see the it by sample: invoke dotnet migrate on a project.json based project).

But let me tell you my handling: I had multiple projects sharing the same version. I added a version.props file which contained a property group including a item named VersionPrefix. This file I included via the csproj file (Include statement). I also removed all AssemblyInfo.cs files and let the SDK generate them for me.

I modify the version.props file during build.

I use Jenkins + Octopus for CI, and following worked quite well:

  1. Have a pre-build Powershell script that can take CI build number as a param or default to something preset.
  2. Have a separate nuspec file in the project for CI.
  3. The pre-build script would update nuspec file with the latest build version.
  4. Publish project with Jenkins.
  5. Call Nuget manually with a nuspec file from #2.
  6. Push the nuget package to Octopus.

MsBuild 2017 will generate some assembly info if you missing them in Project file.

If you can read the msbuild target file you can look at:

[VS Install Dir] \MSBuild\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.GenerateAssemblyInfo.targets

You will see you can use some property in you project file to disable generated assembly info to prevent duplicated with your generated tools.

  • <GenerateAssemblyInfo> (This property will on/off all generate assembly info)
  • <GenerateAssemblyCompanyAttribute>
  • <GenerateAssemblyConfigurationAttribute>
  • <GenerateAssemblyCopyrightAttribute>
  • <GenerateAssemblyDescriptionAttribute>
  • <GenerateAssemblyFileVersionAttribute>
  • <GenerateAssemblyInformationalVersionAttribute>
  • <GenerateAssemblyProductAttribute>
  • <GenerateAssemblyTitleAttribute>
  • <GenerateAssemblyVersionAttribute>
  • <GenerateNeutralResourcesLanguageAttribute>

In my case, the main key was /property:Version=1.2.3.4. And the following command line did the job:

dotnet build SolutionName.sln -c Release /property:Version=1.2.3.4

This will override Assembly default version.

Tagc

As I've answered here, I've made a CLI tool called dotnet-setversion which you can use for versioning *.csproj-style .NET Core projects.

During a CI build, you can use GitVersion or another tool to determine a version number for your project and then invoke dotnet-setversion $YOUR_VERSION_STRING in your project root directory.

Passing /p:PropertyName=Value as arguments does not work for me (ASP.Net Core 2.0 Web App). I found the Manifest Versioning Build Tasks on marketpace: https://marketplace.visualstudio.com/items?itemName=richardfennellBM.BM-VSTS-Versioning-Task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!