Animesh D
Animesh D

Reputation: 5002

Copying entire project structure into another directory using afterbuild task

I want to use the AfterBuild target for copying the entire project structure into a separate folder. This is the project structure

TANKProject  
|
|__TANK.CLI
|__TANK.Core
|__TANK.Web 

I want to copy the entire project structure including bin folder and the .sln folder into a location B:\animesh\repos.

To do that, I put the following snippet in the AfterBuild target in each .csproj file:

<ItemGroup>
    <_CustomFiles Include="..\TANK.ProjectName\*.*" />
</ItemGroup>
<Copy
    SourceFiles="@(_CustomFiles)"
    DestinationFiles="B:\repos\TANK"
    SkipUnchangedFiles="true" />

I get the following error while building the project:

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

What am I missing here?


Solution

Based on AdrianMonk's answer, I changed my AfterBuild configuration like this:

  <Target Name="AfterBuild">
    <ItemGroup>
      <_CustomFiles Include="..\**" />
    </ItemGroup>
    <Copy 
      SourceFiles="@(_CustomFiles)" 
      DestinationFiles="@(_CustomFiles->'B:\repos\TANK\%(RecursiveDir)%(Filename)%(Extension)')" 
      SkipUnchangedFiles="true" />
  </Target>

I put this AfterBuild target in only one of the projects since that would be sufficient.

Upvotes: 0

Views: 756

Answers (1)

adrianbanks
adrianbanks

Reputation: 82934

You need to specify DestinationFolder, not DestinationFiles:

<Copy
    SourceFiles="@(_CustomFiles)"
    DestinationFolder="B:\repos\TANK"
    SkipUnchangedFiles="true" />

However, this will only copy the files from the root of you project structure. To copy the whole structure including files in subdirectories, you need to use:

<Copy
    SourceFiles="@(_CustomFiles)"
    DestinationFiles="@(_CustomFiles->'B:\repos\TANK\%(RecursiveDir)%(Filename)%(Extension)')"
    SkipUnchangedFiles="true" />

This will do a recursive copy of the entire project structure (also including the output directories). For more information, refer to the MSBuild Copy task reference docs.

Upvotes: 2

Related Questions