我有一个 .NET 6 项目
MyProject
,它引用 NuGet 包。 NuGet 包包含 MyHost.exe
和 MyHost.runtimeconfig.json
和 MyHost.deps.json
文件,全部位于 lib\net6.0
文件夹中。
问题是,当我发布
MyProject
时,MyHost.runtimeconfig.json
和 MyHost.deps.json
文件不会复制到输出文件夹。
我在 PackageReference 中尝试过
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
和 <IncludeAssets>all</IncludeAssets>
但没有帮助。还有一个建议的解决方案,将 *.targets 文件添加到 NuGet 包中,看起来像一门重炮。
由于需要
runtimeconfig.json
文件,我认为应该有一种简单的方法来自动复制它。
以下方法有效。 我在项目的
<NuGet package name>.props
文件夹中创建了一个 build
文件,其中包含以下内容:
<Project>
<Target Name="AddRuntimeDependenciesToContent"
Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'"
BeforeTargets="GetCopyToOutputDirectoryItems"
DependsOnTargets="GenerateBuildDependencyFile;
GenerateBuildRuntimeConfigurationFiles">
<ItemGroup>
<ContentWithTargetPath Include="$(ProjectDepsFilePath)" TargetPath="MyHost.deps.json" CopyToOutputDirectory="PreserveNewest" />
<ContentWithTargetPath Include="$(ProjectRuntimeConfigFilePath)" TargetPath="MyHost.runtimeconfig.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Target>
</Project>
并将此文件夹添加到
MyHost.csproj
文件:
<ItemGroup>
<None Include="build\**" Pack="True" PackagePath="build\" />
</ItemGroup>
这样,任何引用此 NuGet 包的项目都会将
*.props
文件自动“附加”到 *.csproj
文件中。结果 *.runtimeconfig.json
和 *.deps.json
文件被复制到构建输出。