Search code examples
visual-studio-2012sql-server-data-tools

SSDT - Run AfterBuild step only if model has changed?


I've got an AfterBuild step in my SSDT project file. This step can take some time, so I'd like only to run this step if something has changed in the SSDT project.

To do this for a C# project, I'm using this:

<Target
    Name="AfterBuild"
    Condition="'$(_AssemblyTimestampBeforeCompile)' != '$(_AssemblyTimestampAfterCompile)'">

Unfortunately this does not work for an SSDT project. Is there a Condition='' that will? Does anyone have any docs I can look at for Conditions that are valid in SSDT projects?


Solution

  • Add the following to the SSDT project:

    <Project>
      <PropertyGroup>
        ...
        <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
      </PropertyGroup>
      ...
      <Target Name="GetTimeStampBeforeBuild" BeforeTargets="BeforeBuild">
        <ItemGroup>
          <ProjectOutputFiles Include="$(TargetPath)" />
        </ItemGroup>
        <PropertyGroup>
          <_AssemblyTimestampBeforeCompile>%(ProjectOutputFiles.ModifiedTime)</_AssemblyTimestampBeforeCompile>
        </PropertyGroup>
      </Target>
      <Target Name="GetTimeStampAfterBuild" AfterTargets="SqlPrepareForRun">
        <PropertyGroup>
          <_AssemblyTimestampAfterCompile>%(ProjectOutputFiles.ModifiedTime)</_AssemblyTimestampAfterCompile>
        </PropertyGroup>
      </Target>
    </Project>
    

    This will prevent the PostBuildEvent target from executing during an incremental build. The post-build event will only run the first time the project is built (after being loaded) and then also on a re-build.