Notes

Using MSBuild Outside Of An IDE

Build Tools MSBuild .NET

MSBuild is a .NET build system. It is typically used for compilation of .NET projects in Visual Studio. It can also be used as a standard build tool outside of Visual Studio to perform other software development and deployment tasks.

Getting MSBuild Setup

Using MSBuild outside of an IDE is pretty straightforward on Windows machines. If you have .NET installed on the machine, MSBuild will already be available. You only need to ensure that your PATH contains an entry to MSBuild. The path to MSBuild will be similar to the following:

C:\Windows\Microsoft.NET\Framework\v4.0.30319

The actual version number may be different depending on the version of .NET installed on the machine.

Structure of an MSBuild Project File

MSBuild projects are defined by .csproj files. These files are xml format. The key elements of a MSBuild project file are the Project and Target tags. Below is an example of simple MSBuild project file that logs a message.

   
<?xml version="1.0" encoding="utf-8"?>

<!-- SimpleProject.csproj -->

<!-- 
    Project - Root element of MSBuild project file.
        xlmns (Required) - Attribute defines default XML namespace.
        DefaultTargets (Optional) - Attribute specifies the starting default target that should run.
-->
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <!-- 
        PropertyGroup - Within the PropertyGroup properties can be specified. These properties can 
        be any type of arbitrary data that a particular task may need to run.
            Condition (Optional) - Typically you'd specify a property that will be passed as a command 
            line property. To use retrieve a property value you would use $(PropertyName).
    -->         
    <PropertyGroup>
    <!-- Set default value for MyProperty -->
        <MyProperty>Property Value Here</MyProperty>                               
    </PropertyGroup>

    <PropertyGroup Condition="'$(Env)'=='production'">
        <!-- If Env property passed via command line. Ex: msbuild -p:Env=production -->
        <MyProperty>Override Property Value Here</MyProperty>                              
    </PropertyGroup>

    <!-- 
    Target - Child element of Project tag that can be used to encapsulate a group of tasks. All tasks 
    must be nested inside of a target.
        Name (Required) - Name of the target.
        DependsOnTargets (Optional) - Indicates targets that need to execute before.
    -->
    <Target Name="Build">

        <!-- Echo value of MyProperty  -->
        <Message Text="My Property Value: $(MyProperty)" />

        <!-- Perform Tasks Here -->

    </Target>

</Project>

Executing a Build

To execute an MSBuild project build you would simply open a command prompt in the directory and run the following:

msbuild

or if you want to set the Env property.

msbuild -p:Env=production

You could also overide a single property directly.

msbuild -p:MyProperty="Howdy Property"

Important Command Line Flags

  • -t - Specifies which targets to build. Multiple target can be separated by a semicolon or comma.
  • -p - Set or override project-level properties. Multiple properties can be separated by a semicolon or comma.

For a list of all the flags that can be passed simply run the following in a command prompt

msbuild -h

Disclaimer: The opinions expressed here are my own personal opinions and do not represent my employer's view in any way.