.net - dotnet发布包含额外的文件

标签 .net .net-core .net-core-2.0

我有一个netcore应用程序取决于我的自定义项目。因为我无法直接引用它,所以我编写了将这些文件复制到输出的postbuildevents:

xcopy "$(SolutionDir)Ethereum.Contracts\bin\$(ConfigurationName)\*.abi" "$(TargetDir)" /Y /I
xcopy "$(SolutionDir)Ethereum.Contracts\bin\$(ConfigurationName)\*.bin" "$(TargetDir)" /Y /I

当我构建和运行项目时,它工作正常,但是当运行publish命令时,它不包含这些文件。

如何做呢?我也尝试了this approach,但是AddPayloadsFolder没有被调用。

我的csproj:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Dependencies\Dependencies.csproj" />
  </ItemGroup>

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="log4net.config">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="xcopy &quot;$(SolutionDir)Ethereum.Contracts\bin\$(ConfigurationName)\*.abi&quot; &quot;$(TargetDir)&quot; /Y /I&#xA;&#xA;&#xD;&#xA;xcopy &quot;$(SolutionDir)Ethereum.Contracts\bin\$(ConfigurationName)\*.bin&quot; &quot;$(TargetDir)&quot; /Y /I" />
  </Target>

  <Target Name="AddPayloadsFolder" AfterTargets="AfterPublish">
    <Exec Command="xcopy &quot;$(TargetDir)\*.abi&quot; &quot;$(PublishDir)&quot; /Y /I&#xA;&#xA;&#xD;&#xA;xcopy &quot;$(TargetDir)\*.bin&quot; &quot;$(PublishDir)&quot; /Y /I" />
  </Target>

</Project>

最佳答案

当您启动dotnet publish命令时,未定义$(SolutionDir)宏。结果,使用不正确的源路径启动了xcopy

解决方案宏丢失的问题是described here,它仍处于打开状态。我不确定它是否会被修复,因为$(SolutionDir)是特定于IDE的宏。

要解决您的问题,您可以通过$(SolutionDir)开关将dotnet publish宏的值传递给/p命令:

dotnet publish -c Release -r win-x64 /p:SolutionDir="D:\projects\SolutionDir"



您还应该在$(SolutionDir)宏后面添加反斜杠:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="xcopy &quot;$(SolutionDir)\Ethereum.Contracts\bin\$(ConfigurationName)\*.abi&quot; &quot;$(TargetDir)&quot; /Y /I&#xA;&#xA;&#xD;&#xA;xcopy &quot;$(SolutionDir)\Ethereum.Contracts\bin\$(ConfigurationName)\*.bin&quot; &quot;$(TargetDir)&quot; /Y /I" />
</Target>

UPDATE (关于AddPayloadsFolder目标):
AddPayloadsFolder目标定义为
<Target Name="AddPayloadsFolder" AfterTargets="AfterPublish">

由于AfterTargetsdotnet publish的正确值是Publish而不是AfterPublish,因此不会执行。我没有找到有关此事实的明确文档,但是在this issue中提到了该文档。

还可以考虑将用换行符分隔的几个xcopy命令替换为多个命令。当我最终执行了AddPayloadsFolder目标时,我收到了由\r分隔符附加到命令行引起的错误。

总结以上所有内容,以下是调整后的PostBuildAddPayloadsFolder目标:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="xcopy &quot;$(SolutionDir)\Ethereum.Contracts\bin\$(ConfigurationName)\*.abi&quot; &quot;$(TargetDir)&quot; /Y /I" />
    <Exec Command="xcopy &quot;$(SolutionDir)\Ethereum.Contracts\bin\$(ConfigurationName)\*.bin&quot; &quot;$(TargetDir)&quot; /Y /I" />
</Target>

<Target Name="AddPayloadsFolder" AfterTargets="Publish">
    <Exec Command="xcopy &quot;$(TargetDir)*.abi&quot; &quot;$(PublishDir)&quot; /Y /I" />
    <Exec Command="xcopy &quot;$(TargetDir)*.bin&quot; &quot;$(PublishDir)&quot; /Y /I" />
</Target>

关于.net - dotnet发布包含额外的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49573010/

相关文章:

c# - ServiceStack 和 .NET Core 2 的根 URL

c# - Visual Studio 不会构建为 exe (.NET Core 2.0)

c# - 覆盖等于 : typeof returns the static type

c# - 是否有任何使用.NET 的自动化测试示例源代码

websocket - SignalR 不在 IIS Express 10 上使用 websockets

c# - dotnet 核心 Web API JSON 响应包含不必要的元素

.net - Kendo 网格聚合总和显示空页脚

c# - 是否可以动态修补 dotnet 函数

c# - 我如何将 gzip 用于我的 Angular 项目?

.net - 为什么 IHostedService 是异步的