JDługosz
JDługosz

Reputation: 5642

Interacting with standard MSBuild infrastructure: <CleanDependsOn>

I read about using these mechanisms in Brian Kretzler’s book and this example posted by Adam Badura, in particular.

Following Badura’s example (which he notes “Cleaning/Rebuilding does remove the file as expected as well.”) I have:

<PropertyGroup>
    ⋮
    <CleanDependsOn>QtClean;$(CleanDependsOn)</CleanDependsOn> <!-- doesn't work -->
    ⋮
</PropertyGroup>

at top-level (direct child of the document root element).

When I use the IDE menu to Clean Solution, the QtClean Target is not performed. The Message task I included within it does not appear, and the expected effect of the RemoveDir task is not observed (nor are any error messages).

Why would this not work?

Upvotes: 3

Views: 533

Answers (2)

goldcode
goldcode

Reputation: 673

This should run using the latest VS2017 feature enhancement Directory.Build.Targets. Insert the following in a file with the name Directory.Build.Targets at the root folder in trunk of your repository. MSBuild, while loading your .sln, will automatically load your customized Directory.Build.Targets file.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Inject a custom target into Clean by extending CleanDependsOn -->
  <PropertyGroup>
    <CleanDependsOn> $(CleanDependsOn); CustomAfterClean </CleanDependsOn>
  </PropertyGroup>

  <Target Name="CustomAfterClean" Condition="$(ProjectName) == 'XXXMyProjectXXX'" >
    <!--- my custom clean up -->
  </Target>
</Project>

Upvotes: 2

JDługosz
JDługosz

Reputation: 5642

I've learned that <CleanDependsOn> doesn't work because it's overwritten (not appended to) by Microsoft.Common.Targets, which is pulled in at the bottom of a project file. Thus, it cannot be extended by statements within the meaty center of the project file or property sheets as normally included. The “extension targets” are included at the end of the project file, after the normal common targets.

However, the <CppCleanDependsOn> property is extended (not overwritten without including the previous value) everywhere it is used.

One general answer to “Why would this not work?” is that global variables are evil. You have to understand the temporal proximity of the variable (“property”), as it may get changed again before it is read, or read before you are setting it.

Upvotes: 1

Related Questions