| MSBuild-Index :👈 | 👉:MSBuild-2 |
Day 1 – MSBuild 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.
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.
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<PropertyGroup> <Configuration>Debug</Configuration> <OutputPath>bin\Debug\</OutputPath> </PropertyGroup>
Use with $(Configuration), $(OutputPath). Examples of built‑in: $(MSBuildProjectName), $(TargetFramework), $(Configuration).
<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).
<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).
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.
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)dotnet new console -o MSBuildDemo && cd MSBuildDemoMSBuildDemo.csproj and add:
<PropertyGroup>
<MyName>Shivshanker</MyName>
</PropertyGroup>
<Target Name="PrintName" AfterTargets="Build">
<Message Text="Hello $(MyName)! Build completed." Importance="High" />
</Target>
dotnet build. You should see the message after build.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>
dotnet build -v:diagdotnet msbuild -t:Build -v:mdotnet msbuild -pp:out.xmlDirectory.Build.props load before the project file?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 |