3
votes

I read John Robbins' article TFS 2010 Build Number and Assembly File Versions: Completely In Sync with Only MSBuild 4.0, and I'm wondering about the best way to go about integrating this.

The download for the article has two files, one is a targets file and one is a proj file.

The targets file has a number of tasks to scrape out a build number based on the Tfs build number (the same one used for the builds) and write that number out to some location (call it BuildNumberFile) for consumption by other proj files.

The proj file is very simple. It just imports the aforementioned targets file, and then declares a target with name "All" while also declaring DefaultTargets on the Project element to be All as well.

    <Project ToolsVersion="4.0" DefaultTargets="All" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <!-- The two required properties so the Wintellect.TFSBuildNumber tasks knows your major and minor values.-->
            <TFSMajorBuildNumber>3</TFSMajorBuildNumber>
            <TFSMinorBuildNumber>1</TFSMinorBuildNumber>
        </PropertyGroup>

        <Import Project="Wintellect.TFSBuildNumber.targets"/>

        <!-- Just ask for the version information files you need. These are here to show all the diffent ones in 
           Wintellect.TFSBuildNumber.Targets. You can change the names -->
        <Target Name="All"
              DependsOnTargets="WriteSharedCSharpAssemblyVersionFile;
                              WriteSharedVBAssemblyVersionFile;
                              WriteSharedCPPCLIAssemblyVersionFile;
                              WriteSharedCPPAssemblyVersionFile;
                              WriteSharedWiXAssemblyVersionFile;
                              WriteSharedTextAssemblyVersionFile;"/>
    </Project>

I have two questions about this:

  • I'm still learning MSBuild. If the name of the target isn't specified elsewhere in the targets, is the target executed? How do I ensure that this target is run?
  • Are the csproj files supposed to declare an Include item for the location where BuildNumberFile is, even though it doesn't exist until compiletime?
  • Do ItemGroups and Include have a DependsOnTargets or something that allows them make sure the file exists before they build?
  • Are the entire contents of the csproj file using this supposed to be wrapped in a target that expresses DependsOnTargets for BuildNumberFile?

Thanks!

1

1 Answers

1
votes

I think I've got this figured out, but two people promoted my question so I'll answer it here:

  • You can ensure that a target is run by expressing a dependency on it from another target. Microsoft.Common.targets exposes two targets--BeforeBuild and AfterBuild--expressly for the purpose of being overridden for customizability. I found the easiest way to do this was <Target Name="BeforeBuild" DependsOnTargets="WriteSharedCSharpAssemblyVersionFile" /> where WriteSharedCSharpAssemblyVersionFile is the target declared in the download from the link in the original post. Also, if you're new to MSBuild, this BeforeBuild target must be declared after the Microsoft.CSharp.targets is imported, but the default csproj template guides you in doing this.

  • The WriteSharedCSharpAssemblyVersionFile target should indeed write the file to some central location, since when building a solution, all targets are executed only once. All projects should reference the file from that location even if it doesn't exist, since by the time compilation happens (or more importantly, by the time references are resolved), the BeforeBuild target will have run and the file will be in place.

    • In my structure, I have these versioning files in a folder directly underneath the branch root folder. Furthermore, since the file being built is generated, I have it build to the output directory. It seems a little strange to be referencing things from the output, but it preserves the invariant of having all build products in one place so that the output directory can be blown away as a means of performing a clean.
  • In MSBuild items constitute inputs into the system (usually files) so it's weird to think of them depending on targets. After some learning this question doesn't make a lot of sense. In any case, the answer is no.

  • The entire contents of the file should indeed not be all in one target--all that is required is to import the Wintellect.TFSBuildNumber.targets file at the beginning of your csproj file, and declare BeforeBuild's dependency on WriteSharedCSharpAssemblyVersionFile at the end.

Hope this helps!