Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an MSBuild file that builds */.sln files (builds all .sln files that exist).

The build uses the Build target, so if no changes were made to input files, no project should be built again.

I would like to execute some custom target only if a project actually gets built again.

How can this be done?

Both AfterBuild and AfterCompile are always called, no matter if the compile/build actually takes place.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
648 views
Welcome To Ask or Share your Answers For Others

1 Answer

Basically you want the same behaviour as the PostBuildEvent for instance, so I looked up how Microsoft.Common.Targets does it (this file always provides a nice insight in to how msbuild is supposed to be used). Here's the solution:

<PropertyGroup>
  <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
</PropertyGroup>
<Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
<Target Name="RunWhenBuild" AfterTargets="CoreBuild"       
        Condition="'$(_AssemblyTimestampBeforeCompile)'!='$(_AssemblyTimestampAfterCompile)'">
  <Message Importance="high" Text="RunWhenBuild!!"/>
</Target>

And this is what goes on: when there is a property named RunPostBuildEvent with a value of OnOutputUpdated the CoreBuild target's dependncies will eventually record the timestamp of the output file before and after the build. And if they are equal the output was not build. So all that is left is getting your target to run after CoreBuild and checking on these timestamps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...