Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting .net core library assembly/file/nuget package version at build time

I'm after a way to set my .net core library's assembly version (and file version and nuget package version) at build time. My library is written using the latest Visual Studio 2017 RC so no more projects.json file and is being built by TeamCity with a standard powershell buildscript that invokes dotnet restore dotnet build dotnet test and of course dotnet pack.

Been scanning the web for an elegant solution but haven't found anything even close to it. All I could find on the interweb is for the now obsolete xproj and projects.json format.

I'm quite surprised that the dotnet build and dotnet pack commands don't support this out of the box.

Thanks :)

like image 774
Duy Avatar asked Feb 17 '17 08:02

Duy


People also ask

How to Create Nupkg file in Visual Studio?

Run the pack command Select Build > Configuration Manager, and then set the Active solution configuration to Release. Select the AppLogger project in Solution Explorer, and then select Build > Pack. Visual Studio builds the project and creates the . nupkg file.

What is assembly versioning in c#?

The assembly's version number, which, together with the assembly name and culture information, is part of the assembly's identity. This number is used by the runtime to enforce version policy and plays a key part in the type resolution process at run time.


2 Answers

In case anyone else comes along and needs this, add this to your csproj:

<PropertyGroup>
    <Version>1.2.3.4</Version>
    <PackageId>$(AssemblyName)</PackageId>
    <Title>My Super Library</Title>
    <AssemblyTitle>$(AssemblyName)</AssemblyTitle>
    <Company>AwesomeCo, Inc.</Company>
    <Product>My Super Library</Product>
    <Copyright>Copyright © AwesomeCo, Inc. 2016-2017</Copyright>
    <Description>There can be only one.</Description>

    <GenerateAssemblyInfo>true</GenerateAssemblyInfo>
    <GenerateAssemblyTitleAttribute>true</GenerateAssemblyTitleAttribute>
    <GenerateAssemblyConfigurationAttribute>true</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyCompanyAttribute>true</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyProductAttribute>true</GenerateAssemblyProductAttribute>
    <GenerateAssemblyCopyrightAttribute>true</GenerateAssemblyCopyrightAttribute>
    <GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute>
    <GenerateAssemblyInformationalVersionAttribute>true</GenerateAssemblyInformationalVersionAttribute>
</PropertyGroup>

Our PackageId is obviously same as our project names, but you can change it.

You can either hard-code $(Version) here in your csproj, or send it in when you publish/pack:

dotnet publish /property:Version=1.2.3.4
like image 131
Dave Thieben Avatar answered Oct 20 '22 03:10

Dave Thieben


For those who still wonder how to do this without heavily modifying your csproj files, you can pass msbuild params along with your dotnet commands, in this case, we could simply do

dotnet build /p:Version=1.2.3
dotnet publish /p:Version=1.2.3

this will set both the assembly and file version to 1.2.3. I have verified with .Net Core 2 SDK

like image 35
Duy Avatar answered Oct 20 '22 02:10

Duy