MSBuild Notes

  • Use .rsp file to pass command line arguments to msbuild from a file (similar to the way .config files work)
  • PropertyGroup contains properties. A property is a key-value pair. The value is a scalar. Properties are referenced using the $(PropertyName) syntax
  • ItemGroup contains items. Use Items to store collections e.g., all files inside a directory. Items are referenced using the @(ItemName) syntax
  • An Item can have metadata associated with it
  • A Target contains one or more Tasks
  • There are many ways to chain Targets. E.g.,: CallTarget, DependsOnTargets, BeforeTargets, AfterTargets
  • Goto MSBuild Task Reference to see all tasks that come with MSBuild. Examples of some common tasks: Exec, Copy, Move, Message, MakeDir, RemoveDir
  • Download MSBuildExtensionPack from codeplex for more tasks
  • UsingTask allows you to import a MSBuild Task defined in a dll
  • To zip files, use DNZip task from MSBuildExtensionPack
  • Use the MSBuild task to build a csproj
  • To get the name of the dll/exe built by a msbuild task use the TargetOutputs task parameter:
    <MSBuild Projects="$(MyCsProj)" ToolsVersion="4.0" StopOnFirstFailure="true">
    			<Output TaskParameter="TargetOutputs" PropertyName="MyDll" />
    		</MSBuild>
    
  • To create you own MSBuild task, implement the ITask interface. Subclass the Task class instead of inheriting from ITask directly
  • To pass output of one task as input to another:
    <Task1 >
    			<Output TaskParameter="Version" PropertyName="Version"/>
    		</Task1>
    		<Task2 Version="$(Version)" />
    

    In the C# code for Task1:

    [Output]
            public string Version { get; private set; }
    
  • Use mstext.exe /testcontainer:myunittests.dll to run unit tests from command line
  • To change assembly version use the AssemblyInfo task in MSBuildExtension Pack
    <MSBuild.ExtensionPack.Framework.AssemblyInfo AssemblyInfoFiles="@(files)",
    AssemblyFileMajorVersion="1",
    AssemblyFileMinorVersion="2" />
    
  • More useful tasks in MSBuildExtension Pack: MSBuildExtensionPack.Web.Iis7Website, MSBuildExtensionPack.FileSystem.Robocopy
This entry was posted in Software. Bookmark the permalink.

Leave a comment