Использование MSBuild 2.0 / 3.5: пользовательская задача
Вы можете написать пользовательскую задачу msbuild, например:
using System;
using System.Collections.Generic;
using Microsoft.Build.BuildEngine;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace MSBuildTasks
{
public class GetAllTargets : Task
{
[Required]
public String ProjectFile { get; set; }
[Output]
public ITaskItem[] Targets { get; set; }
public override bool Execute()
{
var project = new Project(BuildEngine as Engine);
project.Load(ProjectFile);
var taskItems = new List<ITaskItem>(project.Targets.Count);
foreach (Target target in project.Targets)
{
var metadata = new Dictionary<string, string>
{
{"Condition", target.Condition},
{"Inputs", target.Inputs},
{"Outputs", target.Outputs},
{"DependsOnTargets", target.DependsOnTargets}
};
taskItems.Add(new TaskItem(target.Name, metadata));
}
Targets = taskItems.ToArray();
return true;
}
}
}
, которую вы будете использовать следующим образом:
<Target Name="TestGetAllTargets">
<GetAllTargets ProjectFile="$(MSBuildProjectFile)">
<Output ItemName="TargetItems" TaskParameter="Targets"/>
</GetAllTargets>
<Message Text="Name: %(TargetItems.Identity) Input: %(TargetItems.Input) --> Output: %(TargetItems.Output)"/>
</Target>
Использование MSBuild 4.0: встроенная задача
С MSBuild 4 вы можете использовать новую блестящую вещь: встроенную задачу.Встроенное задание позволяет вам определять поведение непосредственно в файле msbuild.
<UsingTask TaskName="GetAllTargets"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<ProjectFile ParameterType="System.String" Required="true"/>
<TargetsOut ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true"/>
</ParameterGroup>
<Task>
<Reference Include="System.Xml"/>
<Reference Include="Microsoft.Build"/>
<Reference Include="Microsoft.Build.Framework"/>
<Using Namespace="Microsoft.Build.Evaluation"/>
<Using Namespace="Microsoft.Build.Execution"/>
<Using Namespace="Microsoft.Build.Utilities"/>
<Using Namespace="Microsoft.Build.Framework"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
var project = new Project(ProjectFile);
var taskItems = new List<ITaskItem>(project.Targets.Count);
foreach (KeyValuePair<string, ProjectTargetInstance> kvp in project.Targets)
{
var target = kvp.Value;
var metadata = new Dictionary<string, string>
{
{"Condition", target.Condition},
{"Inputs", target.Inputs},
{"Outputs", target.Outputs},
{"DependsOnTargets", target.DependsOnTargets}
};
taskItems.Add(new TaskItem(kvp.Key, metadata));
}
TargetsOut = taskItems.ToArray();
]]>
</Code>
</Task>
</UsingTask>
<Target Name="Test">
<GetAllTargets ProjectFile="$(MSBuildProjectFile)">
<Output ItemName="TargetItems" TaskParameter="TargetsOut"/>
</GetAllTargets>
<Message Text="%(TargetItems.Identity)"/>
</Target>