Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package version is always 1.0.0 with dotnet pack

TLDR: Where is dotnet pack pulling the version information when it creates the nuget package for an assembly?

I have a library, that I had transitioned from a .NET 4.6.1 project to a .NET Core project with project.json. For my CI during this period (using TFS 2015 vnext), I would get my version number and replace the version number in the project.json file with the new version. The dotnet pack command would pick the version up just fine, and create a new package with the updated version number.

Last week, I upgraded from TFS 2015 to TFS 2017. Turns out, project.json was replaced with an updated .csproj file. I've updated my CI. During my CI - I update my /Properties/AssemblyInfo.cs file, replacing the AssemblyVersion tag with the version for the current build. Then I build the solution - which builds just fine. Then I package the solution.

However, despite the AssemblyVersion and AssemblyFileVersion being set in AssemblyInfo.cs to the correct build number - dotnet pack is still producing .nupkg files that are *.1.0.0.nupkg.

What am I missing?

Here is my pack command:

dotnet pack $projectFile -o $currentDirectory 
like image 500
Alexander Matusiak Avatar asked Mar 14 '17 22:03

Alexander Matusiak


People also ask

How do I change NuGet package version?

Right-click the Packages folder in the project, and select Update. This will update the NuGet package to the latest version. You can double-click the Add packages and choose the specific version.

What is dotnet pack?

The dotnet pack command builds the project and creates NuGet packages. The result of this command is a NuGet package (that is, a . nupkg file).

How do I find the NuGet package version?

In Visual Studio, use the Help > About Microsoft Visual Studio command and look at the version displayed next to NuGet Package Manager. Alternatively, launch the Package Manager Console (Tools > NuGet Package Manager > Package Manager Console) and enter $host to see information about NuGet including the version.

What is the difference between the dotnet pack and dotnet publish commands?

dotnet pack : The output is a package that is meant to be reused by other projects. dotnet publish : The output is mean to be deployed / "shipped" - it is not a single "package file" but a directory with all the project's output.


Video Answer


2 Answers

Better yet, specify /p:Version=$(Build.BuildNumber) (TFS/VSTS) on the dotnet pack command and it will build it with the specified version in the nuget package. Example (non TFS specific):

dotnet pack .\src\example\example.csproj -o c:\published\example -c Release /p:Version=1.2.3 

Example (TFS specific) <- we use this for our TFS 2017 packing using a powershell script step.

dotnet pack $(Build.SourcesDirectory)\src\example\example.csproj -o $(Build.ArtifactStagingDirectory)\Pack -c Release /p:Version=$(Build.BuildNumber) 

Note: It does not update package reference versions.

like image 125
Edward Avatar answered Oct 05 '22 06:10

Edward


When you use dotnet pack, the version is pulled from the project definition (previously project.json, now *.csproj), not AssemblyInfo.cs. So, your new workflow will be very similar to what it was with project.json.

From the project.json to csproj migration docs, you can use the VersionPrefix and VersionSuffix properties.

Before:

{   "version": "1.0.0-alpha-*" } 

Now:

<PropertyGroup>   <VersionPrefix>1.0.0</VersionPrefix>   <VersionSuffix>alpha</VersionSuffix> </PropertyGroup> 

You can also use the single Version property, but the docs warn that this "may override version settings during packaging".

<PropertyGroup>   <Version>1.0.0-alpha</Version> </PropertyGroup> 
like image 30
Nate Barbettini Avatar answered Oct 05 '22 05:10

Nate Barbettini