我有一个netcore应用程序取决于我的自定义项目.因为我无法直接引用它,所以我编写了postbuildevents,这些事件会将我的文件复制到输出中:
I have a netcore application depends on my custom project. Because I cannot reference it directly, I have written postbuildevents that are copying my files to output:
xcopy "$(SolutionDir)Ethereum.Contracts\bin\$(ConfigurationName)\*.abi" "$(TargetDir)" /Y /I xcopy "$(SolutionDir)Ethereum.Contracts\bin\$(ConfigurationName)\*.bin" "$(TargetDir)" /Y /I在我构建和运行项目时,它工作正常,但是在运行 publish 命令时,它不包含这些文件.
It works fine when I build and run project, but when run publish command it doesn't include these files.
该怎么办?我也尝试过这种方法,但是 AddPayloadsFolder 没有被调用.
How can it be done? I tried this approach as well but AddPayloadsFolder doesn't get called.
我的 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 "$(SolutionDir)Ethereum.Contracts\bin\$(ConfigurationName)\*.abi" "$(TargetDir)" /Y /I


xcopy "$(SolutionDir)Ethereum.Contracts\bin\$(ConfigurationName)\*.bin" "$(TargetDir)" /Y /I" /> </Target> <Target Name="AddPayloadsFolder" AfterTargets="AfterPublish"> <Exec Command="xcopy "$(TargetDir)\*.abi" "$(PublishDir)" /Y /I


xcopy "$(TargetDir)\*.bin" "$(PublishDir)" /Y /I" /> </Target> </Project> 推荐答案启动 dotnet publish 命令时,未定义 $(SolutionDir)宏.结果,使用错误的源路径启动了 xcopy .
When you launch dotnet publish command, $(SolutionDir) macros is not defined. As result xcopy is launched with incorrect source path.
缺少解决方案宏的问题是此处所述,并且该问题仍处于打开状态状态.我不确定它是否会修复,因为 $(SolutionDir)是IDE特定的宏.
The issue with missing solution macros is described here and it's still in opened state. I'm not sure it will be ever fixed because $(SolutionDir) is IDE-specific macros.
要解决您的问题,您可以通过开关/p 将 $(SolutionDir)宏的值传递给 dotnet publish 命令:/p>
To fix your problem, you could pass the value for $(SolutionDir) macros to dotnet publish command via switch /p:
dotnet发布-c版本-r win-x64/p:SolutionDir ="D:\ projects \ SolutionDir"
dotnet publish -c Release -r win-x64 /p:SolutionDir="D:\projects\SolutionDir"
您还应该在 $(SolutionDir)宏之后添加反斜杠:
You should also add a backslash after $(SolutionDir) macros:
<Target Name="PostBuild" AfterTargets="PostBuildEvent"> <Exec Command="xcopy "$(SolutionDir)\Ethereum.Contracts\bin\$(ConfigurationName)\*.abi" "$(TargetDir)" /Y /I


xcopy "$(SolutionDir)\Ethereum.Contracts\bin\$(ConfigurationName)\*.bin" "$(TargetDir)" /Y /I" /> </Target>更新(关于 AddPayloadsFolder 目标):
AddPayloadsFolder 目标定义为
<Target Name="AddPayloadsFolder" AfterTargets="AfterPublish">不会执行
,因为对于 dotnet publish 而言, AfterTargets 的正确值是 Publish ,而不是 AfterPublish .我尚未找到有关此事实的明确文档,但是在此问题
is not executed since the correct value of AfterTargets for dotnet publish is Publish, not AfterPublish. I have not found explicit documentation for this fact, however it is mentioned in this issue.
还可以考虑将用换行符分隔的多个 xcopy 命令替换为多个命令.当我最终执行了 AddPayloadsFolder 目标时,出现了由 \ r 分隔符附加到命令行引起的错误.
Also consider replacing several xcopy commands delimited by newline separator with multiple commands. When I got AddPayloadsFolder target finally executed, I got an error caused by \r delimiter appended to the command line.
总结以上所有内容,这里调整了 PostBuild 和 AddPayloadsFolder 目标:
Summarizing all of the above, here are adjusted PostBuild and AddPayloadsFolder targets:
<Target Name="PostBuild" AfterTargets="PostBuildEvent"> <Exec Command="xcopy "$(SolutionDir)\Ethereum.Contracts\bin\$(ConfigurationName)\*.abi" "$(TargetDir)" /Y /I" /> <Exec Command="xcopy "$(SolutionDir)\Ethereum.Contracts\bin\$(ConfigurationName)\*.bin" "$(TargetDir)" /Y /I" /> </Target> <Target Name="AddPayloadsFolder" AfterTargets="Publish"> <Exec Command="xcopy "$(TargetDir)*.abi" "$(PublishDir)" /Y /I" /> <Exec Command="xcopy "$(TargetDir)*.bin" "$(PublishDir)" /Y /I" /> </Target>更多推荐
dotnet发布包含额外的文件
发布评论