Thursday, October 29, 2009

Use Items in MsBuild

Following methods are applicable to Team Foundation Build 2008 (MSBuild 3.5).

Define Items in an Item Definition Group

<ItemGroup>
   <Environment Include="Dev">
      <Server>DevServer</Server>
   </Environment>
   <Environment Include="Test">
      <Server>TestServer</Server>
   </Environment>
   <Environment Include="Prod">
      <Server>ProdServer</Server>
   </Environment>
</ItemGroup>

Dynamically Create Items by CreateItem Task with Wildcard

<CreateProperty Value="$(DropLocation)\$(BuildNumber) \x86\Release" >
      <Output TaskParameter="Value" PropertyName="Release" />
</CreateProperty>
<CreateItem Include="$(Release)\_PublishedWebsites\Website1\**\*.*">
      <Output TaskParameter="Include" ItemName="Website1Files"/>
</CreateItem>

Create items with additional metadata:

<CreateItem Include="$(Release)\_PublishedWebsites\Website1\**\*.*" AdditionalMetadata="ToDir=$(Release)\Environments\%(Environment.Identity)">
      <Output TaskParameter="Include" ItemName="ItemsWithToDir"/>
</CreateItem>

Manipulate Items

Copy a all files of a Web application to a folder:

<Copy SourceFiles="@(Website1Files)" DestinationFiles="@(Website1Files->'$(Release)\Website1Copy\%(RecursiveDir)%(Filename)%(Extension)')"/>

Copy a files of a Web application to folders for Dev, Test & Prod staging environments:

<Copy SourceFiles="@(ItemsWithToDir)" DestinationFiles="@(ItemsWithToDir->'%(ToDir)\%(RecursiveDir)%(Filename)%(Extension)')"/>

Update configuration for environments (FileUpdate is one of the MsBuild Community Tasks):

<FileUpdate Files="$(Release)\Environments\%(Environment.Identity)\web.config" Regex="myserver" ReplacementText="%(Server)" IgnoreCase="True" />

"myserver" will be replaced by "DevServer" in Dev\wb.config; and by "TestServer" in Test\web.config, etc.

See Also

No comments:

Post a Comment