How to query MSBUILD file for list of supported targets?Build only one project in a solution from command lineIs there a way to list all the build targets available in a build file?In NAnt, can I create a fileset of the files listed in a VS project?MSBuild: Specifying a target from the command lineHow to suppress specific MSBuild warningMSBuild doesn't copy references (DLL files) if using project dependencies in solutionMSBuild generate files in d:target, but not d:target_PublishedWebsitesHow to Publish Web with msbuild?How do I specify the platform for MSBuild?Build MSBuild target without dependenciesHow to read properties from msbuild fileImport .targets file from command line in msbuildHow can I install the VS2017 version of msbuild on a build server without installing the IDE?

Move arrows along a contour

Was the Psych theme song written for the show?

Was Donald Trump at ground zero helping out on 9-11?

Using Python in a Bash Script

Scam? Checks via Email

Why does one get the wrong value when printing counters together?

Can a US President, after impeachment and removal, be re-elected or re-appointed?

Correct word for a little toy that always stands up?

Why did I lose on time with 3 pawns vs Knight. Shouldn't it be a draw?

Can I attune a Circlet of Human Perfection to my animated skeletons to allow them to blend in and speak?

Prepare a user to perform an action before proceeding to the next step

What language is Raven using for her attack in the new 52?

Why are we moving in circles with a tandem kayak?

Narset, Parter of Veils interaction with Matter Reshaper

Why didn't Stark and Nebula use jump points with their ship to go back to Earth?

What are these bugs on my milkweed?

Circle symbol compatible with square and triangle

What is a good example for artistic ND filter applications?

Is it possible to tell if a child will turn into a Hag?

How did astronauts using rovers tell direction without compasses on the Moon?

What is the highest achievable score in Catan

How to prevent a single-element caster from being useless against immune foes?

On the sensitivity conjecture?

"Valet parking " or "parking valet"



How to query MSBUILD file for list of supported targets?


Build only one project in a solution from command lineIs there a way to list all the build targets available in a build file?In NAnt, can I create a fileset of the files listed in a VS project?MSBuild: Specifying a target from the command lineHow to suppress specific MSBuild warningMSBuild doesn't copy references (DLL files) if using project dependencies in solutionMSBuild generate files in d:target, but not d:target_PublishedWebsitesHow to Publish Web with msbuild?How do I specify the platform for MSBuild?Build MSBuild target without dependenciesHow to read properties from msbuild fileImport .targets file from command line in msbuildHow can I install the VS2017 version of msbuild on a build server without installing the IDE?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








23















Is there any way to ask msbuild what build targets provided msbuild file support? If there is no way to do it in command prompt? May be it could be done programmatically?



Are there no way to do it besides parsing msbuild XML?










share|improve this question


























  • I recently used XML parsing for examining/modifying MSBuild project files. Sorry, no answer for you...

    – mmmmmmmm
    Jan 27 '09 at 15:48

















23















Is there any way to ask msbuild what build targets provided msbuild file support? If there is no way to do it in command prompt? May be it could be done programmatically?



Are there no way to do it besides parsing msbuild XML?










share|improve this question


























  • I recently used XML parsing for examining/modifying MSBuild project files. Sorry, no answer for you...

    – mmmmmmmm
    Jan 27 '09 at 15:48













23












23








23


7






Is there any way to ask msbuild what build targets provided msbuild file support? If there is no way to do it in command prompt? May be it could be done programmatically?



Are there no way to do it besides parsing msbuild XML?










share|improve this question
















Is there any way to ask msbuild what build targets provided msbuild file support? If there is no way to do it in command prompt? May be it could be done programmatically?



Are there no way to do it besides parsing msbuild XML?







msbuild






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 27 '11 at 5:33







Artem Tikhomirov

















asked Jan 14 '09 at 1:43









Artem TikhomirovArtem Tikhomirov

10.2k7 gold badges41 silver badges66 bronze badges




10.2k7 gold badges41 silver badges66 bronze badges















  • I recently used XML parsing for examining/modifying MSBuild project files. Sorry, no answer for you...

    – mmmmmmmm
    Jan 27 '09 at 15:48

















  • I recently used XML parsing for examining/modifying MSBuild project files. Sorry, no answer for you...

    – mmmmmmmm
    Jan 27 '09 at 15:48
















I recently used XML parsing for examining/modifying MSBuild project files. Sorry, no answer for you...

– mmmmmmmm
Jan 27 '09 at 15:48





I recently used XML parsing for examining/modifying MSBuild project files. Sorry, no answer for you...

– mmmmmmmm
Jan 27 '09 at 15:48












6 Answers
6






active

oldest

votes


















11














Updated for .NET Framework 4, since the above has been deprecated. Import microsoft.build.dll and code is as follows:



using System;
using Microsoft.Build.Evaluation;
class MyTargets

static void Main(string[] args)

Project project = new Project(args[0]);
foreach (string target in project.Targets.Keys)

Console.WriteLine("0", target);








share|improve this answer




















  • 17





    My next question would be, Why in the hell do we have to write a program to do this???

    – jww
    Jul 25 '15 at 12:22











  • For Visual Studio 2013 add this reference Microsoft.Build.dll v12 (C:Program Files (x86)MSBuild12.0BinMicrosoft.Build.dll), not the v4 one from GAC.

    – Marcos
    Feb 23 '16 at 20:19






  • 2





    @jww dynamic extensibility. curse and gift at the same time. it causes more tears of pain than joy though.

    – Dbl
    May 18 '16 at 16:14


















18





+150









Certainly MS provides the api to do this without parsing the xml yourself. Look up microsoft.build.buildengine



Adapted from some C# code found on msdn ... it's usually worth exploring. Need to reference the microsoft.build.engine dll for this to compile. Replace framework version and path below with your values. This worked on a sample project file, although the list may be longer than you expect.



using System;
using Microsoft.Build.BuildEngine;
class MyTargets

static void Main(string[] args)

Engine.GlobalEngine.BinPath = @"C:WindowsMicrosoft.NETFrameworkv2.0.NNNNN";
Project project = new Project();
project.Load(@"c:pathtomyproject.proj");
foreach (Target target in project.Targets)

Console.WriteLine("0", target.Name);








share|improve this answer




















  • 18





    My next question would be, Why in the hell do we have to write a program to do this???

    – jww
    Jul 25 '15 at 12:23


















17














I loved the idea of using PowerShell, but the pure XML solution doesn't work because it only outputs targets defined in that project file, and not imports. Of course, the C# code everyone keeps referring to is dead simple, and with .Net 4.5 it's two lines (the first of which you should consider just adding to your profile):



Add-Type -As Microsoft.Build
New-Object Microsoft.Build.Evaluation.Project $Project | Select -Expand Targets


Yeah. Really. That's the whole thing.



Since the output is very verbose, you might want to restrict what you're looking at:



New-Object Microsoft.Build.Evaluation.Project $Project | 
Select -Expand Targets |
Format-Table Name, DependsOnTargets -Wrap


There is a catch, however.



When you load builds like that, they stick in the GlobalProjectCollection as long as you leave that PowerShell window open and you can't re-open them until you unload them. To unload them:



[Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.UnloadAllProjects()


Considering that, it might be worth wrapping that in a function that can accept partial and relative paths or even piped project files as input:



Add-Type -As Microsoft.Build
Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance

function Get-Target
param(
# Path to project file (supports pipeline input and wildcards)
[Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Position=1)]
[Alias("PSPath")]
[String]$Project,

# Filter targets by name. Supports wildcards
[Parameter(Position=2)]
[String]$Name = "*"

)
begin
# People do funny things with parameters
# Lets make sure they didn't pass a Project file as the name ;)
if(-not $Project -and $Name -ne "*")
$Project = Resolve-Path $Name
if($Project) $Name = "*"

if(-not $Project)
$Project = Get-Item *.*proj


process
Write-Host "Project: $_ Target: $Name"
Resolve-Path $Project
end
[microsoft.build.evaluation.projectcollection]::globalprojectcollection.UnloadAllProjects()




And now you don't even need to manually Format-Table...



Addendum:



Obviously you can add anything you want to the output with that Update-TypeData, for instance, if you wanted to see Conditions, or maybe BeforeTargets or AfterTargets...



You could even pull nested information. For instance, you could replace the Update-TypeData call above with these two:



Update-TypeData -MemberName CallTargets -MemberType ScriptProperty -Value % $_.Parameters["Targets"] 
-TypeName Microsoft.Build.Execution.ProjectTargetInstance

Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets, CallTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance


You see the first one adds a calculated CallTargets property that enumerates the direct children and looks for CallTarget tasks to print their Targets, and then we just include that ind the DefaultDisplayPropertySet.



NOTE: It would take a lot of logic on top of this to see every target that's going to be executed when you build any particular target (for that, we'd have to recursively process the DependsOnTargets and we'd also need to look for any targets that have this target in their BeforeTargets or AfterTargets (also recursively), and that's before we get to tasks that can actually just call targets, like CallTargets and MSBuild ... and all of these things can depend on conditions so complex that it's impossible to tell what's going to happen without actually executing it ;)






share|improve this answer



























  • Your solution does indeed, provide a more complete reporting... but: You show DependsOnTargets but you should also include CallTargets to get a full accounting. (Some time ago I adapted @knut's answer and added CallTargets to it.) Is it possible to report CallTargets with your solution?

    – Michael Sorens
    Aug 7 '14 at 1:09











  • For myself, I don't think I care about CallTargets -- I'm not sure what you're trying to accomplish, but I just want to list the targets available to call on the command-line. Having said that, let me append something to the answer for you, before I run out of space in this box.

    – Jaykul
    Aug 8 '14 at 18:18






  • 1





    Does that help? I'm pretty sure that as a general rule, you should not care about CallTargets though -- it is part of the nitty-gritty of how a target does whatever it's supposed to do. Why do you care about this particular task? I mean, you're not going to look at all the tasks from the command-line, and (just for instance), MSBuild is required instead of CallTargets if you just want to call "the default targets" in your build.

    – Jaykul
    Aug 8 '14 at 18:28












  • To my mind, DependsOnTargets tell me that when I call target A, before it processes anything it will call target B and target C. Whereas CallTargets tell me that when I call target A, sometime during its processing it will call target B and target C. They seem to be equivalently-useful pieces of information. Or is my view of this skewed in some fashion...?

    – Michael Sorens
    Aug 8 '14 at 23:05











  • The thing is that there are lots of ways to do the same thing differently ;) A can DependsOnTargets B or A can After B or B can Before A ... or B can CallTarget A or it can MSBuild Project="$(MSBuildProjectFile)" Targets="A" ... or it could use Exec or there could be third-party tasks ...

    – Jaykul
    Aug 10 '14 at 19:51


















2














I suggest you use PowerShell:



Select-Xml `
-XPath //b:Target `
-Path path-to-build-file `
-Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
Select-Object -ExpandProperty Node |
Format-Table -Property Name, DependsOnTargets -AutoSize


The XPath query will find all Target elements and display the target name and dependencies in table format. Here is a sample that selects the 10 first targets from Microsoft.Web.Publishing.targets:



PS C:Program Files (x86)MSBuildMicrosoftVisualStudiov11.0Web> Select-Xml `
-XPath //b:Target `
-Path Microsoft.Web.Publishing.targets `
-Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
Select-Object -ExpandProperty Node |
Sort-Object -Property Name |
Select-Object -First 10 |
Format-Table -Property Name, DependsOnTargets -AutoSize

Name DependsOnTargets
---- ----------------
_CheckPublishToolsUpToDate
_CheckRemoteFx45
_CleanWPPIfNeedTo
_DetectDbDacFxProvider
_WPPCopyWebApplication $(_WPPCopyWebApplicationDependsOn)
AddContentPathToSourceManifest $(AddContentPathToSourceManifestDepe...
AddDeclareParametersItems $(AddDeclareParametersItemsDependsOn)
AddDeclareParametersItemsForContentPath $(AddDeclareParametersItemsForConten...
AddDeclareParametersItemsForIis6 $(AddDeclareParametersItemsForIis6De...
AddDeclareParametersItemsForIis7 $(AddDeclareParametersItemsForIis7De...





share|improve this answer



























  • Very nice! How difficult is it to add a column for the list of values in the immediate child node CallTarget? Some dependencies lie there as well.

    – Michael Sorens
    Apr 17 '13 at 21:32






  • 1





    The problem with that is that it's file based, and won't import the includes, so for an actual project build file, you'll get only a tiny subset of the targets.

    – Jaykul
    Aug 6 '14 at 22:04


















0














Here is the code snippet to get all targets in the order their execution.



 static void Main(string[] args)

Project project = new Project(@"build.core.xml");
var orderedTargets = GetAllTargetsInOrderOfExecution(project.Targets, project.Targets["FinalTargetInTheDependencyChain"]).ToList();
File.WriteAllText(@"orderedTargets.txt", orderedTargets.Select(x => x.Name).Aggregate((a, b) => a + "rn" + b));


/// <summary>
/// Gets all targets in the order of their execution by traversing through the dependent targets recursively
/// </summary>
/// <param name="allTargetsInfo"></param>
/// <param name="target"></param>
/// <returns></returns>
public static List<ProjectTargetInstance> GetAllTargetsInOrderOfExecution(IDictionary<string, ProjectTargetInstance> allTargetsInfo, ProjectTargetInstance target)

var orderedTargets = new List<ProjectTargetInstance>();

var dependentTargets =
target
.DependsOnTargets
.Split(';')
.Where(allTargetsInfo.ContainsKey)
.Select(x => allTargetsInfo[x])
.ToList();

foreach (var dependentTarget in dependentTargets)

orderedTargets = orderedTargets.Union(GetAllTargetsInOrderOfExecution(allTargetsInfo, dependentTarget)).ToList();


orderedTargets.Add(target);

return orderedTargets;






share|improve this answer


































    0














    If you build from command-line you can generate a metaproject file by setting an environment variable before building. This file will detail everything that a solution-file will target as well as outlining their target order.



    It's not really a read-friendly diagnostic file but does have all the information you likely need in XML format.



    E.g. from CMD and a typical VS2017 installation; replace line 1 with whatever build-tool-loader you use



    call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
    set MSBuildEmitSolution=1
    call MSBuild SolutionFile.sln /t:rebuild


    For a real example, I've made an SLN file starting with a C# project called mixed_proj, so I've got mixed_proj.sln, mixed_proj.csproj, then the two C++ projects I added, ConsoleApplication1 and DLL1.



    I've set the build dependency order to where ConsoleApplication1 only builds after DLL1



    Here is the command-line for building it:



    call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
    set MSBuildEmitSolution=1
    call MSBuild miced_proj.sln /t:rebuild


    Here is the generated metaproject file content (too big to paste here)






    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f441614%2fhow-to-query-msbuild-file-for-list-of-supported-targets%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      11














      Updated for .NET Framework 4, since the above has been deprecated. Import microsoft.build.dll and code is as follows:



      using System;
      using Microsoft.Build.Evaluation;
      class MyTargets

      static void Main(string[] args)

      Project project = new Project(args[0]);
      foreach (string target in project.Targets.Keys)

      Console.WriteLine("0", target);








      share|improve this answer




















      • 17





        My next question would be, Why in the hell do we have to write a program to do this???

        – jww
        Jul 25 '15 at 12:22











      • For Visual Studio 2013 add this reference Microsoft.Build.dll v12 (C:Program Files (x86)MSBuild12.0BinMicrosoft.Build.dll), not the v4 one from GAC.

        – Marcos
        Feb 23 '16 at 20:19






      • 2





        @jww dynamic extensibility. curse and gift at the same time. it causes more tears of pain than joy though.

        – Dbl
        May 18 '16 at 16:14















      11














      Updated for .NET Framework 4, since the above has been deprecated. Import microsoft.build.dll and code is as follows:



      using System;
      using Microsoft.Build.Evaluation;
      class MyTargets

      static void Main(string[] args)

      Project project = new Project(args[0]);
      foreach (string target in project.Targets.Keys)

      Console.WriteLine("0", target);








      share|improve this answer




















      • 17





        My next question would be, Why in the hell do we have to write a program to do this???

        – jww
        Jul 25 '15 at 12:22











      • For Visual Studio 2013 add this reference Microsoft.Build.dll v12 (C:Program Files (x86)MSBuild12.0BinMicrosoft.Build.dll), not the v4 one from GAC.

        – Marcos
        Feb 23 '16 at 20:19






      • 2





        @jww dynamic extensibility. curse and gift at the same time. it causes more tears of pain than joy though.

        – Dbl
        May 18 '16 at 16:14













      11












      11








      11







      Updated for .NET Framework 4, since the above has been deprecated. Import microsoft.build.dll and code is as follows:



      using System;
      using Microsoft.Build.Evaluation;
      class MyTargets

      static void Main(string[] args)

      Project project = new Project(args[0]);
      foreach (string target in project.Targets.Keys)

      Console.WriteLine("0", target);








      share|improve this answer













      Updated for .NET Framework 4, since the above has been deprecated. Import microsoft.build.dll and code is as follows:



      using System;
      using Microsoft.Build.Evaluation;
      class MyTargets

      static void Main(string[] args)

      Project project = new Project(args[0]);
      foreach (string target in project.Targets.Keys)

      Console.WriteLine("0", target);









      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 31 '12 at 9:43









      woodswoods

      1261 silver badge2 bronze badges




      1261 silver badge2 bronze badges










      • 17





        My next question would be, Why in the hell do we have to write a program to do this???

        – jww
        Jul 25 '15 at 12:22











      • For Visual Studio 2013 add this reference Microsoft.Build.dll v12 (C:Program Files (x86)MSBuild12.0BinMicrosoft.Build.dll), not the v4 one from GAC.

        – Marcos
        Feb 23 '16 at 20:19






      • 2





        @jww dynamic extensibility. curse and gift at the same time. it causes more tears of pain than joy though.

        – Dbl
        May 18 '16 at 16:14












      • 17





        My next question would be, Why in the hell do we have to write a program to do this???

        – jww
        Jul 25 '15 at 12:22











      • For Visual Studio 2013 add this reference Microsoft.Build.dll v12 (C:Program Files (x86)MSBuild12.0BinMicrosoft.Build.dll), not the v4 one from GAC.

        – Marcos
        Feb 23 '16 at 20:19






      • 2





        @jww dynamic extensibility. curse and gift at the same time. it causes more tears of pain than joy though.

        – Dbl
        May 18 '16 at 16:14







      17




      17





      My next question would be, Why in the hell do we have to write a program to do this???

      – jww
      Jul 25 '15 at 12:22





      My next question would be, Why in the hell do we have to write a program to do this???

      – jww
      Jul 25 '15 at 12:22













      For Visual Studio 2013 add this reference Microsoft.Build.dll v12 (C:Program Files (x86)MSBuild12.0BinMicrosoft.Build.dll), not the v4 one from GAC.

      – Marcos
      Feb 23 '16 at 20:19





      For Visual Studio 2013 add this reference Microsoft.Build.dll v12 (C:Program Files (x86)MSBuild12.0BinMicrosoft.Build.dll), not the v4 one from GAC.

      – Marcos
      Feb 23 '16 at 20:19




      2




      2





      @jww dynamic extensibility. curse and gift at the same time. it causes more tears of pain than joy though.

      – Dbl
      May 18 '16 at 16:14





      @jww dynamic extensibility. curse and gift at the same time. it causes more tears of pain than joy though.

      – Dbl
      May 18 '16 at 16:14













      18





      +150









      Certainly MS provides the api to do this without parsing the xml yourself. Look up microsoft.build.buildengine



      Adapted from some C# code found on msdn ... it's usually worth exploring. Need to reference the microsoft.build.engine dll for this to compile. Replace framework version and path below with your values. This worked on a sample project file, although the list may be longer than you expect.



      using System;
      using Microsoft.Build.BuildEngine;
      class MyTargets

      static void Main(string[] args)

      Engine.GlobalEngine.BinPath = @"C:WindowsMicrosoft.NETFrameworkv2.0.NNNNN";
      Project project = new Project();
      project.Load(@"c:pathtomyproject.proj");
      foreach (Target target in project.Targets)

      Console.WriteLine("0", target.Name);








      share|improve this answer




















      • 18





        My next question would be, Why in the hell do we have to write a program to do this???

        – jww
        Jul 25 '15 at 12:23















      18





      +150









      Certainly MS provides the api to do this without parsing the xml yourself. Look up microsoft.build.buildengine



      Adapted from some C# code found on msdn ... it's usually worth exploring. Need to reference the microsoft.build.engine dll for this to compile. Replace framework version and path below with your values. This worked on a sample project file, although the list may be longer than you expect.



      using System;
      using Microsoft.Build.BuildEngine;
      class MyTargets

      static void Main(string[] args)

      Engine.GlobalEngine.BinPath = @"C:WindowsMicrosoft.NETFrameworkv2.0.NNNNN";
      Project project = new Project();
      project.Load(@"c:pathtomyproject.proj");
      foreach (Target target in project.Targets)

      Console.WriteLine("0", target.Name);








      share|improve this answer




















      • 18





        My next question would be, Why in the hell do we have to write a program to do this???

        – jww
        Jul 25 '15 at 12:23













      18





      +150







      18





      +150



      18




      +150





      Certainly MS provides the api to do this without parsing the xml yourself. Look up microsoft.build.buildengine



      Adapted from some C# code found on msdn ... it's usually worth exploring. Need to reference the microsoft.build.engine dll for this to compile. Replace framework version and path below with your values. This worked on a sample project file, although the list may be longer than you expect.



      using System;
      using Microsoft.Build.BuildEngine;
      class MyTargets

      static void Main(string[] args)

      Engine.GlobalEngine.BinPath = @"C:WindowsMicrosoft.NETFrameworkv2.0.NNNNN";
      Project project = new Project();
      project.Load(@"c:pathtomyproject.proj");
      foreach (Target target in project.Targets)

      Console.WriteLine("0", target.Name);








      share|improve this answer













      Certainly MS provides the api to do this without parsing the xml yourself. Look up microsoft.build.buildengine



      Adapted from some C# code found on msdn ... it's usually worth exploring. Need to reference the microsoft.build.engine dll for this to compile. Replace framework version and path below with your values. This worked on a sample project file, although the list may be longer than you expect.



      using System;
      using Microsoft.Build.BuildEngine;
      class MyTargets

      static void Main(string[] args)

      Engine.GlobalEngine.BinPath = @"C:WindowsMicrosoft.NETFrameworkv2.0.NNNNN";
      Project project = new Project();
      project.Load(@"c:pathtomyproject.proj");
      foreach (Target target in project.Targets)

      Console.WriteLine("0", target.Name);









      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 27 '09 at 18:07









      Zac ThompsonZac Thompson

      10.8k35 silver badges53 bronze badges




      10.8k35 silver badges53 bronze badges










      • 18





        My next question would be, Why in the hell do we have to write a program to do this???

        – jww
        Jul 25 '15 at 12:23












      • 18





        My next question would be, Why in the hell do we have to write a program to do this???

        – jww
        Jul 25 '15 at 12:23







      18




      18





      My next question would be, Why in the hell do we have to write a program to do this???

      – jww
      Jul 25 '15 at 12:23





      My next question would be, Why in the hell do we have to write a program to do this???

      – jww
      Jul 25 '15 at 12:23











      17














      I loved the idea of using PowerShell, but the pure XML solution doesn't work because it only outputs targets defined in that project file, and not imports. Of course, the C# code everyone keeps referring to is dead simple, and with .Net 4.5 it's two lines (the first of which you should consider just adding to your profile):



      Add-Type -As Microsoft.Build
      New-Object Microsoft.Build.Evaluation.Project $Project | Select -Expand Targets


      Yeah. Really. That's the whole thing.



      Since the output is very verbose, you might want to restrict what you're looking at:



      New-Object Microsoft.Build.Evaluation.Project $Project | 
      Select -Expand Targets |
      Format-Table Name, DependsOnTargets -Wrap


      There is a catch, however.



      When you load builds like that, they stick in the GlobalProjectCollection as long as you leave that PowerShell window open and you can't re-open them until you unload them. To unload them:



      [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.UnloadAllProjects()


      Considering that, it might be worth wrapping that in a function that can accept partial and relative paths or even piped project files as input:



      Add-Type -As Microsoft.Build
      Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance

      function Get-Target
      param(
      # Path to project file (supports pipeline input and wildcards)
      [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Position=1)]
      [Alias("PSPath")]
      [String]$Project,

      # Filter targets by name. Supports wildcards
      [Parameter(Position=2)]
      [String]$Name = "*"

      )
      begin
      # People do funny things with parameters
      # Lets make sure they didn't pass a Project file as the name ;)
      if(-not $Project -and $Name -ne "*")
      $Project = Resolve-Path $Name
      if($Project) $Name = "*"

      if(-not $Project)
      $Project = Get-Item *.*proj


      process
      Write-Host "Project: $_ Target: $Name"
      Resolve-Path $Project
      end
      [microsoft.build.evaluation.projectcollection]::globalprojectcollection.UnloadAllProjects()




      And now you don't even need to manually Format-Table...



      Addendum:



      Obviously you can add anything you want to the output with that Update-TypeData, for instance, if you wanted to see Conditions, or maybe BeforeTargets or AfterTargets...



      You could even pull nested information. For instance, you could replace the Update-TypeData call above with these two:



      Update-TypeData -MemberName CallTargets -MemberType ScriptProperty -Value % $_.Parameters["Targets"] 
      -TypeName Microsoft.Build.Execution.ProjectTargetInstance

      Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets, CallTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance


      You see the first one adds a calculated CallTargets property that enumerates the direct children and looks for CallTarget tasks to print their Targets, and then we just include that ind the DefaultDisplayPropertySet.



      NOTE: It would take a lot of logic on top of this to see every target that's going to be executed when you build any particular target (for that, we'd have to recursively process the DependsOnTargets and we'd also need to look for any targets that have this target in their BeforeTargets or AfterTargets (also recursively), and that's before we get to tasks that can actually just call targets, like CallTargets and MSBuild ... and all of these things can depend on conditions so complex that it's impossible to tell what's going to happen without actually executing it ;)






      share|improve this answer



























      • Your solution does indeed, provide a more complete reporting... but: You show DependsOnTargets but you should also include CallTargets to get a full accounting. (Some time ago I adapted @knut's answer and added CallTargets to it.) Is it possible to report CallTargets with your solution?

        – Michael Sorens
        Aug 7 '14 at 1:09











      • For myself, I don't think I care about CallTargets -- I'm not sure what you're trying to accomplish, but I just want to list the targets available to call on the command-line. Having said that, let me append something to the answer for you, before I run out of space in this box.

        – Jaykul
        Aug 8 '14 at 18:18






      • 1





        Does that help? I'm pretty sure that as a general rule, you should not care about CallTargets though -- it is part of the nitty-gritty of how a target does whatever it's supposed to do. Why do you care about this particular task? I mean, you're not going to look at all the tasks from the command-line, and (just for instance), MSBuild is required instead of CallTargets if you just want to call "the default targets" in your build.

        – Jaykul
        Aug 8 '14 at 18:28












      • To my mind, DependsOnTargets tell me that when I call target A, before it processes anything it will call target B and target C. Whereas CallTargets tell me that when I call target A, sometime during its processing it will call target B and target C. They seem to be equivalently-useful pieces of information. Or is my view of this skewed in some fashion...?

        – Michael Sorens
        Aug 8 '14 at 23:05











      • The thing is that there are lots of ways to do the same thing differently ;) A can DependsOnTargets B or A can After B or B can Before A ... or B can CallTarget A or it can MSBuild Project="$(MSBuildProjectFile)" Targets="A" ... or it could use Exec or there could be third-party tasks ...

        – Jaykul
        Aug 10 '14 at 19:51















      17














      I loved the idea of using PowerShell, but the pure XML solution doesn't work because it only outputs targets defined in that project file, and not imports. Of course, the C# code everyone keeps referring to is dead simple, and with .Net 4.5 it's two lines (the first of which you should consider just adding to your profile):



      Add-Type -As Microsoft.Build
      New-Object Microsoft.Build.Evaluation.Project $Project | Select -Expand Targets


      Yeah. Really. That's the whole thing.



      Since the output is very verbose, you might want to restrict what you're looking at:



      New-Object Microsoft.Build.Evaluation.Project $Project | 
      Select -Expand Targets |
      Format-Table Name, DependsOnTargets -Wrap


      There is a catch, however.



      When you load builds like that, they stick in the GlobalProjectCollection as long as you leave that PowerShell window open and you can't re-open them until you unload them. To unload them:



      [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.UnloadAllProjects()


      Considering that, it might be worth wrapping that in a function that can accept partial and relative paths or even piped project files as input:



      Add-Type -As Microsoft.Build
      Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance

      function Get-Target
      param(
      # Path to project file (supports pipeline input and wildcards)
      [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Position=1)]
      [Alias("PSPath")]
      [String]$Project,

      # Filter targets by name. Supports wildcards
      [Parameter(Position=2)]
      [String]$Name = "*"

      )
      begin
      # People do funny things with parameters
      # Lets make sure they didn't pass a Project file as the name ;)
      if(-not $Project -and $Name -ne "*")
      $Project = Resolve-Path $Name
      if($Project) $Name = "*"

      if(-not $Project)
      $Project = Get-Item *.*proj


      process
      Write-Host "Project: $_ Target: $Name"
      Resolve-Path $Project
      end
      [microsoft.build.evaluation.projectcollection]::globalprojectcollection.UnloadAllProjects()




      And now you don't even need to manually Format-Table...



      Addendum:



      Obviously you can add anything you want to the output with that Update-TypeData, for instance, if you wanted to see Conditions, or maybe BeforeTargets or AfterTargets...



      You could even pull nested information. For instance, you could replace the Update-TypeData call above with these two:



      Update-TypeData -MemberName CallTargets -MemberType ScriptProperty -Value % $_.Parameters["Targets"] 
      -TypeName Microsoft.Build.Execution.ProjectTargetInstance

      Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets, CallTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance


      You see the first one adds a calculated CallTargets property that enumerates the direct children and looks for CallTarget tasks to print their Targets, and then we just include that ind the DefaultDisplayPropertySet.



      NOTE: It would take a lot of logic on top of this to see every target that's going to be executed when you build any particular target (for that, we'd have to recursively process the DependsOnTargets and we'd also need to look for any targets that have this target in their BeforeTargets or AfterTargets (also recursively), and that's before we get to tasks that can actually just call targets, like CallTargets and MSBuild ... and all of these things can depend on conditions so complex that it's impossible to tell what's going to happen without actually executing it ;)






      share|improve this answer



























      • Your solution does indeed, provide a more complete reporting... but: You show DependsOnTargets but you should also include CallTargets to get a full accounting. (Some time ago I adapted @knut's answer and added CallTargets to it.) Is it possible to report CallTargets with your solution?

        – Michael Sorens
        Aug 7 '14 at 1:09











      • For myself, I don't think I care about CallTargets -- I'm not sure what you're trying to accomplish, but I just want to list the targets available to call on the command-line. Having said that, let me append something to the answer for you, before I run out of space in this box.

        – Jaykul
        Aug 8 '14 at 18:18






      • 1





        Does that help? I'm pretty sure that as a general rule, you should not care about CallTargets though -- it is part of the nitty-gritty of how a target does whatever it's supposed to do. Why do you care about this particular task? I mean, you're not going to look at all the tasks from the command-line, and (just for instance), MSBuild is required instead of CallTargets if you just want to call "the default targets" in your build.

        – Jaykul
        Aug 8 '14 at 18:28












      • To my mind, DependsOnTargets tell me that when I call target A, before it processes anything it will call target B and target C. Whereas CallTargets tell me that when I call target A, sometime during its processing it will call target B and target C. They seem to be equivalently-useful pieces of information. Or is my view of this skewed in some fashion...?

        – Michael Sorens
        Aug 8 '14 at 23:05











      • The thing is that there are lots of ways to do the same thing differently ;) A can DependsOnTargets B or A can After B or B can Before A ... or B can CallTarget A or it can MSBuild Project="$(MSBuildProjectFile)" Targets="A" ... or it could use Exec or there could be third-party tasks ...

        – Jaykul
        Aug 10 '14 at 19:51













      17












      17








      17







      I loved the idea of using PowerShell, but the pure XML solution doesn't work because it only outputs targets defined in that project file, and not imports. Of course, the C# code everyone keeps referring to is dead simple, and with .Net 4.5 it's two lines (the first of which you should consider just adding to your profile):



      Add-Type -As Microsoft.Build
      New-Object Microsoft.Build.Evaluation.Project $Project | Select -Expand Targets


      Yeah. Really. That's the whole thing.



      Since the output is very verbose, you might want to restrict what you're looking at:



      New-Object Microsoft.Build.Evaluation.Project $Project | 
      Select -Expand Targets |
      Format-Table Name, DependsOnTargets -Wrap


      There is a catch, however.



      When you load builds like that, they stick in the GlobalProjectCollection as long as you leave that PowerShell window open and you can't re-open them until you unload them. To unload them:



      [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.UnloadAllProjects()


      Considering that, it might be worth wrapping that in a function that can accept partial and relative paths or even piped project files as input:



      Add-Type -As Microsoft.Build
      Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance

      function Get-Target
      param(
      # Path to project file (supports pipeline input and wildcards)
      [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Position=1)]
      [Alias("PSPath")]
      [String]$Project,

      # Filter targets by name. Supports wildcards
      [Parameter(Position=2)]
      [String]$Name = "*"

      )
      begin
      # People do funny things with parameters
      # Lets make sure they didn't pass a Project file as the name ;)
      if(-not $Project -and $Name -ne "*")
      $Project = Resolve-Path $Name
      if($Project) $Name = "*"

      if(-not $Project)
      $Project = Get-Item *.*proj


      process
      Write-Host "Project: $_ Target: $Name"
      Resolve-Path $Project
      end
      [microsoft.build.evaluation.projectcollection]::globalprojectcollection.UnloadAllProjects()




      And now you don't even need to manually Format-Table...



      Addendum:



      Obviously you can add anything you want to the output with that Update-TypeData, for instance, if you wanted to see Conditions, or maybe BeforeTargets or AfterTargets...



      You could even pull nested information. For instance, you could replace the Update-TypeData call above with these two:



      Update-TypeData -MemberName CallTargets -MemberType ScriptProperty -Value % $_.Parameters["Targets"] 
      -TypeName Microsoft.Build.Execution.ProjectTargetInstance

      Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets, CallTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance


      You see the first one adds a calculated CallTargets property that enumerates the direct children and looks for CallTarget tasks to print their Targets, and then we just include that ind the DefaultDisplayPropertySet.



      NOTE: It would take a lot of logic on top of this to see every target that's going to be executed when you build any particular target (for that, we'd have to recursively process the DependsOnTargets and we'd also need to look for any targets that have this target in their BeforeTargets or AfterTargets (also recursively), and that's before we get to tasks that can actually just call targets, like CallTargets and MSBuild ... and all of these things can depend on conditions so complex that it's impossible to tell what's going to happen without actually executing it ;)






      share|improve this answer















      I loved the idea of using PowerShell, but the pure XML solution doesn't work because it only outputs targets defined in that project file, and not imports. Of course, the C# code everyone keeps referring to is dead simple, and with .Net 4.5 it's two lines (the first of which you should consider just adding to your profile):



      Add-Type -As Microsoft.Build
      New-Object Microsoft.Build.Evaluation.Project $Project | Select -Expand Targets


      Yeah. Really. That's the whole thing.



      Since the output is very verbose, you might want to restrict what you're looking at:



      New-Object Microsoft.Build.Evaluation.Project $Project | 
      Select -Expand Targets |
      Format-Table Name, DependsOnTargets -Wrap


      There is a catch, however.



      When you load builds like that, they stick in the GlobalProjectCollection as long as you leave that PowerShell window open and you can't re-open them until you unload them. To unload them:



      [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.UnloadAllProjects()


      Considering that, it might be worth wrapping that in a function that can accept partial and relative paths or even piped project files as input:



      Add-Type -As Microsoft.Build
      Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance

      function Get-Target
      param(
      # Path to project file (supports pipeline input and wildcards)
      [Parameter(ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, Position=1)]
      [Alias("PSPath")]
      [String]$Project,

      # Filter targets by name. Supports wildcards
      [Parameter(Position=2)]
      [String]$Name = "*"

      )
      begin
      # People do funny things with parameters
      # Lets make sure they didn't pass a Project file as the name ;)
      if(-not $Project -and $Name -ne "*")
      $Project = Resolve-Path $Name
      if($Project) $Name = "*"

      if(-not $Project)
      $Project = Get-Item *.*proj


      process
      Write-Host "Project: $_ Target: $Name"
      Resolve-Path $Project
      end
      [microsoft.build.evaluation.projectcollection]::globalprojectcollection.UnloadAllProjects()




      And now you don't even need to manually Format-Table...



      Addendum:



      Obviously you can add anything you want to the output with that Update-TypeData, for instance, if you wanted to see Conditions, or maybe BeforeTargets or AfterTargets...



      You could even pull nested information. For instance, you could replace the Update-TypeData call above with these two:



      Update-TypeData -MemberName CallTargets -MemberType ScriptProperty -Value % $_.Parameters["Targets"] 
      -TypeName Microsoft.Build.Execution.ProjectTargetInstance

      Update-TypeData -DefaultDisplayPropertySet Name, DependsOnTargets, CallTargets -TypeName Microsoft.Build.Execution.ProjectTargetInstance


      You see the first one adds a calculated CallTargets property that enumerates the direct children and looks for CallTarget tasks to print their Targets, and then we just include that ind the DefaultDisplayPropertySet.



      NOTE: It would take a lot of logic on top of this to see every target that's going to be executed when you build any particular target (for that, we'd have to recursively process the DependsOnTargets and we'd also need to look for any targets that have this target in their BeforeTargets or AfterTargets (also recursively), and that's before we get to tasks that can actually just call targets, like CallTargets and MSBuild ... and all of these things can depend on conditions so complex that it's impossible to tell what's going to happen without actually executing it ;)







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 8 '14 at 18:24

























      answered Aug 6 '14 at 23:03









      JaykulJaykul

      12.3k6 gold badges48 silver badges64 bronze badges




      12.3k6 gold badges48 silver badges64 bronze badges















      • Your solution does indeed, provide a more complete reporting... but: You show DependsOnTargets but you should also include CallTargets to get a full accounting. (Some time ago I adapted @knut's answer and added CallTargets to it.) Is it possible to report CallTargets with your solution?

        – Michael Sorens
        Aug 7 '14 at 1:09











      • For myself, I don't think I care about CallTargets -- I'm not sure what you're trying to accomplish, but I just want to list the targets available to call on the command-line. Having said that, let me append something to the answer for you, before I run out of space in this box.

        – Jaykul
        Aug 8 '14 at 18:18






      • 1





        Does that help? I'm pretty sure that as a general rule, you should not care about CallTargets though -- it is part of the nitty-gritty of how a target does whatever it's supposed to do. Why do you care about this particular task? I mean, you're not going to look at all the tasks from the command-line, and (just for instance), MSBuild is required instead of CallTargets if you just want to call "the default targets" in your build.

        – Jaykul
        Aug 8 '14 at 18:28












      • To my mind, DependsOnTargets tell me that when I call target A, before it processes anything it will call target B and target C. Whereas CallTargets tell me that when I call target A, sometime during its processing it will call target B and target C. They seem to be equivalently-useful pieces of information. Or is my view of this skewed in some fashion...?

        – Michael Sorens
        Aug 8 '14 at 23:05











      • The thing is that there are lots of ways to do the same thing differently ;) A can DependsOnTargets B or A can After B or B can Before A ... or B can CallTarget A or it can MSBuild Project="$(MSBuildProjectFile)" Targets="A" ... or it could use Exec or there could be third-party tasks ...

        – Jaykul
        Aug 10 '14 at 19:51

















      • Your solution does indeed, provide a more complete reporting... but: You show DependsOnTargets but you should also include CallTargets to get a full accounting. (Some time ago I adapted @knut's answer and added CallTargets to it.) Is it possible to report CallTargets with your solution?

        – Michael Sorens
        Aug 7 '14 at 1:09











      • For myself, I don't think I care about CallTargets -- I'm not sure what you're trying to accomplish, but I just want to list the targets available to call on the command-line. Having said that, let me append something to the answer for you, before I run out of space in this box.

        – Jaykul
        Aug 8 '14 at 18:18






      • 1





        Does that help? I'm pretty sure that as a general rule, you should not care about CallTargets though -- it is part of the nitty-gritty of how a target does whatever it's supposed to do. Why do you care about this particular task? I mean, you're not going to look at all the tasks from the command-line, and (just for instance), MSBuild is required instead of CallTargets if you just want to call "the default targets" in your build.

        – Jaykul
        Aug 8 '14 at 18:28












      • To my mind, DependsOnTargets tell me that when I call target A, before it processes anything it will call target B and target C. Whereas CallTargets tell me that when I call target A, sometime during its processing it will call target B and target C. They seem to be equivalently-useful pieces of information. Or is my view of this skewed in some fashion...?

        – Michael Sorens
        Aug 8 '14 at 23:05











      • The thing is that there are lots of ways to do the same thing differently ;) A can DependsOnTargets B or A can After B or B can Before A ... or B can CallTarget A or it can MSBuild Project="$(MSBuildProjectFile)" Targets="A" ... or it could use Exec or there could be third-party tasks ...

        – Jaykul
        Aug 10 '14 at 19:51
















      Your solution does indeed, provide a more complete reporting... but: You show DependsOnTargets but you should also include CallTargets to get a full accounting. (Some time ago I adapted @knut's answer and added CallTargets to it.) Is it possible to report CallTargets with your solution?

      – Michael Sorens
      Aug 7 '14 at 1:09





      Your solution does indeed, provide a more complete reporting... but: You show DependsOnTargets but you should also include CallTargets to get a full accounting. (Some time ago I adapted @knut's answer and added CallTargets to it.) Is it possible to report CallTargets with your solution?

      – Michael Sorens
      Aug 7 '14 at 1:09













      For myself, I don't think I care about CallTargets -- I'm not sure what you're trying to accomplish, but I just want to list the targets available to call on the command-line. Having said that, let me append something to the answer for you, before I run out of space in this box.

      – Jaykul
      Aug 8 '14 at 18:18





      For myself, I don't think I care about CallTargets -- I'm not sure what you're trying to accomplish, but I just want to list the targets available to call on the command-line. Having said that, let me append something to the answer for you, before I run out of space in this box.

      – Jaykul
      Aug 8 '14 at 18:18




      1




      1





      Does that help? I'm pretty sure that as a general rule, you should not care about CallTargets though -- it is part of the nitty-gritty of how a target does whatever it's supposed to do. Why do you care about this particular task? I mean, you're not going to look at all the tasks from the command-line, and (just for instance), MSBuild is required instead of CallTargets if you just want to call "the default targets" in your build.

      – Jaykul
      Aug 8 '14 at 18:28






      Does that help? I'm pretty sure that as a general rule, you should not care about CallTargets though -- it is part of the nitty-gritty of how a target does whatever it's supposed to do. Why do you care about this particular task? I mean, you're not going to look at all the tasks from the command-line, and (just for instance), MSBuild is required instead of CallTargets if you just want to call "the default targets" in your build.

      – Jaykul
      Aug 8 '14 at 18:28














      To my mind, DependsOnTargets tell me that when I call target A, before it processes anything it will call target B and target C. Whereas CallTargets tell me that when I call target A, sometime during its processing it will call target B and target C. They seem to be equivalently-useful pieces of information. Or is my view of this skewed in some fashion...?

      – Michael Sorens
      Aug 8 '14 at 23:05





      To my mind, DependsOnTargets tell me that when I call target A, before it processes anything it will call target B and target C. Whereas CallTargets tell me that when I call target A, sometime during its processing it will call target B and target C. They seem to be equivalently-useful pieces of information. Or is my view of this skewed in some fashion...?

      – Michael Sorens
      Aug 8 '14 at 23:05













      The thing is that there are lots of ways to do the same thing differently ;) A can DependsOnTargets B or A can After B or B can Before A ... or B can CallTarget A or it can MSBuild Project="$(MSBuildProjectFile)" Targets="A" ... or it could use Exec or there could be third-party tasks ...

      – Jaykul
      Aug 10 '14 at 19:51





      The thing is that there are lots of ways to do the same thing differently ;) A can DependsOnTargets B or A can After B or B can Before A ... or B can CallTarget A or it can MSBuild Project="$(MSBuildProjectFile)" Targets="A" ... or it could use Exec or there could be third-party tasks ...

      – Jaykul
      Aug 10 '14 at 19:51











      2














      I suggest you use PowerShell:



      Select-Xml `
      -XPath //b:Target `
      -Path path-to-build-file `
      -Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
      Select-Object -ExpandProperty Node |
      Format-Table -Property Name, DependsOnTargets -AutoSize


      The XPath query will find all Target elements and display the target name and dependencies in table format. Here is a sample that selects the 10 first targets from Microsoft.Web.Publishing.targets:



      PS C:Program Files (x86)MSBuildMicrosoftVisualStudiov11.0Web> Select-Xml `
      -XPath //b:Target `
      -Path Microsoft.Web.Publishing.targets `
      -Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
      Select-Object -ExpandProperty Node |
      Sort-Object -Property Name |
      Select-Object -First 10 |
      Format-Table -Property Name, DependsOnTargets -AutoSize

      Name DependsOnTargets
      ---- ----------------
      _CheckPublishToolsUpToDate
      _CheckRemoteFx45
      _CleanWPPIfNeedTo
      _DetectDbDacFxProvider
      _WPPCopyWebApplication $(_WPPCopyWebApplicationDependsOn)
      AddContentPathToSourceManifest $(AddContentPathToSourceManifestDepe...
      AddDeclareParametersItems $(AddDeclareParametersItemsDependsOn)
      AddDeclareParametersItemsForContentPath $(AddDeclareParametersItemsForConten...
      AddDeclareParametersItemsForIis6 $(AddDeclareParametersItemsForIis6De...
      AddDeclareParametersItemsForIis7 $(AddDeclareParametersItemsForIis7De...





      share|improve this answer



























      • Very nice! How difficult is it to add a column for the list of values in the immediate child node CallTarget? Some dependencies lie there as well.

        – Michael Sorens
        Apr 17 '13 at 21:32






      • 1





        The problem with that is that it's file based, and won't import the includes, so for an actual project build file, you'll get only a tiny subset of the targets.

        – Jaykul
        Aug 6 '14 at 22:04















      2














      I suggest you use PowerShell:



      Select-Xml `
      -XPath //b:Target `
      -Path path-to-build-file `
      -Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
      Select-Object -ExpandProperty Node |
      Format-Table -Property Name, DependsOnTargets -AutoSize


      The XPath query will find all Target elements and display the target name and dependencies in table format. Here is a sample that selects the 10 first targets from Microsoft.Web.Publishing.targets:



      PS C:Program Files (x86)MSBuildMicrosoftVisualStudiov11.0Web> Select-Xml `
      -XPath //b:Target `
      -Path Microsoft.Web.Publishing.targets `
      -Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
      Select-Object -ExpandProperty Node |
      Sort-Object -Property Name |
      Select-Object -First 10 |
      Format-Table -Property Name, DependsOnTargets -AutoSize

      Name DependsOnTargets
      ---- ----------------
      _CheckPublishToolsUpToDate
      _CheckRemoteFx45
      _CleanWPPIfNeedTo
      _DetectDbDacFxProvider
      _WPPCopyWebApplication $(_WPPCopyWebApplicationDependsOn)
      AddContentPathToSourceManifest $(AddContentPathToSourceManifestDepe...
      AddDeclareParametersItems $(AddDeclareParametersItemsDependsOn)
      AddDeclareParametersItemsForContentPath $(AddDeclareParametersItemsForConten...
      AddDeclareParametersItemsForIis6 $(AddDeclareParametersItemsForIis6De...
      AddDeclareParametersItemsForIis7 $(AddDeclareParametersItemsForIis7De...





      share|improve this answer



























      • Very nice! How difficult is it to add a column for the list of values in the immediate child node CallTarget? Some dependencies lie there as well.

        – Michael Sorens
        Apr 17 '13 at 21:32






      • 1





        The problem with that is that it's file based, and won't import the includes, so for an actual project build file, you'll get only a tiny subset of the targets.

        – Jaykul
        Aug 6 '14 at 22:04













      2












      2








      2







      I suggest you use PowerShell:



      Select-Xml `
      -XPath //b:Target `
      -Path path-to-build-file `
      -Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
      Select-Object -ExpandProperty Node |
      Format-Table -Property Name, DependsOnTargets -AutoSize


      The XPath query will find all Target elements and display the target name and dependencies in table format. Here is a sample that selects the 10 first targets from Microsoft.Web.Publishing.targets:



      PS C:Program Files (x86)MSBuildMicrosoftVisualStudiov11.0Web> Select-Xml `
      -XPath //b:Target `
      -Path Microsoft.Web.Publishing.targets `
      -Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
      Select-Object -ExpandProperty Node |
      Sort-Object -Property Name |
      Select-Object -First 10 |
      Format-Table -Property Name, DependsOnTargets -AutoSize

      Name DependsOnTargets
      ---- ----------------
      _CheckPublishToolsUpToDate
      _CheckRemoteFx45
      _CleanWPPIfNeedTo
      _DetectDbDacFxProvider
      _WPPCopyWebApplication $(_WPPCopyWebApplicationDependsOn)
      AddContentPathToSourceManifest $(AddContentPathToSourceManifestDepe...
      AddDeclareParametersItems $(AddDeclareParametersItemsDependsOn)
      AddDeclareParametersItemsForContentPath $(AddDeclareParametersItemsForConten...
      AddDeclareParametersItemsForIis6 $(AddDeclareParametersItemsForIis6De...
      AddDeclareParametersItemsForIis7 $(AddDeclareParametersItemsForIis7De...





      share|improve this answer















      I suggest you use PowerShell:



      Select-Xml `
      -XPath //b:Target `
      -Path path-to-build-file `
      -Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
      Select-Object -ExpandProperty Node |
      Format-Table -Property Name, DependsOnTargets -AutoSize


      The XPath query will find all Target elements and display the target name and dependencies in table format. Here is a sample that selects the 10 first targets from Microsoft.Web.Publishing.targets:



      PS C:Program Files (x86)MSBuildMicrosoftVisualStudiov11.0Web> Select-Xml `
      -XPath //b:Target `
      -Path Microsoft.Web.Publishing.targets `
      -Namespace @ b = 'http://schemas.microsoft.com/developer/msbuild/2003' |
      Select-Object -ExpandProperty Node |
      Sort-Object -Property Name |
      Select-Object -First 10 |
      Format-Table -Property Name, DependsOnTargets -AutoSize

      Name DependsOnTargets
      ---- ----------------
      _CheckPublishToolsUpToDate
      _CheckRemoteFx45
      _CleanWPPIfNeedTo
      _DetectDbDacFxProvider
      _WPPCopyWebApplication $(_WPPCopyWebApplicationDependsOn)
      AddContentPathToSourceManifest $(AddContentPathToSourceManifestDepe...
      AddDeclareParametersItems $(AddDeclareParametersItemsDependsOn)
      AddDeclareParametersItemsForContentPath $(AddDeclareParametersItemsForConten...
      AddDeclareParametersItemsForIis6 $(AddDeclareParametersItemsForIis6De...
      AddDeclareParametersItemsForIis7 $(AddDeclareParametersItemsForIis7De...






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 16 '12 at 22:38

























      answered Oct 16 '12 at 22:32









      knutknut

      3,6252 gold badges28 silver badges42 bronze badges




      3,6252 gold badges28 silver badges42 bronze badges















      • Very nice! How difficult is it to add a column for the list of values in the immediate child node CallTarget? Some dependencies lie there as well.

        – Michael Sorens
        Apr 17 '13 at 21:32






      • 1





        The problem with that is that it's file based, and won't import the includes, so for an actual project build file, you'll get only a tiny subset of the targets.

        – Jaykul
        Aug 6 '14 at 22:04

















      • Very nice! How difficult is it to add a column for the list of values in the immediate child node CallTarget? Some dependencies lie there as well.

        – Michael Sorens
        Apr 17 '13 at 21:32






      • 1





        The problem with that is that it's file based, and won't import the includes, so for an actual project build file, you'll get only a tiny subset of the targets.

        – Jaykul
        Aug 6 '14 at 22:04
















      Very nice! How difficult is it to add a column for the list of values in the immediate child node CallTarget? Some dependencies lie there as well.

      – Michael Sorens
      Apr 17 '13 at 21:32





      Very nice! How difficult is it to add a column for the list of values in the immediate child node CallTarget? Some dependencies lie there as well.

      – Michael Sorens
      Apr 17 '13 at 21:32




      1




      1





      The problem with that is that it's file based, and won't import the includes, so for an actual project build file, you'll get only a tiny subset of the targets.

      – Jaykul
      Aug 6 '14 at 22:04





      The problem with that is that it's file based, and won't import the includes, so for an actual project build file, you'll get only a tiny subset of the targets.

      – Jaykul
      Aug 6 '14 at 22:04











      0














      Here is the code snippet to get all targets in the order their execution.



       static void Main(string[] args)

      Project project = new Project(@"build.core.xml");
      var orderedTargets = GetAllTargetsInOrderOfExecution(project.Targets, project.Targets["FinalTargetInTheDependencyChain"]).ToList();
      File.WriteAllText(@"orderedTargets.txt", orderedTargets.Select(x => x.Name).Aggregate((a, b) => a + "rn" + b));


      /// <summary>
      /// Gets all targets in the order of their execution by traversing through the dependent targets recursively
      /// </summary>
      /// <param name="allTargetsInfo"></param>
      /// <param name="target"></param>
      /// <returns></returns>
      public static List<ProjectTargetInstance> GetAllTargetsInOrderOfExecution(IDictionary<string, ProjectTargetInstance> allTargetsInfo, ProjectTargetInstance target)

      var orderedTargets = new List<ProjectTargetInstance>();

      var dependentTargets =
      target
      .DependsOnTargets
      .Split(';')
      .Where(allTargetsInfo.ContainsKey)
      .Select(x => allTargetsInfo[x])
      .ToList();

      foreach (var dependentTarget in dependentTargets)

      orderedTargets = orderedTargets.Union(GetAllTargetsInOrderOfExecution(allTargetsInfo, dependentTarget)).ToList();


      orderedTargets.Add(target);

      return orderedTargets;






      share|improve this answer































        0














        Here is the code snippet to get all targets in the order their execution.



         static void Main(string[] args)

        Project project = new Project(@"build.core.xml");
        var orderedTargets = GetAllTargetsInOrderOfExecution(project.Targets, project.Targets["FinalTargetInTheDependencyChain"]).ToList();
        File.WriteAllText(@"orderedTargets.txt", orderedTargets.Select(x => x.Name).Aggregate((a, b) => a + "rn" + b));


        /// <summary>
        /// Gets all targets in the order of their execution by traversing through the dependent targets recursively
        /// </summary>
        /// <param name="allTargetsInfo"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static List<ProjectTargetInstance> GetAllTargetsInOrderOfExecution(IDictionary<string, ProjectTargetInstance> allTargetsInfo, ProjectTargetInstance target)

        var orderedTargets = new List<ProjectTargetInstance>();

        var dependentTargets =
        target
        .DependsOnTargets
        .Split(';')
        .Where(allTargetsInfo.ContainsKey)
        .Select(x => allTargetsInfo[x])
        .ToList();

        foreach (var dependentTarget in dependentTargets)

        orderedTargets = orderedTargets.Union(GetAllTargetsInOrderOfExecution(allTargetsInfo, dependentTarget)).ToList();


        orderedTargets.Add(target);

        return orderedTargets;






        share|improve this answer





























          0












          0








          0







          Here is the code snippet to get all targets in the order their execution.



           static void Main(string[] args)

          Project project = new Project(@"build.core.xml");
          var orderedTargets = GetAllTargetsInOrderOfExecution(project.Targets, project.Targets["FinalTargetInTheDependencyChain"]).ToList();
          File.WriteAllText(@"orderedTargets.txt", orderedTargets.Select(x => x.Name).Aggregate((a, b) => a + "rn" + b));


          /// <summary>
          /// Gets all targets in the order of their execution by traversing through the dependent targets recursively
          /// </summary>
          /// <param name="allTargetsInfo"></param>
          /// <param name="target"></param>
          /// <returns></returns>
          public static List<ProjectTargetInstance> GetAllTargetsInOrderOfExecution(IDictionary<string, ProjectTargetInstance> allTargetsInfo, ProjectTargetInstance target)

          var orderedTargets = new List<ProjectTargetInstance>();

          var dependentTargets =
          target
          .DependsOnTargets
          .Split(';')
          .Where(allTargetsInfo.ContainsKey)
          .Select(x => allTargetsInfo[x])
          .ToList();

          foreach (var dependentTarget in dependentTargets)

          orderedTargets = orderedTargets.Union(GetAllTargetsInOrderOfExecution(allTargetsInfo, dependentTarget)).ToList();


          orderedTargets.Add(target);

          return orderedTargets;






          share|improve this answer















          Here is the code snippet to get all targets in the order their execution.



           static void Main(string[] args)

          Project project = new Project(@"build.core.xml");
          var orderedTargets = GetAllTargetsInOrderOfExecution(project.Targets, project.Targets["FinalTargetInTheDependencyChain"]).ToList();
          File.WriteAllText(@"orderedTargets.txt", orderedTargets.Select(x => x.Name).Aggregate((a, b) => a + "rn" + b));


          /// <summary>
          /// Gets all targets in the order of their execution by traversing through the dependent targets recursively
          /// </summary>
          /// <param name="allTargetsInfo"></param>
          /// <param name="target"></param>
          /// <returns></returns>
          public static List<ProjectTargetInstance> GetAllTargetsInOrderOfExecution(IDictionary<string, ProjectTargetInstance> allTargetsInfo, ProjectTargetInstance target)

          var orderedTargets = new List<ProjectTargetInstance>();

          var dependentTargets =
          target
          .DependsOnTargets
          .Split(';')
          .Where(allTargetsInfo.ContainsKey)
          .Select(x => allTargetsInfo[x])
          .ToList();

          foreach (var dependentTarget in dependentTargets)

          orderedTargets = orderedTargets.Union(GetAllTargetsInOrderOfExecution(allTargetsInfo, dependentTarget)).ToList();


          orderedTargets.Add(target);

          return orderedTargets;







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 24 '17 at 17:55

























          answered Apr 13 '17 at 19:58









          Krishna DesirajuKrishna Desiraju

          281 silver badge6 bronze badges




          281 silver badge6 bronze badges
























              0














              If you build from command-line you can generate a metaproject file by setting an environment variable before building. This file will detail everything that a solution-file will target as well as outlining their target order.



              It's not really a read-friendly diagnostic file but does have all the information you likely need in XML format.



              E.g. from CMD and a typical VS2017 installation; replace line 1 with whatever build-tool-loader you use



              call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
              set MSBuildEmitSolution=1
              call MSBuild SolutionFile.sln /t:rebuild


              For a real example, I've made an SLN file starting with a C# project called mixed_proj, so I've got mixed_proj.sln, mixed_proj.csproj, then the two C++ projects I added, ConsoleApplication1 and DLL1.



              I've set the build dependency order to where ConsoleApplication1 only builds after DLL1



              Here is the command-line for building it:



              call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
              set MSBuildEmitSolution=1
              call MSBuild miced_proj.sln /t:rebuild


              Here is the generated metaproject file content (too big to paste here)






              share|improve this answer





























                0














                If you build from command-line you can generate a metaproject file by setting an environment variable before building. This file will detail everything that a solution-file will target as well as outlining their target order.



                It's not really a read-friendly diagnostic file but does have all the information you likely need in XML format.



                E.g. from CMD and a typical VS2017 installation; replace line 1 with whatever build-tool-loader you use



                call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
                set MSBuildEmitSolution=1
                call MSBuild SolutionFile.sln /t:rebuild


                For a real example, I've made an SLN file starting with a C# project called mixed_proj, so I've got mixed_proj.sln, mixed_proj.csproj, then the two C++ projects I added, ConsoleApplication1 and DLL1.



                I've set the build dependency order to where ConsoleApplication1 only builds after DLL1



                Here is the command-line for building it:



                call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
                set MSBuildEmitSolution=1
                call MSBuild miced_proj.sln /t:rebuild


                Here is the generated metaproject file content (too big to paste here)






                share|improve this answer



























                  0












                  0








                  0







                  If you build from command-line you can generate a metaproject file by setting an environment variable before building. This file will detail everything that a solution-file will target as well as outlining their target order.



                  It's not really a read-friendly diagnostic file but does have all the information you likely need in XML format.



                  E.g. from CMD and a typical VS2017 installation; replace line 1 with whatever build-tool-loader you use



                  call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
                  set MSBuildEmitSolution=1
                  call MSBuild SolutionFile.sln /t:rebuild


                  For a real example, I've made an SLN file starting with a C# project called mixed_proj, so I've got mixed_proj.sln, mixed_proj.csproj, then the two C++ projects I added, ConsoleApplication1 and DLL1.



                  I've set the build dependency order to where ConsoleApplication1 only builds after DLL1



                  Here is the command-line for building it:



                  call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
                  set MSBuildEmitSolution=1
                  call MSBuild miced_proj.sln /t:rebuild


                  Here is the generated metaproject file content (too big to paste here)






                  share|improve this answer













                  If you build from command-line you can generate a metaproject file by setting an environment variable before building. This file will detail everything that a solution-file will target as well as outlining their target order.



                  It's not really a read-friendly diagnostic file but does have all the information you likely need in XML format.



                  E.g. from CMD and a typical VS2017 installation; replace line 1 with whatever build-tool-loader you use



                  call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
                  set MSBuildEmitSolution=1
                  call MSBuild SolutionFile.sln /t:rebuild


                  For a real example, I've made an SLN file starting with a C# project called mixed_proj, so I've got mixed_proj.sln, mixed_proj.csproj, then the two C++ projects I added, ConsoleApplication1 and DLL1.



                  I've set the build dependency order to where ConsoleApplication1 only builds after DLL1



                  Here is the command-line for building it:



                  call "%ProgramFiles(x86)%Microsoft Visual Studio2017EnterpriseCommon7ToolsVsDevCmd.bat"
                  set MSBuildEmitSolution=1
                  call MSBuild miced_proj.sln /t:rebuild


                  Here is the generated metaproject file content (too big to paste here)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 26 at 21:20









                  kayleeFrye_onDeckkayleeFrye_onDeck

                  3,6262 gold badges31 silver badges50 bronze badges




                  3,6262 gold badges31 silver badges50 bronze badges






























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f441614%2fhow-to-query-msbuild-file-for-list-of-supported-targets%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript