Wednesday, November 01, 2006

Customising Web Deployments with MSBuild

Managing web site build configurations has been dramatically simplified with the introduction of the Web Deployment Project. When a Web Deployment Project is used in conjunction with a web site which of course has no project file, the App_Code directory is easily excluded and a clean build directory results. Web Application Projects on the other hand, have a host of additional files that aren't necessary for deployment and by default, are not removed by the Web Deployment Project. This prompted an investigation of the MSBuild script, automatically generated by the Web Deployment Project UI. Luckily, Wayne Brantley has posted some great tips on customising web deployments with MSBuild.

To view the MSBuild script, right click on the Web Deployment Project in Visual Studio, and select Open Project File.

To remove files and folders not required once deployed, add the elements below to the ItemGroup section.

<ItemGroup>
<!--
Exclude folders
-->
<
ExcludeFromBuild Include="$(SourceWebPhysicalPath)\helpers\**\*.*"
/>
<ExcludeFromBuild Include="$(SourceWebPhysicalPath)\obj\**\*.*" />
<
ExcludeFromBuild Include="$(SourceWebPhysicalPath)\proxies\**\*.*"
/>
<
ExcludeFromBuild Include="$(SourceWebPhysicalPath)\properties\**\*.*"
/>
<!--
Exclude file types
-->
<
ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.csproj"
/>
<
ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.scc"
/>
<
ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.user"
/>
<
ExcludeFromBuild Include="$(SourceWebPhysicalPath)\**\*.vspscc"
/>
</
ItemGroup>

It's also possible to deploy specific files depending on the build configuration. For instance, to replace the web.config file when building in release configuration, create a file named web.config.release and add the elements below to the MSBuild script. This works for any file type in the root directory named *.release, not just config files.

<ItemGroup>
<MySourceConfigFiles Include="$(SourceWebPhysicalPath)\*.$(Configuration)"
/>
<
MyUnNeededDestFiles Include="$(SourceWebPhysicalPath)\*.release"
/>
</
ItemGroup
>
...
...
<Target Name="AfterBuild"
>
<
Copy SourceFiles="@(MySourceConfigFiles)" DestinationFiles="@(MySourceConfigFiles->'$(OutputPath)\%(Filename)')"
/>
<
Delete Files="@(MyUnNeededDestFiles->'$(OutputPath)\%(Filename)%(Extension)')"
/>
</
Target>

No comments: