Versioning .net Core applications using Cake
If you are moving to .net Core and you want to implement a versioning strategy in your application, now you have an awesome alternative to the good old fellow "AssemblyInfo".
The alternative is the Directory.Build.props file. If you place these file in the root folder that contains your source code, when MSBuild runs will add to every project the properties defined in the Directory.Build.props.
In this post, we will see how to quickly setup a solution with multiple projects and use Directory.Build.props with Cake to build and increment the version number.
Setup your project
Let's use the .net Core CLI to quickly accomplish this task.
Start by creating a folder in your computer and then open the command line and go to the directory that you just created. Then execute the following commands.
- Create a Solution:
dotnet new sln --name DotNetCoreVersioning
- Create a console application and place it in the src folder:
dotnet new console --name Application --output src\Application
- Create a class library and place it in the src folder:
dotnet new classlib --name Library --output src\Library
- Add the console application to the solution:
dotnet sln add .\src\Application\Application.csproj
- Add the class library to the solution:
dotnet sln add .\src\Library\Library.csproj
- Build it:
dotnet build
If you go to the folder you will see a solution file that you can open in Visual Studio.
Adding the Directory.Build.props
Now it's time to add the file that will do the magic.
You just need to create a file with the name Directory.Build.props inside the src folder.
Add it and then copy the following snippet into it.
<Project>
<PropertyGroup>
<Company>Empire</Company>
<Version>1.0.1</Version>
<AssemblyTitle>Death Star - Superlaser</AssemblyTitle>
</PropertyGroup>
</Project>
If you run the dotnet build
command and go to the bin folder, of the console application or the class library, you can see in the DLL details that now the version is 1.0.1.
NOTE: The casing is important if you want to use a Linux machine to run your build. Otherwise, the version may be 1.0.0.
Incrementing the version number.
In this example, I will use a simple Cake script to increment the version number.
Setup your build script
I will explain how to do that manually, but if you are a Visual Studio Code user I highly recommend that you install the cake extension and use the tools provided by the extension.
- Install the Cake bootstrapper. You can find here how to do that.
- Create a build.cake file next to the bootstrapper.
- Add the following snippet to the build.cake.
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Version")
.Does(() => {
});
Task("Build")
.IsDependentOn("Version")
.Does(() => {
DotNetCoreBuild("./DotNetCoreVersioning.sln");
});
Task("Default")
.IsDependentOn("Build");
RunTarget(target);
Your build has now two steps and is building your solution using DotNetCoreBuild.
Increment the version number
To increment the version number we need to update the Directory.Build.props file.
Since the props file is an XML we can use the Cake XML aliases like XmlPeek and XmlPoke to read and update the version number.
Note: I recommend you to use the Build Number of your build server to calculate the new version number. In this case, we will read the current version from the file content.
Go ahead and copy the following snippet into the Version task in your build.cake file.
var propsFile = "./Directory.Build.props";
var readedVersion = XmlPeek(propsFile, "//Version");
var currentVersion = new Version(readedVersion);
var semVersion = new Version(currentVersion.Major, currentVersion.Minor, currentVersion.Build + 1);
var version = semVersion.ToString();
XmlPoke(propsFile, "//Version", version);
As you can see, we are reading the version property, incrementing the build number and then updating the props file again.
The result
Now that our build script is completed, go ahead and run your Cake bootstrapper (build.ps1 or build.sh) and you will see that the compiled DLLs from all the projects have the version incremented.
You can use the Directory.Build.props to customize other project properties, not only for versioning.
Go ahead and give it a try.