MSBuild:导入项目的输出属性

时间:2015-02-27 21:14:56

标签: msbuild

假设我有build.proj这样的话:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0"
         DefaultTargets="AfterBuild"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <PropertyGroup>
        <CustomAfterMicrosoftCSharpTargets>$(MSBuildThisFileDirectory)Common.Build.targets</CustomAfterMicrosoftCSharpTargets>
        <Configuration>Release</Configuration>
        <Platform>Any CPU</Platform>
        <ProjectProperties>
            Configuration=$(Configuration);
            Platform=$(Platform);
            CustomAfterMicrosoftCSharpTargets=$(CustomAfterMicrosoftCSharpTargets);
        </ProjectProperties>
    </PropertyGroup>

    <ItemGroup>
        <ProjectToBuild Include="$(MSBuildThisFileDirectory)src\Proj\MyApp.csproj" />
    </ItemGroup>

    <Target Name="Build">
        <MSBuild Targets="Build"
                 Projects="@(ProjectToBuild)"
                 Properties="$(ProjectProperties)" />
    </Target>

    <Target Name="AfterBuild" DependsOn="Build">
        <Message Text="ChildProperty: $(ChildProperty)" />
    </Target>
</Project>

Common.Build.targets中,我有Target创建了一个属性:

<Target Name="DoSomethingUseful">
    <!-- Do something useful -->
    <CreateProperty Value="SomeComputedThingy">
        <Output TaskParameter="Value" PropertyName="ChildProperty"/>
    </CreateProperty>
</Target>

现在,如果我构建build.proj,我在消息中看不到ChildProperty的值。输出为空白:ChildProperty:

我的印象是,目标的任何输出在执行后都会合并回全局上下文。但它似乎只适用于目标文件中 内的任何内容。

如何让ChildProperty冒泡到父build.proj

1 个答案:

答案 0 :(得分:0)

当您在依赖项目上调用<MSBuild>任务时,请阅读任务的TargetOutputs输出参数。请参阅MSDN中的示例:

<Target Name="BuildOtherProjects">
    <MSBuild
        Projects="@(ProjectReferences)"
        Targets="Build">
        <Output
            TaskParameter="TargetOutputs"
            ItemName="AssembliesBuiltByChildProjects" />
    </MSBuild>
</Target>

您还需要确保在依赖项目中调用的目标正确填充ReturnsOutput参数(Returns优先使用)。 E.g:

<Target Name="MyTarget" Inputs="..." Outputs="..." Returns="$(MyOutputValue)">
    <PropertyGroup>
        <MyOutputValue>set it here</MyOutputValue>
    </PropertyGroup>
</Target>