MSBuild:导入项目的输出属性 [英] MSBuild: Output properties from imported projects

查看:94
本文介绍了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:

In Common.Build.targets, I have a Target that creates a property:

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

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

Now if I build build.proj, I do not see the value of ChildProperty in the message. The output is blank: ChildProperty:.

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

I was under the impression that any output for a target is merged back to global context after its execution. But it seems that it only applies to anything within that target file.

如何使ChildProperty气泡上升到父级build.proj?

How do I make ChildProperty bubble up to the parent build.proj?

推荐答案

在从属项目上调用<MSBuild>任务时,请读取任务的TargetOutputs输出参数.请参见 MSDN 中的示例:

When you are calling <MSBuild> task on dependent projects, read TargetOutputs output parameter of the task. See example from MSDN:

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

您还需要确保在从属项目中调用的目标正确填充ReturnsOutput参数(如果使用Returns,则优先使用).例如:

You will also need to ensure the target you are calling in dependent projects correctly populates Returns or Output parameter (Returns takes precedence if used). E.g.:

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

这篇关于MSBuild:导入项目的输出属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