User Tools

Site Tools


notes:tools:msbuild

MSBuild

Properties are key/values pairs used to configure builds. A property value can be changed by re-defining the property.

<PropertyGroup>
  <Prop1>PropValue</Prop1>
  <Prop2 Condition=" '$(Prop2)' == '' ">NewValue</Prop2>
</PropertyGroup>

Items are inputs into the build system. 'MyItem' is the item type (or item name).

<ItemGroup>
  <MyItem Include="file1.txt" />
  <MyItem Include="file2.txt" />
</ItemGroup>

Tasks are units of executable code. The logic of a task is mapped to MSBuild with the <UsingTask> element.

<PropertyGroup>
  <Dir>MyDirectory</Dir>
</PropertyGroup>
<Target Name="MyTargetCreateDir">
  <MakeDir Directories="$(Dir)" />
</Target>

Targets group tasks together in a particular order and expose sections of a proj file as entry points.

<ItemGroup>
  <File>File1.cs</File>
  <File>File2.cs</File>
  <File>File3.cs</File>
</ItemGroup>
<Target Name="MyTargetCompile">
  <Csc Sources="@(File)" />
</Target>

Creating properties - Properties outside of Target are assigned during the evaluation phase of a build. During the execution phase, properties can be created or modified as follows:

  • Emit a property using any Task: <Task><Output PropertyName=“…” … /></Task>
  • Embed a PropertyGroup in a Target: <Target><PropertyGroup>…</PropertyGroup></Target>
  • (depreciated) Use the CreateProperty task.
Element Reference Remarks
Property $(PropName) - used to pass values to tasks
- used to evaluate conditions
- used to store values in a proj file
Environment
Variable
$(Path) or $(PATH) - property names are not case sensitive
- values of environment variables are set at the beginning of a build and are not modified
Items @(ItemType) - a parameter for a task
- the task uses individual items
Case-sensitive Not case-sensitive
- Element Name
- Attribute Name
- Property Name
- Item Type
- Metadata Name

Example: .NET Core app:

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

Example: Include files and references:

<ItemGroup>
  <Compile Include="**\*.cs" />
  <EmbeddedResources Include="**\*.resx />
</ItemGroup>
 
<ItemGroup>
  <PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
</ItemGroup>

Example: Default property values:

<PropertyGroup>
  <RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
  <RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
  <NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
  <NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
  <NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">4.7.0</NuGetToolVersion>
</PropertyGroup>

Example: Import another MSBuild script if it exists:

<ImportGroup>
  <Import Project="C:\MyProject\Script.props" Condition="Exists('C:\MyProject\Script.props')" />
</ImportGroup>

Example: Store XML in a property:

<PropertyGroup>
  <MyProp>
    ... XML goes here; it may contain other $props
  </MyProp>
</PropertyGroup>
notes/tools/msbuild.txt · Last modified: 2018/09/12 by leszek