| MSBuild-3 :👈 | 👉:MSBuild-5 |
Day 4 – SDK-Style Projects and Directory.Build Files |
Today you will learn how modern .NET SDK-style projects work, what files are automatically imported, how Directory.Build.props and Directory.Build.targets affect the build, and how multi-targeting works. This is essential for understanding the true MSBuild pipeline in .NET.
SDK-style projects were introduced with .NET Core and now used for all .NET (6/7/8) and MAUI. They simplify .csproj files by including many defaults automatically.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
This replaces the old verbose .csproj files.
When you write:
<Project Sdk="Microsoft.NET.Sdk" />
It automatically imports:
Microsoft.NET.Sdk.props (at the top)Microsoft.NET.Sdk.targets (at the bottom)These define the standard build pipeline including Restore, Build, Compile, Publish, Pack, Clean, etc.
Use this command to see the entire file with all imports expanded:
dotnet msbuild -pp:full.xml
Open full.xml and you will see hundreds of lines of MSBuild logic — all included automatically.
This file is automatically imported before the project file. It is used to set default properties for all projects in a folder or solution.
<Project>
<PropertyGroup>
<Company>Fux Logistics</Company>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
Useful for:
This file is imported after the project file and is intended for build behavior, such as adding targets.
<Project>
<Target Name="AfterBuildMessage" AfterTargets="Build">
<Message Text="A project has finished building." Importance="High" />
</Target>
</Project>
This will run for every project in the solution.
Directory.Build.props ↓ Project.csproj ↓ Directory.Build.targets ↓ SDK .targets files
This explains why:
MSBuild supports building for multiple frameworks in one project.
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
</PropertyGroup>
Use the plural TargetFrameworks to enable multi-target builds.
To build a specific target:
dotnet build -f net8.0
SDK-style projects use implicit item includes:
*.cs is included automatically*.resx includedappsettings.json includedTo disable implicit includes:
<PropertyGroup>
<EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>
| MSBuild-3 :👈 | 👉:MSBuild-5 |