1

Closed

Should not use AfterBuild target.

description

Only one AfterBuild target can exist per project, including external target references. This NuGet package adds a reference to a .targets file in the installed project and creates a Target named "AfterBuild". This is done so the target will execute after the build process is complete.

However, if the user has provided their own AfterBuild one of two things will happen.
  1. The installation of this package will cause the .targets import to occur after the user's AfterBuild in the project file, in which case the user's AfterBulid target will not be executed.
  2. The installation of this package will cause the .targets import to occur before the user's AfterBuild in the project file, in which case this package's AfterBuild target will not be executed.
This sort of bug is very difficult for end-users to troubleshoot, especially if they are working with a project that someone else authored (and setup the AfterBuild target).

Recommendation:
If willing to require MSBuild 4.0 or higher then utilizing BeforeTargets and AfterTargets is ideal. This is the exact problem that BeforeTargets and AfterTargets are designed to solve.
http://msdn.microsoft.com/en-us/library/ms171462.aspx
http://blogs.msdn.com/b/msbuild/archive/2010/02/18/build-extensibility-with-net-framework-4.aspx

If unwilling to require MSBuild 4.0 or higher then DependsOnTargets should get the job done instead, without breaking user's project files.

In NuGet.for.MSBuild.targets, this:
  <PropertyGroup>
    <AfterBuildDependsOn>
      $(AfterBuildDependsOn);
      CreateNuGetPackage;
    </AfterBuildDependsOn>
  </PropertyGroup>
  
  <Target Name="AfterBuild" DependsOnTargets="$(AfterBuildDependsOn)"/>
should be replaced with:
  <PropertyGroup>
    <BuildDependsOn>
      $(BuildDependsOn);
      CreateNuGetPackage;
    </BuildDependsOn>
  </PropertyGroup>
Closed Jul 9, 2014 at 2:12 AM by jeff_winn

comments

jeff_winn wrote Jul 8, 2014 at 9:42 PM

I had debated about changing it before, but never had an excuse to. Now I do, good suggestion!

wrote Jul 8, 2014 at 9:46 PM

wrote Jul 9, 2014 at 2:11 AM

wrote Jul 9, 2014 at 2:12 AM

Resolved with changeset 81237: Replacing the AfterBuild target with BuildDependsOn property group for hooking into build.