User Tools

Site Tools


notes:tools:msbuild

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
notes:tools:msbuild [2018/09/12]
leszek
notes:tools:msbuild [2018/11/29] (current)
leszek
Line 1: Line 1:
 ====== MSBuild ====== ====== MSBuild ======
 +
 +<code text>
 +msbuild file.csproj /​t:​TargetA ​      # execute TargetA in file.csproj
 +msbuild file.sln ​                    # build all projects in sln
 +msbuild file.csproj /​v:​diag ​         # specify verbosity
 +</​code>​
  
 **Properties** are key/values pairs used to configure builds. A property value can be changed by re-defining the property. **Properties** are key/values pairs used to configure builds. A property value can be changed by re-defining the property.
Line 56: Line 62:
 ^  Case-sensitive ​ ^  Not case-sensitive ​ ^ ^  Case-sensitive ​ ^  Not case-sensitive ​ ^
 | - Element Name\\ - Attribute Name | - Property Name\\ - Item Type\\ - Metadata Name | | - Element Name\\ - Attribute Name | - Property Name\\ - Item Type\\ - Metadata Name |
 +
 +Example: Specify a DefaultTarget:​
 +<code xml>
 +<Project DefaultTargets="​MainTarget"​ Sdk="​Microsoft.NET.Sdk">​
 +  <Target Name="​MainTarget">​
 +    ...
 +  </​Target>​
 +</​Project>​
 +</​code>​
  
 Example: .NET Core app: Example: .NET Core app:
