2

I am trying to have MSBuild generate a text file with a version number representing when the build was created.

Right now in my MSBuild task I have the following:

  <Target Name="WriteBuildDate" AfterTargets="Compile">
    <WriteLinesToFile File="buildversion.txt" Lines="$([System.DateTime]::UtcNow.Year).$([System.DateTime]::UtcNow.Month).$([System.DateTime]::UtcNow.Day).$([System.Math]::Floor($([System.DateTime]::UtcNow.Subtract($([System.DateTime]::UtcNow.Date)).TotalSeconds)))" Overwrite="true" Encoding="Unicode" />
  </Target>

This works, except that it executes even if the all assemblies were not rebuilt. This means if I use the VS publish capability for multiple servers each of them will have slightly different version numbers, even though the builds are all the same.

Is there any way (without custom msbuild tasks) to have this target only execute if at least one assembly was rebuilt?

KallDrexx
  • 27,229
  • 33
  • 143
  • 254
  • 1
    See http://stackoverflow.com/questions/22542820/post-build-event-always-runs-in-msbuild-vs2013 or http://stackoverflow.com/questions/13972820/run-an-msbuild-target-only-if-project-is-actually-built/13974783#13974783 or specify dependencies explicitly like `` (which won't update version if the main executable changed - not sure if I understood that correctly from your question? if not, add $(TargetPath) to Inputs) – stijn Apr 27 '17 at 08:41

1 Answers1

1

Thanks to @stijn's comment/link, I was able to get this working via:

  <PropertyGroup>
    <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
  </PropertyGroup>

  <Target Name="WriteBuildDate" AfterTargets="CoreBuild" Condition="'$(_AssemblyTimestampBeforeCompile)'!='$(_AssemblyTimestampAfterCompile)'">
    <WriteLinesToFile File="buildversion.txt" Lines="$([System.DateTime]::UtcNow.Year).$([System.DateTime]::UtcNow.Month).$([System.DateTime]::UtcNow.Day).$([System.Math]::Floor($([System.DateTime]::UtcNow.Subtract($([System.DateTime]::UtcNow.Date)).TotalSeconds)))" Overwrite="true" Encoding="Unicode" />
  </Target>
KallDrexx
  • 27,229
  • 33
  • 143
  • 254