nvirth
nvirth

Reputation: 1751

MsBuild: Can I detect if I'm "inside" of an <MSBuild>?

I would like to be able to detect if the current evaluation happens within an <MSBuild>, or it just happens "top level".
So smg like this:

<Project>
  <PropertyGroup>
    <MyProperty>Value if called from "top level" build</MyProperty>
    <MyProperty Condition=" '$(AreWeInsideOfAnMsBuildTarget)' == 'true' ">Value if called from &lt;MSBuild&gt; </MyProperty>
  </PropertyGroup>
</Project>

I can't touch the <MSBuild> task call itself, it is outside of my reach, like:

<Project>
  <Target Name="SomeExternalTargetICantTouch">
    <MSBuild ... />
  </Target>
</Project>

One of my own Targets gets executed 3x within 1 project build, I guess because 1x it gets executed "top level", and then 2x within <MSBuild> tasks.
I guess the <MSBuild> -s are called using different Properties, thus my Target gets reexecuted instead of skipped

Upvotes: 0

Views: 174

Answers (1)

Jonathan Dodds
Jonathan Dodds

Reputation: 5068

MSBuild doesn't provide a built-in property that indicates how the MSBuild project was invoked.

The MSBuild task shares the list of already-built targets across the parent and the child builds so I concur with your assumption that something is being changed on each MSBuild task.

As described in the docs in Incremental builds you could add Inputs and Outputs to your target. (But not all targets will have a clean in/out pattern.)

Upvotes: 1

Related Questions