36

Is there something like the AfterBuild Target in msbuild with .NET Core in Visual Studio 2017 RC?

I tried to add the following snipped to the .csproj file, but it is not excuted during a build (Contrary to VS2015 where it does work).

<Target Name="AfterBuild">
  <Message Importance="High" Text="This is a test" />
</Target>

Another interesting discovery: As I thought that the AfterBuild target might have been removed - running msbuild <project.csproj> /t:AfterBuild doesn't seem to call the added target. If I rename the target to "Test" an call it with msbuild <project.csproj> /t:Test it works just fine.


Additionally, is there any documentation on the msbuild version (and possibly the .NET Core build scripts) shipping with Visual Studio 2017 RC?

Fionn
  • 10,975
  • 11
  • 54
  • 84

1 Answers1

52

An alternative is to use the AfterTargets attribute on the Target. Something like:

<Target Name="TestTarget" AfterTargets="Build">
  <Message Importance="High" Text="This is a test" />
</Target>

I'm not sure why "AfterBuild" wouldn't work any more, but this appears to be a conscious decision by the maintainers of MSBuild (h/t to Livven on pointing me to this github issue). "AfterBuild" was a special name that was used by the Build target. The current version of Microsoft.Common.CurrentVersion.targets still has it:

  <PropertyGroup>
    <BuildDependsOn>
      BeforeBuild;
      CoreBuild;
      AfterBuild
    </BuildDependsOn>
  </PropertyGroup>
  <Target
      Name="Build"
      Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
      DependsOnTargets="$(BuildDependsOn)"
      Returns="$(TargetPath)" />
  <!--
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • 8
    The alternate way just saved me. – Dmytro Bogatov Apr 12 '17 at 17:17
  • 9
    Indeed the old `BeforeBuild` and `AfterBuild` targets do not work for .NET Core projects anymore, which seems to be by design. See https://github.com/dotnet/cli/issues/5844 and https://github.com/Microsoft/MSBuild/issues/1680 – Livven Aug 08 '17 at 16:59
  • @Livven That explains it. – Mike Zboray Aug 09 '17 at 18:33
  • 1
    Is there a way to edit this in the IDE? I couldn't find a way to do it without editing the csproj file manually. – SpaceMonkey Dec 28 '17 at 15:32
  • @Spacemonkey AFAIK when you edit the post build event from VS it does something like what I describe above but there is no way to use tasks like Message. The post build event always uses the Exec task. – Mike Zboray Dec 29 '17 at 02:06
  • @mikez I'm using the angular core 2 template from microsoft and it has some commands under Target (like you did here) to run webpack and build client files, but it doesn't show up in VS or Rider. That's really my main frustration. Thanks for your reply tho – SpaceMonkey Dec 29 '17 at 05:20
  • Using AfterTargets="Build" this didn't work on a netcoreapp2.1 application. Did I miss something or does this no longer work? – user3167162 Jul 31 '19 at 06:27
  • @user3167162 AFAIK it still works. I just tried it and it worked for me. – Mike Zboray Jul 31 '19 at 06:36