This post sets out a way to enable TFS to copy and group applications files as well as web project files.
I have a Visual Studio solution with multiple projects. In particular I have a website, a web service (WCF) and a console application. I have TFS running onsite and a build definition associated with my solution. On check in, units tests are run and the build produces build output into a defined drop folder. TFS handily packages the website and web service into their own folders, living inside the _PublishedWebsites folder. The output of the console app is just dumped into the root of the drop folder. Ideally, we want it to be in its own folder and ideally in a folder like _PublishedApps. From here it can easily be picked up by our continuous delivery tooling to be deployed.
There are a couple of solutions out there on the web, lurking inside stackoverflow answers and suggesting themselves in other peoples’ blogs. None answered it as clearly as I want and I’ve since lost the links to them, so this post is here to help me remember how to do it.
I suspect there may be other ways of solving this problem. Please do let me know yours in the comments section.The crux of this solution lies in understanding how your web project files get copied to the PublishedWebsites folder in the first place.
The answer lies in the webproject .csproj file. Near the bottom of the file there is a line:
<Import Project=”$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets” Condition=”false” />
It turns out that WebApplication.targets file (comes with Visual Studio install I think) does all the fun stuff.
Find that file on your pc, on mine it’s here:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications
At this point I confess my knowledge about msbuild scripts comes largely from google. I understand that it’s copying files needed for the website to a directory; this directory is defined near the top:
In order for our console application to copy its files to its own output folder, I wanted a .targets file for applications, not web sites.
There isn’t one out of the box, like there is for websites. So we need to create it. Fortunately there’s a shortcut; I call it Ctrl C, Ctrl V. seriously… make a copy of the WebApplication.targets file. Place it inside your solution file structure and name it something different, like Application.targets, for instance.
Next make two amends:
In the file find, <WebProjectOutputDir Condition=”!$(WebProjectOutputDirInsideProject)”>$(OutDir)_PublishedWebsites\$(MSBuildProjectName)</WebProjectOutputDir>
Change the name of the output folder, to _PublishedApps, for instance:
Find <WebProjectOutputDir Condition=”!$(WebProjectOutputDirInsideProject)”>$(OutDir)_PublishedApps\$(MSBuildProjectName)</WebProjectOutputDir>.
Inside <Target Name=”_CopyWebApplication”>, add: <Copy SourceFiles=”$(OutDir)$(TargetFileName).config” DestinationFolder=”$(WebProjectOutputDir)\bin” SkipUnchangedFiles=”true” />
The first is just a change to the directory name where files should be output too.
The second is a change to help it copy and rename the app.config file.
The final part of the puzzle is to amend your console app project to use the new .targets file.
Open the project file and immediately before the <target name=”beforebuild”> section add:
<Import Project=”..\build\Application.targets” />
Getting the relative path to your targets file correct. Save it all, check it all in (inc. the .targets file) and you should get a new output folder in your drop location, specifically for your console application’s files.
Written in August 2015, using TFS 2013 and Visual Studio 2013.
Image credit: Flood G