Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the name of the current Target in MSBuild?

Tags:

msbuild

Is there a way to get the Name of the currently running Target in MSBuild?

like image 413
PenFold Avatar asked Jan 30 '26 16:01

PenFold


2 Answers

MSBuild does not provide a property (or other mechanism) that provides the name of the current executing target.

If you are thinking in terms of languages and frameworks that expose a stack trace at runtime, MSBuild has nothing equivalent to that.

If you are debugging and trying to visualize the order of target invocation, one trick is to define your own property and successively add values to it as a breadcrumb trail.

e.g.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Target Name="A" DependsOnTargets="B">
    <PropertyGroup>
      <Trace>$(Trace);A</Trace>
    </PropertyGroup>

    <Message Text="$(Trace)" />
  </Target>

  <Target Name="B" DependsOnTargets="C">
    <PropertyGroup>
      <Trace>$(Trace);B</Trace>
    </PropertyGroup>
  </Target>

  <Target Name="C">
    <PropertyGroup>
      <Trace>$(Trace);C</Trace>
    </PropertyGroup>
  </Target>

</Project>

In the example, each PropertyGroup in each target is hard-coding a value that is added to the $(Trace) property.

Running this MSBuild file with verbosity set to normal will produce the output:

;C;B;A

It might be nice to have a generic snippet of code that could be pasted unchanged into each target but currently that's not possible.

like image 199
Jonathan Dodds Avatar answered Feb 03 '26 09:02

Jonathan Dodds


If your version of msbuild supports binlogs (it probably does if you're reading this after I answered this question), you can use the binlog command line switch and view the binlog with a structured log viewer to easily see the order of targets being called without needing to manually add any trace properties. See this microsoft page for more info.

like image 41
Brian Sutherland Avatar answered Feb 03 '26 09:02

Brian Sutherland