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?
One possible solution is to use PostBuildEvent
.
In order to get this working, we need to do following:
<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.<PostBuildEvent>
to OnOutputUpdated
. This causes MSBuild to compare assembly timestamp from before and after compilation and execute <PostBuildEvent>
only if the assembly has changed.<PostBuildEventDependsOn>
The complete example looks like this:
<PropertyGroup>
<PostBuildEvent>echo Post build event completed</PostBuildEvent>
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
<PostBuildEventDependsOn>$(PostBuildEventDependsOn);MyCustomTarget</PostBuildEventDependsOn>
</PropertyGroup>
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.