Skip to content

Instantly share code, notes, and snippets.

@waf
Created April 29, 2022 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waf/e9ffe73a0e16d0c8fd1f3bf2587b065e to your computer and use it in GitHub Desktop.
Save waf/e9ffe73a0e16d0c8fd1f3bf2587b065e to your computer and use it in GitHub Desktop.

MSBuild

  • MSBuild is a build system.
  • Given files and commands, it tries to do the "minimal amount of work"
  • You can execute csproj files by running msbuild (from your Developer terminal)

Three main parts to a csproj

  • PropertyGroup - Defines variables
  • ItemGroup - Defines input files
  • Target - Defines functions on the input files

Hello World

  • Writes "Hello World" to the console.
<Project>
    <Target Name="Main"> 
       <Message Text="Hello World" />  
   </Target>
</Project>  

Using a PropertyGroup to define variables

  • Uses a variable to write "Hello World" to the console.
<Project>
    <PropertyGroup>
        <MessageToPrint>Hello World</MessageToPrint>
    </PropertyGroup>
    <Target Name="Main"> 
       <Message Text="$(MessageToPrint)" />  
   </Target>
</Project>  

Using an ItemGroup to define input files

  • Copies files, but only if they've changed!
<Project>
    <ItemGroup>
        <MyFile Include="*.txt"></MyFile>
    </ItemGroup>
    <Target Name="Main"> 
       <Copy SourceFiles="@(MyFile)" DestinationFolder="Output" />
    </Target>
</Project>

Using conditions for control flow

  • Same as above, but optionally delete the target directory before copying.
<Project>
    <ItemGroup>
        <MyFile Include="*.txt"></MyFile>
    </ItemGroup>
    <Target Name="Main"> 
        <RemoveDir Condition="'$(Purge)' == 'true'" Directories="Output" />
        <Copy SourceFiles="@(MyFile)" DestinationFolder="Output" />
    </Target>
</Project>

Complex MSBuild files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment