Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access current Project context within a custom Task?

Tags:

c#

.net

msbuild

How do I access current Project context within a custom Task in MSBuild? At first it appeared as though the GlobalProjectCollection reference on ProjectCollection would allow access, but this appears to be empty, at least when running MSBuild from the commandline.

I can currently get new Project instance based off of the current project file in the following manner:

List projectAssemblies = new List();
using (XmlReader projectFileReader =
    XmlReader.Create(this.BuildEngine.ProjectFileOfTaskNode))
{
    Project project = new Project(projectFileReader);
    foreach (ProjectItem item in project.AllEvaluatedItems)
    {
      ... woo hoo ...
    }
}

but it just seems like a lot of trouble. How can I just get access to the project from which my task is invoked?

like image 826
bojolais Avatar asked May 06 '12 22:05

bojolais


1 Answers

You can not. MSBuild was specifically designed so that individual tasks only have access to the parameters that you explicitly passed to the task, and nothing else. That makes MSBuild files easier to read, now that you know that each task is only affected by what your specidied when you called this task.

The code you have is not creating a copy of the project, it is creating a new instance, so you should not expect properties to be the same. For example an instance of the currently executing project (from where your task is called from) could have properties overriden from command line or changed along the way your project is built, while your newly created project would have a default values of the properties after first MSBuild engine pass through the file.

like image 59
seva titov Avatar answered Sep 28 '22 06:09

seva titov