| MSBuild-3 :👈 | 👉:MSBuild-5 |
Day 4 – SDK-Style Projects and Directory.Build Files |
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.
1. What Are SDK-Style Projects?
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.
Example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
This replaces the old verbose .csproj files.
2. What the SDK Imports Automatically
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.
3. How to View the Full Expanded Project
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.
4. Directory.Build.props
This file is automatically imported before the project file. It is used to set default properties for all projects in a folder or solution.
Example: Directory.Build.props
<Project>
<PropertyGroup>
<Company>Fux Logistics</Company>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
Useful for:
- Setting company-wide properties
- Default build settings
- Defining shared constants
5. Directory.Build.targets
This file is imported after the project file and is intended for build behavior, such as adding targets.
Example: Directory.Build.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.
6. Summary of Import Order
Directory.Build.props
↓
Project.csproj
↓
Directory.Build.targets
↓
SDK .targets files
This explains why:
- Props = good for setting defaults
- Targets = good for injecting behavior
7. Multi-Targeting
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
8. How Item Includes Work in SDK Projects
SDK-style projects use implicit item includes:
*.csis included automatically*.resxincludedappsettings.jsonincluded
To disable implicit includes:
<PropertyGroup>
<EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>
9. Hands-On Exercise (10 minutes)
| MSBuild-3 :👈 | 👉:MSBuild-5 |