dotnet 发布包含额外文件 [英] dotnet publish include extra files
问题描述
我有一个依赖于我的自定义项目的 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.Contractsin$(ConfigurationName)*.abi" "$(TargetDir)" /Y /I
xcopy "$(SolutionDir)Ethereum.Contractsin$(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="....DependenciesDependencies.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.Contractsin$(ConfigurationName)*.abi" "$(TargetDir)" /Y /I


xcopy "$(SolutionDir)Ethereum.Contractsin$(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
命令:
To fix your problem, you could pass the value for $(SolutionDir)
macros to dotnet publish
command via switch /p
:
dotnet publish -c Release -r win-x64/p:SolutionDir="D:projectsSolutionDir"
dotnet publish -c Release -r win-x64 /p:SolutionDir="D:projectsSolutionDir"
您还应该在 $(SolutionDir)
宏之后添加一个反斜杠:
You should also add a backslash after $(SolutionDir)
macros:
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy "$(SolutionDir)Ethereum.Contractsin$(ConfigurationName)*.abi" "$(TargetDir)" /Y /I


xcopy "$(SolutionDir)Ethereum.Contractsin$(ConfigurationName)*.bin" "$(TargetDir)" /Y /I" />
</Target>
UPDATE(关于 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
目标时,出现了由
附加到命令行的分隔符引起的错误.
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
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.Contractsin$(ConfigurationName)*.abi" "$(TargetDir)" /Y /I" />
<Exec Command="xcopy "$(SolutionDir)Ethereum.Contractsin$(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 发布包含额外文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!