Copy all files and folders using msbuild

后端 未结 10 1810
感动是毒
感动是毒 2020-11-30 22:22

Just wondering if someone could help me with some msbuild scripts that I am trying to write. What I would like to do is copy all the files and sub folders from a folder to

相关标签:
10条回答
  • 2020-11-30 22:35
    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
        <PropertyGroup>
            <YourDestinationDirectory>..\SomeDestinationDirectory</YourDestinationDirectory>
            <YourSourceDirectory>..\SomeSourceDirectory</YourSourceDirectory>
        </PropertyGroup>
    
        <Target Name="BeforeBuild">
            <CreateItem Include="$(YourSourceDirectory)\**\*.*">
                <Output TaskParameter="Include" ItemName="YourFilesToCopy" />
            </CreateItem>
    
            <Copy SourceFiles="@(YourFilesToCopy)"
                    DestinationFiles="$(YourFilesToCopy)\%(RecursiveDir)" />
        </Target>
    </Project>
    

    \**\*.* help to get files from all the folder. RecursiveDir help to put all the file in the respective folder...

    0 讨论(0)
  • 2020-11-30 22:36

    I'm kinda new to MSBuild but I find the EXEC Task handy for situation like these. I came across the same challenge in my project and this worked for me and was much simpler. Someone please let me know if it's not a good practice.

    <Target Name="CopyToDeployFolder" DependsOnTargets="CompileWebSite">
        <Exec Command="xcopy.exe  $(OutputDirectory) $(DeploymentDirectory) /e" WorkingDirectory="C:\Windows\" />
    </Target>
    
    0 讨论(0)
  • 2020-11-30 22:40

    I think the problem might be in how you're creating your ItemGroup and calling the Copy task. See if this makes sense:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
        <PropertyGroup>
            <YourDestinationDirectory>..\SomeDestinationDirectory</YourDestinationDirectory>
            <YourSourceDirectory>..\SomeSourceDirectory</YourSourceDirectory>
        </PropertyGroup>
    
        <Target Name="BeforeBuild">
            <CreateItem Include="$(YourSourceDirectory)\**\*.*">
                <Output TaskParameter="Include" ItemName="YourFilesToCopy" />
            </CreateItem>
    
            <Copy SourceFiles="@(YourFilesToCopy)"
                    DestinationFiles="@(YourFilesToCopy->'$(YourDestinationDirectory)\%(RecursiveDir)%(Filename)%(Extension)')" />
        </Target>
    </Project>
    
    0 讨论(0)
  • 2020-11-30 22:44

    I was searching help on this too. It took me a while, but here is what I did that worked really well.

    <Target Name="AfterBuild">
        <ItemGroup>
            <ANTLR Include="..\Data\antlrcs\**\*.*" />
        </ItemGroup>
        <Copy SourceFiles="@(ANTLR)" DestinationFolder="$(TargetDir)\%(RecursiveDir)" SkipUnchangedFiles="true" />
    </Target>
    

    This recursively copied the contents of the folder named antlrcs to the $(TargetDir).

    0 讨论(0)
  • 2020-11-30 22:51

    Did you try to specify concrete destination directory instead of

    DestinationFolder="@(Viewer->'$(OutputPath)\\Tools')" ? 
    

    I'm not very proficient with advanced MSBuild syntax, but

    @(Viewer->'$(OutputPath)\\Tools') 
    

    looks weird to me. Script looks good, so the problem might be in values of $(ApplicationDirectory) and $(OutputPath)

    EDIT:

    Here is a blog post that might be useful:

    How To: Recursively Copy Files Using the Task

    0 讨论(0)
  • 2020-11-30 22:51

    If you are working with typical C++ toolchain, another way to go is to add your files into standard CopyFileToFolders list

    <ItemGroup>
      <CopyFileToFolders Include="materials\**\*">
        <DestinationFolders>$(MainOutputDirectory)\Resources\materials\%(RecursiveDir)</DestinationFolders>
      </CopyFileToFolders>
    </ItemGroup>
    

    Besides being simple, this is a nice way to go because CopyFilesToFolders task will generate appropriate inputs, outputs and even TLog files therefore making sure that copy operations will run only when one of the input files has changed or one of the output files is missing. With TLog, Visual Studio will also properly recognize project as "up to date" or not (it uses a separate U2DCheck mechanism for that).

    0 讨论(0)
提交回复
热议问题