Search code examples
asp.net-coremsbuildcsproj

Copy to Publish Directory output of PreBuild Event


Inside my csproj I have a pre-build event where I run the build of Vue js project. It outputs to a "dist" folder, and that is loaded by an cshtml file.

In the csproj file I have a reference to the dist folder and I tell it to copy to publish directory:

<ItemGroup>
  <Content Include="dist\**" CopyToPublishDirectory="Always" />
</ItemGroup>

On publish, MsBuild seems to be trying to copy the files in the dist folder that exist before the pre-build event starts. Is there an way to get MsBuild to copy the contents of the folder after the pre-build event?


Solution

  • In order to support all possible publish mechanisms that tooling (VS etc.) supports, I suggest setting it up similar to how the in-box angular template works:

    <Target Name="PublishDistFiles" AfterTargets="ComputeFilesToPublish">
      <ItemGroup>
        <DistFiles Include="dist\**" />
        <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
          <RelativePath>%(DistFiles.Identity)</RelativePath>
          <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        </ResolvedFileToPublish>
      </ItemGroup>
    </Target>