Here is part of my  A.csproj:
 <ItemGroup>
    <Content Include="..\..\Payloads\**\*.*">
      <Link>Payloads\%(RecursiveDir)%(FileName)%(Extension)</Link>
      <CopyToOutputDirectory>None</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>
Payloads directory is created in a project B which is referenced from project A
B.csproj
 <PropertyGroup>
    <PostBuildEvent>
      xcopy /I /S /R /Y /d $(TargetDir)*.* $(SolutionDir)Payloads\Content
    </PostBuildEvent>
  </PropertyGroup>
Payloads directory doesn't exist in publish directory when I run dotnet publish
Info
dotnet SDK version 2.0, ASP.NET Core 2.0 on .NET 461 framework
It seems like publish content is calculated before the build. When I run dotnet publish if Payloads exists it is deployed correctly. This is just simplified example of my projects.
 How can I fix this?
I replaced
 <ItemGroup>
    <Content Include="..\..\Payloads\**\*.*">
      <Link>Payloads\%(RecursiveDir)%(FileName)%(Extension)</Link>
      <CopyToOutputDirectory>None</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>
With
 <Target Name="AddPayloadsFolder" AfterTargets="AfterPublish">
        <PropertyGroup>
          <PayloadsDirectory>$(SolutionDir)Payloads</PayloadsDirectory>
        </PropertyGroup>
        <Exec Command="xcopy.exe /I /S /R /Y /d $(PayloadsDirectory) $(PublishDir)Payloads" Condition="!Exists('$(PublishDir)Payloads')" />
        <Exec Command="xcopy.exe /I /S /R /Y /d $(PayloadsDirectory) $(PublishUrl)Payloads" Condition="!Exists('$(PublishUrl)Payloads')" />
      </Target>
The version with PublishUrl is for Visual Studio. It uses it for compatibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With