开发者

How can I get MSBuild to copy all files marked as Content to a folder, preserving folder structure?

开发者 https://www.devze.com 2023-04-10 04:36 出处:网络
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 in

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 figur开发者_JAVA百科e 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


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>


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>


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消