6

In my solution, some project have tasks that need to run at the end, such as copy files to various places. We implement that with AfterTargets="Build":

<Target Name="CopyStuff" AfterTargets="Build">
    <Copy SourceFiles="..." DestinationFolder="..." />
</Target>

If works. However, when building the solution (not the individual project!), if the copy fails, we get a red build warning, but msbuild (and therefore TFS build) succeeds:

> msbuild /t:clean;build my.sln
(...)
(in red...) error MSB3021: Unable to copy file (...)

> echo %errorlevel%
0     <<<<<<< This means succeeded

To my understanding, that's because msbuild thinks that as long as the major "Build" target passed, everything passed too.

Our workaround - Change target to BeforeTargets="AfterBuild", which puts my target inside the Build target. However, this requires knowledge of the content of "Build" target, and may not work for other project types.

Question:

  1. Is there a way to get AfterTargets="Build" failures to fail solution builds?
  2. If not, is there a way to automatically validates that people didn't add AfterTargets="Build" into their projects?
Jonathan
  • 6,939
  • 4
  • 44
  • 61

1 Answers1

0

Have you tried adding an <Error /> after the <Copy /> task to throw an error if the contents of the Source and Destination are not the same?

Nicodemeus
  • 4,005
  • 20
  • 23