Search code examples
visual-studio-2012msbuildmsbuild-taskmsbuild-propertygroupmsbuild-target

Sequencing project/solution build and cmd file executing in custom MSBuild file


I need to tie together a bunch of steps which include building solutions, projects and running .cmd files using a custom MSBuild file.

My first pass at this is below:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
      <Configuration>Release</Configuration>
      <Platform>AnyCPU</Platform>
   </PropertyGroup>
   <ItemGroup>
      <ProjectsToBuild Include="..\Hosts\solution1.sln"></ProjectsToBuild>
      <ProjectsToBuild Include="..\..\solution2.sln"></ProjectsToBuild>
      <ProjectsToBuild Include="helper1.csproj"></ProjectsToBuild>
      <ProjectsToBuild Include="..\..\Sandboxes\helper2.sln"></ProjectsToBuild>
      <Exec Include="" Command="CALL GetFiles.cmd"/>
      <ProjectsToBuild Include="wix\proc\prod.wixproj"></ProjectsToBuild>
      <Exec Command="CALL final.cmd"/>
   </ItemGroup>
   <Target Name="Build">
      <MSBuild Projects="@(ProjectsToBuild)" Targets="Build">
        <Output ItemName="ProjectOutputs" TaskParameter="TargetOutputs"/>
    </MSBuild>
    <Message Text="@ProjectOutputs"/>
   </Target>       
</Project>

This resulted in an error since the Exec element is in the wrong place.

Basically, I need to build solution1.sln, solution2.sln,helper1.csproj and helper2.sln (in sequence), then, run the file GetFiles.cmd, then build prod.wixproj followed by running the final.cmd file.

I have looked at MSDN (here, here, here), a blog, and browsed through various stackoverflow questions (including this, this, this, this), but none of them quite address what I am trying to do. This is the first time I have ever worked with MSBuild, so it is possible I may have missed something. Will appreciate any pointers...


Solution

  • Since an ItemGroup node can be a child of a Target node, break down those ItemGroup members into separate targets, then use the DefaultTargets attribute to control the sequence in which those are built.

    <?xml version="1.0" encoding="utf-8"?>
    <Project DefaultTargets="Target1;Target2;Target3" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5" >
        <Target Name="Target1">
            <Message Text="Target 1" />
        </Target>
        <Target Name="Target2">
            <Message Text="Target 2" />
        </Target>
        <Target Name="Target3">
            <Message Text="Target 3" />
        </Target>
    </Project>