Van
Van

Reputation: 1387

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 :)

Upvotes: 7

Views: 1891

Answers (3)

Nouman Shams
Nouman Shams

Reputation: 31

I Know this has been answered but there is a different approach that uses environment variables and don't require all the tags

Edit your project file (csproj) and include following condition based tags for version

<Version Condition=" '$(BUILD_NUMBER)' == '' ">0.0.0</Version>
<Version Condition=" '$(BUILD_NUMBER)' != '' ">$(BUILD_NUMBER)</Version>

here BUILD_NUMBER is the environment variable exposed by TeamCity. If build number is 30 then with above in place the version will be 30.0.0.0

Please note, if BUILD_NUMBER variable is not present then version number will be all zeros (0.0.0).

Hope this gives an alternative approach to above

Upvotes: 0

Van
Van

Reputation: 1387

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

Upvotes: 1

Dave Thieben
Dave Thieben

Reputation: 5437

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

Upvotes: 5

Related Questions