Target rebuild only in a .NET Core 3.1 project

Viewed 70

When compiling a .NET Core 3.1 console application, I want to execute a command only on Rebuild (not Build). I can successfully use the "PreBuild" event, but that executes on both Build & Rebuild. Here is an example of what I am trying to achieve:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <Target Name="BeforeRebuild" BeforeTargets="Rebuild">
    <Exec Command="foo" />
  </Target>

</Project>

As written, the command never executes. I believe the problem is that "Rebuild" is not a valid target.

1 Answers

Try this out:

<Target Name="This is a custom rebuild event" BeforeTargets="BeforeRebuild">
    <Exec Command="echo You did a rebuild" />
</Target>

More information here.

Related