
Executing a Task from a Project File
Before executing a task in your project file, you must first map the type in the assembly that implements the task to the task name with the UsingTask element. This lets MSBuild know where to look for the execution logic of your task when it finds it in your project file.
To execute a task in an MSBuild project file, create an element with the name of the task as a child of a Target element. If a task accepts parameters, these are passed as attributes of the element.
MSBuild item collections and properties can be used as parameters. For example, the following code calls the MakeDir task and sets the value of the Directories property of the MakeDir object equal to the value of the BuildDir property declared in the previous example.
<Target Name="MakeBuildDirectory">
<MakeDir
Directories="$(BuildDir)" />
</Target>
Tasks can also return information to the project file, which can be stored in items or properties for later use. For example, the following code calls the Copy task and stores the information from the CopiedFiles output property in the SuccessfullyCopiedFiles item collection.
<Target Name="CopyFiles">
<Copy
SourceFiles="@(MySourceFiles)"
DestinationFolder="@(MyDestFolder)">
<Output
TaskParameter="CopiedFiles"
ItemName="SuccessfullyCopiedFiles"/>
</Copy>
</Target>