Line 102: Line 117:
   </​MyProp>​   </​MyProp>​
 </​PropertyGroup>​ </​PropertyGroup>​
 +</​code>​
 +
 +Example: Use a condition comparing to a bool value '​DoSomething'​ and another condition comparing to a string value '​SayHello':​
 +<code xml>
 +<Project DefaultTargets="​MainTarget"​ Sdk="​Microsoft.NET.Sdk">​
 +    ​
 +    <​PropertyGroup>​
 +        <​OutputType>​Exe</​OutputType>​
 +        <​TargetFramework>​netcoreapp2.0</​TargetFramework>​
 +    </​PropertyGroup>​
 +
 +    <​PropertyGroup>​
 +        <​DoSomething>​true</​DoSomething>​
 +        <​SayHello>​hello</​SayHello>​
 +    </​PropertyGroup>​
 +
 +    <Target Name="​MainTarget">​
 +        <​CallTarget Targets="​TestBool;​TestStr"​ />
 +    </​Target>​
 +
 +    <Target Name="​TestBool"​ Condition="​$(DoSomething)">​
 +        <Message Text="​Something"​ />
 +    </​Target>​
 +    <Target Name="​TestStr"​ Condition="​$(SayHello) == '​hello'">​
 +        <Message Text="​Hello"​ />
 +    </​Target>​
 +
 +</​Project>​
 +</​code>​
 +
 +Example: Display the names and the ModifiedTime attribute of files located in a specified folder. In this example, we assume there are three files d1.dat, d2.dat, and d3.dat in a subfolder '​data':​
 +<code xml>
 +<Project DefaultTargets="​ListDataFiles"​ Sdk="​Microsoft.NET.Sdk">​
 +    ​
 +    <​PropertyGroup>​
 +        <​OutputType>​Exe</​OutputType>​
 +        <​TargetFramework>​netcoreapp2.0</​TargetFramework>​
 +        <​DataPath>​$(MSBuildProjectDirectory)\data\*.dat</​DataPath>​
 +    </​PropertyGroup>​
 +
 +    <​ItemGroup>​
 +        <​DataFiles Include="​$(DataPath)"​ />
 +    </​ItemGroup>​
 +
 +    <Target Name="​ListDataFiles">​
 +        <Message Text="​@(DataFiles)"​ />
 +        <Message Text="​@(DataFiles->'​%(ModifiedTime)'​)"​ />
 +    </​Target>​
 +
 +</​Project>​
 +</​code>​
 +
 +Output:
 +<code text>
 +Project "​C:​\Temp\test.csproj"​ on node 1 (default targets).
 +ListDataFiles:​
 +  C:​\Temp\data\d1.dat;​C:​\Temp\data\d2.dat;​C:​\Temp\data\d3.dat
 +  2018-08-07 13:​43:​00.6470153;​2018-08-07 13:​43:​00.6470153;​2018-08-07 13:​43:​00.6470153
 +</​code>​
 +
 +Example: Display values of an item's custom property '​Description':​
 +<code xml>
 +<Project DefaultTargets="​ListDescriptions"​ Sdk="​Microsoft.NET.Sdk">​
 +    ​
 +    <​PropertyGroup>​
 +        <​OutputType>​Exe</​OutputType>​
 +        <​TargetFramework>​netcoreapp2.0</​TargetFramework>​
 +    </​PropertyGroup>​
 +
 +    <​ItemGroup>​
 +        <Mammals Include="​Echidna">​
 +            <​Description>​Echidna description</​Description>​
 +        </​Mammals>​
 +        <Mammals Include="​Pangolin">​
 +            <​Description>​Pangolin description</​Description>​
 +        </​Mammals>​
 +        <Mammals Include="​Porpoise">​
 +            <​Description>​Porpoise description</​Description>​
 +        </​Mammals>​
 +    </​ItemGroup>​
 +    ​
 +    <Target Name="​ListDescriptions">​
 +        <Message Text="​@(Mammals)"​ />
 +        <Message Text="​@(Mammals->'​%(Description)'​)"​ />
 +    </​Target>​
 +
 +</​Project>​
 +</​code>​
 +
 +Output:
 +<code text>
 +Project "​C:​\Temp\test.csproj"​ on node 1 (default targets).
 +ListDescriptions:​
 +  Echidna;​Pangolin;​Porpoise
 +  Echidna description;​Pangolin description;​Porpoise description
 +</​code>​
 +
 +Example: Use AfterTargets to enforce the order of targets'​ execution:
 +<code xml>
 +<Project DefaultTargets="​MainTarget"​ Sdk="​Microsoft.NET.Sdk">​
 +    ​
 +    <​PropertyGroup>​
 +        <​OutputType>​Exe</​OutputType>​
 +        <​TargetFramework>​netcoreapp2.0</​TargetFramework>​
 +    </​PropertyGroup>​
 +
 +    <Target Name="​MainTarget">​
 +        <​CallTarget Targets="​TargetZ"​ />
 +    </​Target>​
 +
 +    <Target Name="​TargetX"​ AfterTargets="​TargetZ">​
 +        <Message Text="​X"​ />
 +    </​Target>​
 +    <Target Name="​TargetY"​ AfterTargets="​TargetZ">​
 +        <Message Text="​Y"​ />
 +    </​Target>​
 +    <Target Name="​TargetZ">​
 +        <Message Text="​Z"​ />
 +    </​Target>​
 +
 +</​Project>​
 +</​code>​
 +
 +Output:
 +<code text>
 +Project "​C:​\Temp\test.csproj"​ on node 1 (default targets).
 +TargetZ:
 +  Z
 +TargetX:
 +  X
 +TargetY:
 +  Y
 +</​code>​
 +
 +Example: Use DependsOnTargets to enforce the order of targets'​ execution. Note that C is executed only once:
 +<code xml>
 +<Project DefaultTargets="​MainTarget"​ Sdk="​Microsoft.NET.Sdk">​
 +    ​
 +    <​PropertyGroup>​
 +        <​OutputType>​Exe</​OutputType>​
 +        <​TargetFramework>​netcoreapp2.0</​TargetFramework>​
 +    </​PropertyGroup>​
 +
 +    <Target Name="​MainTarget">​
 +        <​CallTarget Targets="​TargetA"​ />
 +    </​Target>​
 +
 +    <Target Name="​TargetA"​ DependsOnTargets="​TargetC">​
 +        <Message Text="​A"​ />
 +        <​CallTarget Targets="​TargetB"​ />
 +    </​Target>​
 +    <Target Name="​TargetB"​ DependsOnTargets="​TargetC">​
 +        <Message Text="​B"​ />
 +    </​Target>​
 +    <Target Name="​TargetC">​
 +        <Message Text="​C"​ />
 +    </​Target>​
 +
 +</​Project>​
 +</​code>​
 +
 +Output:
 +<code text>
 +Project "​C:​\Temp\test.csproj"​ on node 1 (default targets).
 +TargetC:
 +  C
 +TargetA:
 +  A
 +TargetB:
 +  B
 +</​code>​
 +
 +Example: Include targets and properties from separate *.props files:
 +<code xml>
 +<Project DefaultTargets="​MainTarget"​ Sdk="​Microsoft.NET.Sdk">​
 +    ​
 +    <Import Project="​Common.targets"​ />
 +    <Import Project="​Common.props"​ />
 +
 +    <Target Name="​MainTarget"​ DependsOnTargets="​TargetA">​
 +        <Message Text="​Hello,​ $(FullName)"​ />
 +    </​Target>​
 +
 +</​Project>​
 +</​code>​
 +
 +<code xml>
 +<​Project>​
 +    <!-- Shared targets -->
 +    <Target Name="​TargetA">​
 +        <Message Text="​A"​ />
 +    </​Target>​
 +
 +    <Target Name="​TargetB">​
 +        <Message Text="​B"​ />
 +    </​Target>​
 +
 +    <Target Name="​TargetC">​
 +        <Message Text="​C"​ />
 +    </​Target>​
 +</​Project>​
 +</​code>​
 +
 +<code xml>
 +<​Project>​
 +    <!-- Shared properties -->
 +    <​PropertyGroup>​
 +        <​OutputType>​Exe</​OutputType>​
 +        <​TargetFramework>​netcoreapp2.0</​TargetFramework>​
 +            ​
 +        <​First>​Leon</​First>​
 +        <​Last>​McPowell</​Last>​
 +        <​FullName>​$(First) $(Last)</​FullName>​
 +    </​PropertyGroup>​
 +</​Project>​
 +</​code>​
 +
 +Output:
 +<code text>
 +Project "​C:​\Temp\test.csproj"​ on node 1 (default targets).
 +TargetA:
 +  A
 +MainTarget:
 +  Hello, Leon McPowell
 +</​code>​
 +
 +Example: Compress files using the MSBuild.ExtensionPack.Compression.DNZip task from MSBuild.Extension.Pack. You need to download the extension pack files (dlls) first and point MSBuild to their location (here: a '​tools'​ subfolder):
 +<code xml>
 +<Project DefaultTargets="​ZipFilesTarget"​ Sdk="​Microsoft.NET.Sdk">​
 +
 +    <​PropertyGroup>​
 +        <​OutputType>​Exe</​OutputType>​
 +        <​TargetFramework>​netcoreapp2.0</​TargetFramework>​
 +    </​PropertyGroup>​
 +    ​
 +    <Import Project="​MSBuild.Extension.Pack.targets"​ />
 +
 +    <Target Name="​ZipFilesTarget">​
 +        <​ItemGroup>​
 +            <​FilesToZip Include="​data\*.*"​ />
 +        </​ItemGroup>​
 +
 +        <​MSBuild.ExtensionPack.Compression.DNZip
 +            TaskAction="​Create"​
 +            CompressFiles="​@(FilesToZip)"​
 +            ZipFileName="​files.zip"​ />
 +    </​Target>​
 +
 +</​Project>​
 +</​code>​
 +
 +The MSBuild.Extension.Pack.targets file generated automatically:​
 +<code xml>
 +<?xml version="​1.0"​ encoding="​utf-8"?>​
 +<Project ToolsVersion="​4.0"​ xmlns="​http://​schemas.microsoft.com/​developer/​msbuild/​2003">​
 +    <​PropertyGroup>​
 +        <​ExtensionTasksPath>​tools\</​ExtensionTasksPath>​
 +    </​PropertyGroup>​
 +    ...
 +    <!-- ============== Framework ============== -->
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.CodeQuality.FxCop"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.CodeQuality.NUnit"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.CodeQuality.NUnit3"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Communication.Email"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Communication.Ftp"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Communication.MSMQ"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Compression.Cab"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Compression.DNZip"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Compression.Zip"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Computer.ActiveDirectory"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Computer.ComponentServices"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Computer.EnvironmentVariable"/>​
 +    <​UsingTask AssemblyFile="​$(ExtensionTasksPath)MSBuild.ExtensionPack.dll"​ TaskName="​MSBuild.ExtensionPack.Computer.EventLog"/>​
 +    ...
 +</​Project>​
 +</​code>​
 +
 +Output:
 +<code text>
 +Project "​C:​\Temp\test.csproj"​ on node 1 (default targets).
 +ZipFilesTarget:​
 +  Creating ZipFile: files.zip
 +</​code>​
 +
 +Example: Remove an output folder and build a VS solution:
 +<code xml>
 +<Project DefaultTargets="​BuildSolution"​ Sdk="​Microsoft.NET.Sdk">​
 +    <​PropertyGroup>​
 +        <​OutputType>​Exe</​OutputType>​
 +        <​TargetFramework>​netcoreapp2.0</​TargetFramework>​
 +        <​OutputPath>​$(MSBuildThisFileDirectory)Output</​OutputPath>​
 +        <​SolutionFileName>​C:​\Projects\DoSomething.sln</​SolutionFileName>​
 +    </​PropertyGroup>​
 +
 +    <Target Name="​CleanDir">​
 +        <​RemoveDir Directories="​$(OutputPath)"​ Condition="​Exists($(OutputPath))"​ />
 +    </​Target>​
 +
 +    <Target Name="​BuildSolution"​ DependsOnTargets="​CleanDir">​
 +        <MSBuild Projects="​$(SolutionFileName)"​
 +                 ​Properties="​Configuration=Release;​OutputPath=$(OutputPath)"​ />
 +    </​Target>​
 +</​Project>​
 </​code>​ </​code>​
  
notes/tools/msbuild.1536798052.txt.gz ยท Last modified: 2018/09/12 by leszek