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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With