MSBuild RecursiveDir is empty

2019-08-30 23:38发布

问题:

I'm trying to make a custom MSBuild script to build all solutions in our repository:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <SourceHome Condition=" '$(SourceHome)'=='' ">..\</SourceHome>
        <ToolsHome Condition=" '$(ToolsHome)'=='' ">.\Tools\</ToolsHome>
        <Configuration Condition=" '$(Configuration)'=='' ">Release</Configuration>
        <DestFolder Condition=" '$(DestFolder)'=='' ">.\$(Configuration)\</DestFolder>
    </PropertyGroup>

    <ItemGroup>
        <AllSolutions Include="$(SourceHome)**\*.sln"/>
    </ItemGroup>

    <Target Name="Default" DependsOnTargets="Clean;Build;Assemble"/>

    <Target Name="Clean">
        <Message Text="Cleaning projects..."/>
        <MSBuild Projects="@(AllSolutions)"
             Targets="Clean"
             Properties="Configuration=$(Configuration);"/>
    </Target>

    <Target Name="RestorePackages">
        <Message Text="Restoring packages..."/>
        <Exec Command="echo y| &quot;$(ToolsHome)NuGet\NuGet.exe&quot; restore &quot;%(AllSolutions.Identity)&quot;"/>
    </Target>

    <Target Name="Build" DependsOnTargets="RestorePackages">
        <Message Text="Building projects..."/>
        <MSBuild Projects="@(AllSolutions)"
                 ContinueOnError="true"
                 Properties="Configuration=$(Configuration)">
            <Output ItemName="OutputFiles" TaskParameter="TargetOutputs"/>
        </MSBuild>
    </Target>

    <Target Name="Assemble">
        <Message Text="Assembling output..."/>
        <RemoveDir Directories="$(DestFolder)"/>
        <Copy SourceFiles="@(OutputFiles)"
              DestinationFiles="@(OutputFiles->'$(DestFolder)%(RecursiveDir)%(Filename)%(Extension)')"/>
    </Target>
</Project>

The script works well except for the last copy task where %(RecursiveDir) evaluates to an empty string, putting all files in the root destination folder... I can't see what I'm doing wrong here.

I found MSBuild ITaskItem RecursiveDir metadata disappears but it doesn't seem to apply here...

I also found this and this thread but there are no double slashes or braces in any paths. Heres an example of the output during the copy task:

Copying file from "x:\my\repo\SolutionDir\ProjectDir\bin\Release\Example.dll" to ".\Release\Example.dll".

回答1:

Even though the AllSolutions items have RecursiveDir metadata populated because of the ** wildcard in your code, the MSBuild task is not populating RecursiveDir in the OutputFiles items. The TargetOutputs are placed into a single array, with full paths but no RecursiveDir metadata.