Thursday, June 11, 2009

TFS Build: _PublishedWebsites for exe and dll projects

We’re using TFS on my current project. Yes, yes, I know.

It’s generally good practice to collect all the code under your team’s control in a single uber-solution as described in this Patterns and Practices PDF, Team Development with TFS Guide. If you then configure the TFS build server to build this solution, it’s default behaviour is to place the build output into a single folder, ‘Release’.

Any web application projects in your solution will also be output to a folder called _PublishedWebsites\<name of project>. This is very nice because it means that you can simply robocopy deploy the web application.

Unfortunately there’s no similar default behaviour for other project types such as WinForms, console or library. It would be very nice if we could have a _PublishedApplications\<name of project> sub folder with the output of any selected project(s). Fortunately it’s not that hard to do.

The way _PublishedWebsites works is pretty simple. If you look at the project file of your web application you’ll notice an import near the bottom:

<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />

On my machine the MSBuildExtensionsPath property evaluates to C:\Program Files\MSBuild, if we open the Microsoft.WebApplication.targets file we can see that it’s a pretty simple MSBuild file that recognises when the build is not a desktop build, i.e. it’s a TFS build, and copies the output to:


I simply copied the Micrsoft.WebApplication.targets file, put it under source control with a relative path from my project files and changed _PublishedWebsites to _PublishedApplications and renamed the file CI.exe.targets. For each project that I want to output to _PublishedApplications, I simply added this import at the bottom of the project file:

<Import Project="<your relative path>\CI.exe.targets" />

You can edit CI.exe.targets (or whatever you want to call it) to do your bidding. In my case, the only change so far is to add a couple of lines to copy the App.config file:

<Copy SourceFiles="$(OutDir)$(TargetFileName).config" DestinationFolder="$(WebProjectOutputDir)\bin" SkipUnchangedFiles="true" />

There’s a lot of stuff in Microsoft.WebApplication.targets that’s only relevant to web applications and can be stripped out for other project types, but I’ll leave that as an exercise for the reader.

There was also a discussion on StackOverflow, with some nice alternative suggestions of how you might want to do this. It’s worth checking out.


Ian Nelson said...

I'm curious Mike, what are your preferred tools for source control / work item tracking / automated builds?

Mike Hadlow said...

Hi Ian,

Given a free choice I'd probably go with something like:

Subversion for source control. Although I'd be interested in trying Git or Mercurial too.

Team City for builds. But Cruise Control is adequate.

As for work item tracking, I don't really have that strong an opinion, it depends so much on the size and type of project and the style of the business. There are loads out there, try a few out and use the one that best suits your needs.

TFS does all three of these jobs OK, but it's by no means the best of breed for any of them. However if your team is very keen on it (as my current team is), it's perfectly possible to make it work.

Ian Nelson said...

Thanks for that Mike.

I'm actually quite the TFS fanboy myself - I literally have the t-shirt from the 1.0 release and have had good experiences with it on past projects.

I think the main appeal to me is the integration between the different components - for example when viewing source control history for a file I can see the work items associated with that changeset, and the build into which the change was integrated, results of any tests carried out against that build, etc.

But I agree that TFS isn't best of breed in any area, and it's always nice to get an idea of what alternatives are available for when TFS isn't appropriate.

Team City looks interesting, I'll definitely be checking that out further.

Anonymous said...

Mike, your solution rocks.. Works great with TFS Build as well.. thanks!

Keith Bloom said...

Hi Mike,

Thanks for posting this, I just added it to one of our Console projects which is currently hard to deploy.


Anonymous said...

Awesome, very useful. TFS is confusing and documentation sparse.

Anonymous said...

this worked for me. Thanks!

Anonymous said...

I highly recommend adding a file exists conditional to that line you added. Something like

<Copy Condition="Exists('$(OutDir)$(TargetFileName).config')" SourceFiles="$(OutDir)$(TargetFileName).config" DestinationFolder="$(ServiceProjectOutputDir)" SkipUnchangedFiles="true" />

That way you can use the same targets file weather an app config exists for the particular project or not.

Michael said...

Thanks for sharing! Worked like a charm!

Anonymous said...

Hi , Nuget made this job easy. Install publishedapplication from nuget on which ever project you want to add under the publishedapplication. Commit the changes and run the build.

Luuk Sommers said...

Yesss! Thank you, so elegant and easy!