MSBuild - how to copy files that may or may not exist?

余生长醉 提交于 2019-12-20 10:34:24

问题


I have a situation where I need to copy a few specific files in a MSBuild script, but they may or may not exist. If they don't exist it's fine, I don't need them then. But the standard <copy> task throws an error if it cannot find each and every item in the list...


回答1:


Use the Exists condition on Copy task.

<CreateItem Include="*.xml">
  <Output ItemName="ItemsThatNeedToBeCopied" TaskParameter="Include"/>
</CreateItem>

<Copy SourceFiles="@(ItemsThatNeedToBeCopied)"
      DestinationFolder="$(OutputDir)"
      Condition="Exists('%(RootDir)%(Directory)%(Filename)%(Extension)')"/>



回答2:


The easiest would be to use the ContinueOnError flag http://msdn.microsoft.com/en-us/library/7z253716.aspx

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <ItemGroup>
        <MySourceFiles Include="a.cs;b.cs;c.cs"/>
    </ItemGroup>

    <Target Name="CopyFiles">
        <Copy
            SourceFiles="@(MySourceFiles)"
            DestinationFolder="c:\MyProject\Destination"
            ContinueOnError="true"
        />
    </Target>

</Project>

But if something else is wrong you will not notice it. So the condition exist from madgnome's answer would be better.




回答3:


It looks like you can mark MySourceFiles as SkipUnchangedFiles="true" and it won't copy the files if they already exist.



来源:https://stackoverflow.com/questions/511829/msbuild-how-to-copy-files-that-may-or-may-not-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!