MSBuild - How to copy files based on condition

Viewed 1628

Using MSBuild how do i copy a file based on configuration of the build i.e. if the build configuration is Staging vs Release. I need to be able to copy FolderA\FileA if the build configuration is Staging and FolderB\FileB for production. Also the file when it gets copied should be at the root of the output directory instead in a folder as FolderA or FolderB. Using task is throwing errors during project load.

1 Answers

MSBuild - How to copy files based on condition

You can use Condition=" '$(Configuration)' == 'xxx' " for your copy task in the project file.

To accomplish this, unload your project. Then at the very end of the project, just before the end-tag </Project>, place below scripts:

  <ItemGroup>
    <MySourceFilesA Include="FolderA\FileA.txt" />
    <MySourceFilesB Include="FolderB\FileB.txt" />
  </ItemGroup>

  <Target Name="CopyFiles" AfterTargets="Build">
    <Copy SourceFiles="@(MySourceFilesA)" DestinationFolder="$(OutDir)" Condition=" '$(Configuration)' == 'Staging' " />
    <Copy SourceFiles="@(MySourceFilesB)" DestinationFolder="$(OutDir)" Condition=" '$(Configuration)' == 'Release' " /> 
  </Target>

You can modify the paht of SourceFiles and DestinationFolder according to your needs, just make sure the path is correct.

Hope this helps.

Related