| MSBuild-6 :👈 | 👉:MSBuild-With-AI |
Day 7 – Real-World MSBuild Automation Scenarios |
Day 7 – Real-World MSBuild Automation Scenarios
This final lesson brings together everything learned during Days 1–6 and applies it to real-world scenarios used in modern .NET applications, MAUI apps, DevOps pipelines, and enterprise builds.
1. Automated Versioning with MSBuild
Many teams automate version numbers using MSBuild properties.
Example: SemVer auto-increment
<PropertyGroup>
<Major>1</Major>
<Minor>0</Minor>
<Patch>$([System.DateTime]::Now.DayOfYear)</Patch>
<Version>$(Major).$(Minor).$(Patch)</Version>
</PropertyGroup>
This produces versions like 1.0.123 (123 = day of year).
Example: Git-based versioning
<Target Name="GitVersion" BeforeTargets="Build">
<Exec Command="git rev-parse --short HEAD > gitversion.txt" />
<ReadLinesFromFile File="gitversion.txt">
<Output TaskParameter="Lines" PropertyName="GitHash" />
</ReadLinesFromFile>
<Message Text="Git Commit: $(GitHash)" />
</Target>
2. Environment-Based Build Configuration
You can switch logic based on environment variables or custom properties.
Example:
<PropertyGroup>
<Environment>$(BUILD_ENVIRONMENT)</Environment>
</PropertyGroup>
<Target Name="ShowEnv">
<Message Text="Building for environment: $(Environment)" />
</Target>
Call with:
dotnet build -p:BUILD_ENVIRONMENT=Production
3. MSBuild in CI/CD Pipelines
DevOps pipelines often use MSBuild targets to create artifacts, run scripts, and modify output folder structure.
Example: Creating an artifact folder
<Target Name="PackageArtifacts" AfterTargets="Publish">
<MakeDir Directories="artifacts" />
<Copy
SourceFiles="@(PublishFiles)"
DestinationFolder="artifacts" />
</Target>
Azure DevOps YAML example
- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '-t:PackageArtifacts'
4. Generating Files Before Build
MSBuild can generate files on the fly before compilation.
Generate a C# file dynamically:
<Target Name="GenerateVersionFile" BeforeTargets="Compile">
<WriteLinesToFile
File="VersionInfo.cs"
Lines="public static class VersionInfo { public const string Build = "$(Version)"; }" />
</Target>
<ItemGroup>
<Compile Include="VersionInfo.cs" />
</ItemGroup>
This file is included in the project automatically.
5. MAUI-Specific MSBuild Customization
MAUI uses MSBuild heavily for platform-specific builds. You can hook into different targets depending on platform.
Example: Add a custom step only for Android
<Target Name="AndroidPreBuild"
BeforeTargets="Build"
Condition=" '$(TargetFramework)' == 'net8.0-android' ">
<Message Text="Running Android pre-build logic..." />
</Target>
iOS Example:
<Target Name="iOSBundleStep"
AfterTargets="Build"
Condition=" '$(TargetFramework)' == 'net8.0-ios' ">
<Message Text="iOS custom packaging step..." />
</Target>
6. Running External Tools
Use Exec to run external tools like linters, bundlers, or CLI utilities.
Example:
<Target Name="CheckFormatting" BeforeTargets="Build">
<Exec Command="dotnet format --verify-no-changes" />
</Target>
If formatting issues exist, the build fails.
7. Creating Custom Output Structure
Reorganize the publish folder using post-publish logic.
<Target Name="ReorganizeOutput" AfterTargets="Publish">
<MakeDir Directories="publish\static" />
<Copy SourceFiles="wwwroot\**\*" DestinationFolder="publish\static" />
</Target>
8. Overriding Default Targets Safely
To override built-in behavior, keep the original logic and append your own.
<PropertyGroup>
<BuildDependsOn>
CustomCheck;
$(BuildDependsOn)
</BuildDependsOn>
</PropertyGroup>
<Target Name="CustomCheck">
<Message Text="Performing custom checks before Build..." />
</Target>
9. Hands-On Exercise (20 minutes)
Step 1 — Create a versioning file before build
<Target Name="GenInfo" BeforeTargets="Compile">
<WriteLinesToFile
File="BuildInfo.txt"
Lines="Build: $([System.DateTime]::Now)" />
</Target>
Step 2 — Copy to output folder after publish
<Target Name="CopyBuildInfo" AfterTargets="Publish">
<Copy
SourceFiles="BuildInfo.txt"
DestinationFolder="$(PublishDir)" />
</Target>
Step 3 — Run
dotnet publish
Check the publish folder for BuildInfo.txt.
10. Summary
- Automate versioning using timestamps or Git.
- Switch behavior using environment-based properties.
- Extend DevOps pipelines with MSBuild targets.
- Generate files dynamically before build.
- Customize MAUI builds per platform.
- Run external tools during build.
- Override and extend build pipelines safely.
- Create custom output structures and artifacts.
Final Note
You now understand the full power of MSBuild — from basics to advanced automation. You can design custom pipelines, reuse build logic, integrate with DevOps, and control platform-specific behavior.
| MSBuild-6 :👈 | 👉:MSBuild-With-AI |