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

  1. What is MSBuild?
  2. Project File Structure
  3. Core Concepts: Properties, Items, Targets, Tasks
  4. Evaluation Order
  5. Commands You’ll Use
  6. Hands‑On Exercise (10–15 minutes)
  7. Bonus: Conditions & Before/AfterTargets
  8. Bonus: Diagnostics & Logs
  9. Quick Quiz
  10. 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 metadata
  • Target – 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.

MSBuild Evaluation and Execution Flow Flow from Directory.Build.props to Project to Directory.Build.targets to property/item evaluation and finally target execution. Directory.Build.props Project (.csproj) Directory.Build.targets Imports & Sdk Evaluate Properties & Items Execute Targets (Tasks)

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 project
  • cd MSBuildDemo
  • dotnet build – build default targets
  • dotnet build -t:ShowMessage – run a custom target
  • dotnet msbuild -pp:out.xml – save preprocessed project (after imports)

6) Hands‑On Exercise (10–15 minutes)

  1. Create project: dotnet new console -o MSBuildDemo && cd MSBuildDemo
  2. Edit MSBuildDemo.csproj and add:
    <PropertyGroup>
      <MyName>Shivshanker</MyName>
    </PropertyGroup>
    
    <Target Name="PrintName" AfterTargets="Build">
      <Message Text="Hello $(MyName)! Build completed." Importance="High" />
    </Target>
          
  3. 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>
  
Sample Target Dependency Graph Build depends on Restore and Compile; Compile depends on ResolveReferences. Restore ResolveReferences Compile Build

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

  1. What’s the difference between a Property and an Item?
  2. Why does Directory.Build.props load before the project file?
  3. 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.

Back to Index
MSBuild-Index :👈 👉:MSBuild-2
*