| MSBuild-Index :👈 | 👉:MSBuild-2 |
Day 1 – MSBuild Foundations |
MSBuild Day 1 – Foundations
Today you’ll learn what MSBuild is, how SDK‑style project files are structured, and the core building blocks: Properties, Items, Targets, and Tasks. You’ll finish with a working custom target.
Table of Contents
- What is MSBuild?
- Project File Structure
- Core Concepts: Properties, Items, Targets, Tasks
- Evaluation Order
- Commands You’ll Use
- Hands‑On Exercise (10–15 minutes)
- Bonus: Conditions & Before/AfterTargets
- Bonus: Diagnostics & Logs
- Quick Quiz
- What’s Next (Day 2 Preview)
1) What is MSBuild?
MSBuild (Microsoft Build Engine) is the build system for .NET. Visual Studio, dotnet build, and many CI systems all drive MSBuild to compile code, resolve dependencies, and produce outputs (DLLs, EXEs, packages, apps).
MSBuild files are XML: .csproj, .props, and .targets are all MSBuild.
2) Project File Structure
A minimal SDK‑style .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
Common top‑level elements:
PropertyGroup– variables (single values)ItemGroup– collections (files/resources) with metadataTarget– build steps (named units of work)Task– commands executed inside targets
3) Core Concepts
Properties
<PropertyGroup>
<Configuration>Debug</Configuration>
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>
Use with $(Configuration), $(OutputPath). Examples of built‑in: $(MSBuildProjectName), $(TargetFramework), $(Configuration).
Items & Metadata
<ItemGroup>
<None Include="appsettings.json" />
<Images Include="images\*.png">
<Quality>High</Quality>
</Images>
</ItemGroup>
Access items via @(Images). Access metadata inside tasks with %(Images.Quality).
Targets & Tasks
<Target Name="ShowMessage">
<Message Text="Building project..." Importance="High" />
</Target>
Run with dotnet build -t:ShowMessage. Targets can depend on other targets and contain many tasks (e.g., Copy, Exec, MakeDir, Delete).
4) Evaluation Order
High‑level order: props → project → targets → evaluate → execute targets. The diagram shows how MSBuild flows from configuration to execution.
Tip: Directory.Build.props is imported before your project file; Directory.Build.targets is imported after. This is why props are great for default values and targets for behavior.
5) Commands You’ll Use
dotnet new console -o MSBuildDemo– create a sample projectcd MSBuildDemodotnet build– build default targetsdotnet build -t:ShowMessage– run a custom targetdotnet msbuild -pp:out.xml– save preprocessed project (after imports)
6) Hands‑On Exercise (10–15 minutes)
- Create project:
dotnet new console -o MSBuildDemo && cd MSBuildDemo -
Edit
MSBuildDemo.csprojand add:<PropertyGroup> <MyName>Shivshanker</MyName> </PropertyGroup> <Target Name="PrintName" AfterTargets="Build"> <Message Text="Hello $(MyName)! Build completed." Importance="High" /> </Target> - Run:
dotnet build. You should see the message after build.
7) Bonus: Conditions & Before/AfterTargets
Conditions let you switch values by configuration or environment.
<PropertyGroup>
<OutputPath Condition=" '$(Configuration)' == 'Debug' ">bin\Debug\</OutputPath>
<OutputPath Condition=" '$(Configuration)' == 'Release' ">bin\Release\</OutputPath>
</PropertyGroup>
<Target Name="Stamp" BeforeTargets="Build">
<Message Text="Stamping version before Build..." />
</Target>
<Target Name="Notify" AfterTargets="Build">
<Message Text="Build finished!" />
</Target>
8) Bonus: Diagnostics & Logs
- Increase verbosity:
dotnet build -v:diag - Preview which targets will run:
dotnet msbuild -t:Build -v:m - Save preprocessed (all imports expanded):
dotnet msbuild -pp:out.xml
9) Quick Quiz
- What’s the difference between a Property and an Item?
- Why does
Directory.Build.propsload before the project file? - How do you run a custom target from the command line?
10) What’s Next (Day 2 Preview)
We’ll dive into property evaluation rules, item transforms, batching, and metadata. You’ll learn how to iterate files and generate outputs dynamically.
Tip: Keep this file open as a reference while you experiment in your project.
| MSBuild-Index :👈 | 👉:MSBuild-2 |