1

I've added a 'BeforeClean' and an 'AfterClean' target to my .csproj file, originally designed to delete some build artifacts in locations outside the normal build path.

When I did a "clean" on the project, I could tell that the files weren't getting deleted; so I dumbed down the action to just spit a message out. Like the delete command, the message command isn't getting invoked (I'm expecting to see the message in the 'Output' window).

The only relevant advice I could find on the web was to make sure that you do your target definitions after you import the Microsoft.CSharp.targets file. I'm including the clip of my .csproj file with enough detail to show where my target defs are vis-a-vis the import.

As far as I can tell, I'm doing everything right; why would my targets not get invoked?

Thanks in advance.

[Update and FYI: I was able to get the Target to fire when changing Importance from 'normal' to 'high.']

    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
        <PropertyGroup>
            <PreBuildEvent>
            </PreBuildEvent>
        </PropertyGroup>
    <Target Name="BeforeClean">
        <Message Text="Hello Clean World!" Importance="normal" ContinueOnError="true"/>
    </Target>
    <Target Name="AfterClean">
        <Message Text="Goodbye Clean World!" Importance="normal" ContinueOnError="true"/>
    </Target>
</Project>
Literata
  • 175
  • 2
  • 10

2 Answers2

3

It actually is getting invoked, you just can't see it. Run msbuild.exe with /verbosity:normal. Or change the IDE setting: Tools + Options, Projects and Solutions, Build and Run. Or change the Importance attribute to high.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

<Target Name="AfterClean"> is ignored in dotnet core with <Project Sdk="Microsoft.NET.Sdk">, and the problem isn't the verbosity. Instead, the AfterTargets="Clean" approach works:

    <Target Name="PrintGoodbye" AfterTargets="Clean">
      <Message Text="Goodbye Clean World!" Importance="normal" ContinueOnError="true"/>
    </Target>

run like:

$ dotnet clean 
MSBuild version 17.3.2+561848881 for .NET
Build started 12/9/2022 9:05:04 AM.
     1>Project "C:\code\dotNetBytes\Tests\Tests.csproj" on node 1 (Clean target(s)).
     1>PrintGoodbye:
         Goodbye Clean World!
     1>Done Building Project "C:\code\dotNetBytes\Tests\Tests.csproj" (Clean target(s)).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.82
Carl Walsh
  • 6,100
  • 2
  • 46
  • 50