running custom target only when the compilation was made

Go To


I have developed a custom target, that needs to run only if the compilation (csc) was made (the build process takes too long otherwise). I have found out that Inputs and Outputs attributes can be used to compare file timestamps and execute the target when there is a newer file in the Inputs collection. The problem is that my target does not produce any file, so I am not sure what should I pass as the Outputs attribute to get this working correclty (I cannot use target assembly to check that, because when the compilation is executed, the timestamp of the assembly is actual time and thus my target won't execute).

Any ideas how can I achieve this?

2012-04-04 07:25
by Matus Nemcik


One possible solution is to use PostBuildEvent. In order to get this working, we need to do following:

  1. Set value to the <PostBuildEvent>. That could be done directly within <PropertyGroup>, for example like this: <PostBuildEvent>echo Post build event completed</PostBuildEvent>. I only used echo to write a message to the output, as <PostBuildEvent> cannot be empty.
  2. Set property <PostBuildEvent> to OnOutputUpdated. This causes MSBuild to compare assembly timestamp from before and after compilation and execute <PostBuildEvent> only if the assembly has changed.
  3. Set your dependencies/targets that you want to execute in <PostBuildEventDependsOn>

The complete example looks like this:

    <PostBuildEvent>echo Post build event completed</PostBuildEvent>
2012-04-05 08:32
by Matus Nemcik


You can write out a dummy file and use that for the Outputs. Use the WriteLinesToFile task within your target to output a file called something like "MyTarget.output".

MsBuild will use the timestamp of that file to keep track of when the target was last run, and can then tell if the input files are newer than the output file and know to run the target again.

2012-04-05 08:37
by Scott Langham
Yes, that is definitely one possible way how to achieve this. I was trying to avoid creating dummy output files and find a better way to do this. But thanks for your answer ; - Matus Nemcik 2012-04-05 08:47