我需要使用属性调用msbuild任务,什么名称只能在运行时计算。我尝试通过这个脚本
来做main.xml中
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0" DefaultTargets="Build">
<UsingTask TaskName="GetVars" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Result ParameterType="System.String" Output="true"/>
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
this.Result = "AAA=123;BBB=456;";
]]>
</Code>
</Task>
</UsingTask>
<PropertyGroup>
<Vars></Vars>
</PropertyGroup>
<Target Name="Make">
<GetVars>
<Output TaskParameter="Result" PropertyName="Vars"/>
</GetVars>
<MSBuild Projects="Proj.xml" Targets="make" Properties="$(Vars)"/>
</Target>
</Project>
Proj.xml
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Target Name="Make">
<Message Text="AAA = $(AAA)"/>
<Message Text="BBB = $(BBB)"/>
</Target>
</Project>
此脚本提供此输出:
AAA = 123;BBB=456;
BBB =
我期待这个输出:
AAA = 123;
BBB = 456;
答案 0 :(得分:1)
如果您希望内联任务生成多个项目(msbuild等效于其他语言的数组或列表),您应该这样说,而不是使用属性(这是一个键/值对)。一些内联任务documentation涵盖了这一点 - 但是它使用了完整的ITaskItems,而只使用String数组就可以了。所以:
System.String[]
ItemName =
@()
表示法将项目传递给MSbuild任务(无论如何都是它所期望的)在代码中:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0" DefaultTargets="Make">
<UsingTask TaskName="GetVars" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Result ParameterType="System.String[]" Output="true"/>
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
this.Result = new System.String[]{"AAA=123", "BBB=456"};
]]>
</Code>
</Task>
</UsingTask>
<Target Name="Make">
<GetVars>
<Output TaskParameter="Result" ItemName="Vars"/>
</GetVars>
<MSBuild Projects="$(MSBuildThisFile)" Targets="Show" Properties="@(Vars)"/>
</Target>
<Target Name="Show">
<Message Text="AAA = $(AAA)"/>
<Message Text="BBB = $(BBB)"/>
</Target>
</Project>
输出:
Show:
AAA = 123
BBB = 456