Sometimes you may have a project in your solution who’s sole purpose is to generate files that need to be referenced by a second project. If the files are fixed, then it’s just a matter of adding a link to them in the second project, but what about when you don’t know at build time how many files will be generated?
It turns out that MSBuild can handle this situation. For example, given the following solution:
Note that we’ve added a reference for the Generate project to the IncludeProjectOutputs project. We’ve also set ‘Copy Local’ to false as the only reason for the reference is to enforce building the projects in the right order (we don’t actually refer to the Generate assembly within the IncludeProjectOutputs code).
So that the executable output from the generator project is run, add the following to the generator project’s project file:
<Target Name="AfterBuild"> <Message Text="After build execute $(TargetPath)" Importance="high" /> <Exec Command="$(TargetPath)" /> </Target>
In the project that should be including the generated files, add the following:
<Target Name="IncludeGenerated" BeforeTargets="ResolveAssemblyReferences"> <ItemGroup> <Content Include="$(ProjectDir)..\Generate\*.txt" > <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> </Target>
In this instance, we’re including all .txt files from the ..\Generate folder. Because we define the
<Content />item, they’re added as content (equivalent to the ‘Content’ build action). I haven’t tested it, but conceivably you could also add to
The ability to add ItemGroup elements within a Target element was added in .NET 3.5. The advantage of this is that the filenames matching the pattern of the Include attribute are evaluated in the execution phase. This contrasts with normal ItemGroup elements, which are assigned values during the evaluation phase of the build. See MSBuild Items for more details.
Building this project gives the following result:
Microsoft (R) Build Engine Version 4.0.30319.1 [Microsoft .NET Framework, Version 4.0.30319.431] Copyright (C) Microsoft Corporation 2007. All rights reserved. Build started 2/04/2011 8:49:43 AM. Project "C:\dev\Sandbox\IncludeProjectOutputs\IncludeProjectOutputs.sln" on node 1 (default targets). ValidateSolutionConfiguration: Building solution configuration "Debug|Mixed Platforms". Project "C:\dev\Sandbox\IncludeProjectOutputs\IncludeProjectOutputs.sln" (1) is building "C:\dev\Sandbox\IncludeProjectOutputs\IncludeProjectOutputs\IncludeProjectOutputs.csproj" (2) on node 1 (default targets). Project "C:\dev\Sandbox\IncludeProjectOutputs\IncludeProjectOutputs\IncludeProjectOutputs.csproj" (2) is building "C:\dev\Sandbox\IncludeProjectOutputs\Generate\Generate.csproj" (3) on node 1 (default targets). ResolveAssemblyReferences: A TargetFramework profile exclusion list will be generated. GenerateTargetFrameworkMonikerAttribute: Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files. CoreCompile: Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files. _CopyOutOfDateSourceItemsToOutputDirectory: Skipping target "_CopyOutOfDateSourceItemsToOutputDirectory" because all output files are up-to-date with respect to the input files. CopyFilesToOutputDirectory: Generate -> C:\dev\Sandbox\IncludeProjectOutputs\Generate\bin\Debug\Generate.exe AfterBuild: After build execute C:\dev\Sandbox\IncludeProjectOutputs\Generate\bin\Debug\Generate.exe C:\dev\Sandbox\IncludeProjectOutputs\Generate\bin\Debug\Generate.exe Generated file Done Building Project "C:\dev\Sandbox\IncludeProjectOutputs\Generate\Generate.csproj" (default targets). ResolveAssemblyReferences: A TargetFramework profile exclusion list will be generated. GenerateTargetFrameworkMonikerAttribute: Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files. CoreCompile: Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files. _CopyOutOfDateSourceItemsToOutputDirectory: Copying file from "C:\dev\Sandbox\IncludeProjectOutputs\Generate\stuff.txt" to "bin\Debug\stuff.txt". CopyFilesToOutputDirectory: IncludeProjectOutputs -> C:\dev\Sandbox\IncludeProjectOutputs\IncludeProjectOutputs\bin\Debug\IncludeProjectOutputs.exe Done Building Project "C:\dev\Sandbox\IncludeProjectOutputs\IncludeProjectOutputs\IncludeProjectOutputs.csproj" (default targets). Done Building Project "C:\dev\Sandbox\IncludeProjectOutputs\IncludeProjectOutputs.sln" (default targets). Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:00.20
KCO (formerly KUCA Camp Out, formerly Kid’s Camp Out) is an annual overnight camping event that has been running in South Australia since 1978. It’s also probably the largest event that the Uniting Church runs in Australia, as they’ve often had more than 2,700 kids, parents and helpers.
Groups of kids aged 7-12 years (with parents & leaders) come from all over the state. As well as camping, there’s games, crafts, rides, activities, musicals, dramas, bands, and(sometimes) fireworks, in a safe, well organised setting.
My own involvement dates back to at least 1983, as you can see from this old, silverfish-eaten certificate I came across just the other day:
A few years later when I was too old to attend as a kid, I went instead as a teenage helper. Directing traffic (sometimes in the rain), setting up, packing down, all kinds of stuff.
A bit later still, I co-led the teenage helper group for a few years until 2000. This also meant being on the organising committee for the whole event. Believe me, there’s a lot of planning that goes into making sure KCO runs smoothly! One of my highlights was that I got to have my own walkie-talkie (my call-sign was ‘KUCA Big Dave’ if you’re interested).
Probably the only year I missed during that period was 1998, which KCO kindly let me have off as it coincided with my wedding day!
While I have many fond memories, I’m quite excited because tomorrow morning I’m getting on a bus with my two oldest children (along with over 100 other kids and parent leaders from our church) and heading off to KCO 2011.
Some things will have changed in the 10 years since I last went (for one thing it’s now Nuriootpa instead of West Beach – I’ll leave my boardies at home this time), but I know it’s going to be a fantastic weekend!
I just discovered that I’ve passed yet another beta exam!
- 70-583 - PRO: Designing and Developing Windows Azure Applications
So when I get around to taking the 70-513 exam, I would have then fulfilled the requirements for the certification:
- Microsoft Certified Professional Developer - Windows Azure Developer
Time to brush up on my WCF!