web.config file transform from command line
Using Build transform with Jenkins, I also see that web.config does not transform, however, the actual transformation takes place when you do the deploy. I use an all in one msbuild command to do the build and deploy together.
MSBuild MyProj.csproj /P:Configuration=Release /P:DeployOnBuild=True /P:DeployTarget=MsDeployPublish /P:MsDeployServiceUrl=https://your server/msdeploy.axd /P:AllowUntrustedCertificate=True /P:MSDeployPublishMethod=WMSvc /P:CreatePackageOnPublish=True /P:UserName=username /P:Password=password1 /P:DeployIISAppPath="Default Web Site or name of your website"
Once it has run you can verify on the server that transformation takes place.
If you add the following xml to the bottom of the .csproj file for your web application, you'll ensure that the config transformation occurs before every build:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild">
<TransformXml Source="Web.Base.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>
Edit: In response to your comment, you should be able to use Web.config as the source parameter in the TransformXml task (see step #2). If you only want to perform the config transform in the build script, follow these instructions:
1) Import WebApplication.targets in your build script like so:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
2) Execute the TransformXml build task in your build script target:
<Target Name="MyBuildScriptTarget">
<TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
...other build tasks...
</Target>
Tiny improvement over Jonathan's answer:
Using this line below for importing the web targets will allow to be compatible with any Visual Studio version. Note this is not tied to version v10.0
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
Jonathan's answer is good. I tweaked it slightly to allow the original Web.config file to be retained. These are the lines I added to the bottom of my .csproj file:
<!-- the step to copy the config file first avoids a 'File is being used by another process' error -->
<Target Name="BeforeBuild">
<Copy SourceFiles="Web.config" DestinationFiles="Web.temp.config" OverwriteReadOnlyFiles="True" />
<TransformXml Source="Web.temp.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>
<Target Name="AfterBuild">
<Copy SourceFiles="Web.temp.config" DestinationFiles="Web.config" OverwriteReadOnlyFiles="True" />
<Delete Files="Web.temp.config" />
</Target>
I can see that the web.config file is transformed by running a build within Visual Studio (or from a command line).