如何让 MSBuild 将标记为内容的所有文件复制到文件夹中,同时保留文件夹结构?

发布于 2024-12-07 15:18:30 字数 238 浏览 0 评论 0原文

作为我的解决方案构建的一部分,我想将所有“内容”文件(asp?x 等)复制到另一个文件夹。由于这些在项目中标记得如此清晰,我认为必须有一种简单的方法来复制这些,而不是使用 xcopy 编写我自己的构建后步骤。 不幸的是我一直无法弄清楚这一点 - 这个 msbuild 的东西与我的大脑不兼容。 我只想迈出一步 但无法弄清楚要使用的语法。

Bat 文件语法建议不能回答这个问题 - 仅适用纯 msbuild 解决方案

谢谢, 每

As part of my solution build, I want to copy all "Content" files (asp?x etc) to another folder. Since these are so clearly tagged in the project, I thought there must be an easy way to copy these instead of writing my own post-build step with xcopy.
Unfortunately I haven't been able to figure this out - this msbuild thing is incompatible with my brain.
I just want a step like

but can't figure out the syntax to use.

Bat file syntax suggestions would not be an answer to this question - only pure msbuild solutions apply

Thanks,
Per

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恍梦境° 2024-12-14 15:18:30

您可以通过以下方式轻松完成此操作:

<PropertyGroup>
  <DestFolder>..\Copy\</DestFolder>
</PropertyGroup>

<Target Name="CopyContentFiles">
  <Copy SourceFiles="@(Content)"
        DestinationFiles="@(Content->'$(DestFolder)%(RelativeDir)%(Filename)%(Extension)')"/>
</Target>

如果您想将其作为构建后步骤执行,那么您只需添加 AfterTargets="Build" 例如:

<PropertyGroup>
  <DestFolder>..\Copy\</DestFolder>
</PropertyGroup>

<Target Name="CopyContentFiles" AfterTargets="Build">
  <Copy SourceFiles="@(Content)"
        DestinationFiles="@(Content->'$(DestFolder)%(RelativeDir)%(Filename)%(Extension)')"/>
</Target>

You can easily do this by the following:

<PropertyGroup>
  <DestFolder>..\Copy\</DestFolder>
</PropertyGroup>

<Target Name="CopyContentFiles">
  <Copy SourceFiles="@(Content)"
        DestinationFiles="@(Content->'$(DestFolder)%(RelativeDir)%(Filename)%(Extension)')"/>
</Target>

If you want to execute this as a post-build steps then you can just add AfterTargets="Build" for example:

<PropertyGroup>
  <DestFolder>..\Copy\</DestFolder>
</PropertyGroup>

<Target Name="CopyContentFiles" AfterTargets="Build">
  <Copy SourceFiles="@(Content)"
        DestinationFiles="@(Content->'$(DestFolder)%(RelativeDir)%(Filename)%(Extension)')"/>
</Target>
梦罢 2024-12-14 15:18:30

我使用 Web 部署功能来打包所有内容文件,然后可以使用 Web 部署与站点同步,或者如果无法选择 Web 部署,则可以使用 xcopy 或 RoboCopy。

RoboCopy 任务包含在 MSBuild 社区任务中。

<PropertyGroup>
    <Configuration>Release</Configuration>
    <PackageDir>$(MSBuildProjectDirectory)\obj\$(Configuration)\Package\PackageTmp</PackageDir>
    <ServerPath>\\server\path</ServerPath>
    <MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\...</MSBuildCommunityTasksPath>
</PropertyGroup>    

<ItemGroup>
    <Project Include="WebApplication.csproj"/>
</ItemGroup>

<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />

<Target Name="Deploy">
    <MSBuild Projects="@(Project)" Targets="Build;Package" Properties="Configuration=$(Configuration)"/>
    <RoboCopy
        SourceFolder="@(PackageDir)"
        DestinationFolder="$(ServerPath)"
        Subdirectories="true"
        Mirror="true"
        />
</Target>

I use the web deploy feature to package all content files up and then I can use web deploy to sync with a site or use xcopy or rather RoboCopy if web deploy is not an option.

The RoboCopy task is included in MSBuild Community Tasks.

<PropertyGroup>
    <Configuration>Release</Configuration>
    <PackageDir>$(MSBuildProjectDirectory)\obj\$(Configuration)\Package\PackageTmp</PackageDir>
    <ServerPath>\\server\path</ServerPath>
    <MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\...</MSBuildCommunityTasksPath>
</PropertyGroup>    

<ItemGroup>
    <Project Include="WebApplication.csproj"/>
</ItemGroup>

<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />

<Target Name="Deploy">
    <MSBuild Projects="@(Project)" Targets="Build;Package" Properties="Configuration=$(Configuration)"/>
    <RoboCopy
        SourceFolder="@(PackageDir)"
        DestinationFolder="$(ServerPath)"
        Subdirectories="true"
        Mirror="true"
        />
</Target>
可可 2024-12-14 15:18:30

Sayed 的答案对我不起作用,因为它还保留了原始的父目录。相反,我使用了以下修改后的版本,它工作得非常漂亮,而且更加优雅!

<PropertyGroup>
  <DestFolder>..\Copy\</DestFolder>
</PropertyGroup>

<Target Name="CopyContentFiles">
  <Copy SourceFiles="@(Content)"
        DestinationFiles="$(DestFolder)\%(RecursiveDir)"/>
</Target>

The answer by Sayed didn't work for me because it also retained the original parent directory. I instead used the following altered version and it worked beautifully and is a bit more elegant!

<PropertyGroup>
  <DestFolder>..\Copy\</DestFolder>
</PropertyGroup>

<Target Name="CopyContentFiles">
  <Copy SourceFiles="@(Content)"
        DestinationFiles="$(DestFolder)\%(RecursiveDir)"/>
</Target>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文