2
votes

Working with MSBuild and I need to copy the transform configuration file to various subdirectories.

I have code below within the body of my target

  <ItemGroup>
    <EnvironmentDirectory  Include="Environment\BackUp_Recovery\"/>
    <EnvironmentDirectory Include="Environment\IST\"/>
    <EnvironmentDirectory Include="Environment\Production\"/>
    <EnvironmentDirectory Include="Environment\UAT\"/>
  </ItemGroup>

<!-- Copy task to copy file to environment folders -->

 <Copy SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config"
          DestinationFiles="@(EnvironmentDirectory -> '@(EnvironmentDirectory)$(TargetFileName).config')"
            SkipUnchangedFiles="true">
      <Output TaskParameter="CopiedFiles" ItemName="SuccessfullyCopiedFiles" />
    </Copy>

As is, I get the following error message.

"DestinationFiles" refers to 4 item(s), and "SourceFiles" refers to 1 item(s). They must have the same number of items.

And when I look at output window, I observe the following

2>Task "Copy" (TaskId:73) 2> Task Parameter:SourceFiles=obj\Release\AIT.UI.WinForm.exe.config (TaskId:73) 2> Task Parameter: 2> DestinationFiles= 2> @(EnvironmentDirectory)AIT.UI.WinForm.exe.config 2> @(EnvironmentDirectory)AIT.UI.WinForm.exe.config 2> @(EnvironmentDirectory)AIT.UI.WinForm.exe.config 2> @(EnvironmentDirectory)AIT.UI.WinForm.exe.config (TaskId:73) 2> Task Parameter:SkipUnchangedFiles=True (TaskId:73) 2>C:\AITDevelopment\AIT.UI.WinForm\AIT.UI.WinForm.csproj(634,5): error MSB3094: "DestinationFiles" refers to 4 item(s), and "SourceFiles" refers to 1 item(s). They must have the same number of items. 2>Done executing task "Copy" -- FAILED. (TaskId:73)

I would appreciate any help in resolving this please. Thanks.

1
Pay close attention to the difference between %(item.metadata) and @(item->'%(metadata)') - Nicodemeus

1 Answers

0
votes

The below will copy a single source file...to multiple locations, based on ItemGroup.

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

    <PropertyGroup>
        <WorkingCheckout>.</WorkingCheckout>
    </PropertyGroup>

    <Target Name="AllTargetsWrapper">
        <CallTarget Targets="CopyItTarget" />
    </Target>

    <ItemGroup>
        <EnvironmentDirectory  Include=".\Environment\BackUp_Recovery\"/>
        <EnvironmentDirectory Include=".\Environment\IST\"/>
        <EnvironmentDirectory Include=".\Environment\Production\"/>
        <EnvironmentDirectory Include=".\Environment\UAT\"/>
    </ItemGroup>    

    <PropertyGroup>
        <TargetFileName>system</TargetFileName>
    </PropertyGroup>    

    <Target Name="CopyItTarget">

    <Exec Command="echo %(EnvironmentDirectory.Identity)"/>

        <Copy SourceFiles="c:\windows\system.ini" 
         DestinationFiles="%(EnvironmentDirectory.Identity)$(TargetFileName).config"

        />  

    </Target>
</Project>