.NET Build:如何将依赖项目的内容文件复制到输出文件夹?
解决方案
方案一:调整项目引用的资产传递配置(推荐,无需自定义目标)
SDK-style项目默认不会自动传递依赖项目的内容文件,你可以在Tests.Foobar.csproj中修改对Tests.Common的项目引用,明确指定要包含内容资产:
<ProjectReference Include="..\Tests.Common\Tests.Common.csproj"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>compile; build; contentfiles; analyzers; buildtransitive</IncludeAssets> </ProjectReference>
核心是在IncludeAssets中加入contentfiles,这样Tests.Common里标记为Content的文件会被传递到Tests.Foobar的构建输出目录。
同时要确保Tests.Common中的内容文件配置正确,比如:
<Content Include="SharedFiles\**\*"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content>
方案二:自定义构建目标(若方案一不适用)
如果需要更灵活的复制规则,可在Tests.Foobar.csproj中添加自定义MSBuild目标,在构建完成后复制Tests.Common输出目录的内容文件:
<Target Name="CopyCommonContentFiles" AfterTargets="Build"> <Copy SourceFiles="@(CommonContentFiles)" DestinationFolder="$(OutputPath)\%(RecursiveDir)" SkipUnchangedFiles="true" /> </Target> <ItemGroup> <CommonContentFiles Include="..\Tests.Common\bin\$(Configuration)\$(TargetFramework)\**\*.txt;..\Tests.Common\bin\$(Configuration)\$(TargetFramework)\**\*.json" /> </ItemGroup>
你可以根据实际文件类型修改Include中的匹配规则,这个目标完全兼容dotnet build命令,会在Tests.Foobar构建完成后自动执行复制操作。
内容的提问来源于stack exchange,提问作者TravelingFox
相关产品推荐
相关产品推荐

