问题
I have some requirement to set Copy Local to false for the NuGet dll. Before that, I used the package.config format and everything worked fine. After migration to Package Reference format, I cannot find a way how to do that. Could someone help me?
回答1:
You can use PrivateAssets. Copied from the docs
<ItemGroup>
<!-- ... -->
<PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<!-- ... -->
</ItemGroup>
edit: actually, you might need to use <ExcludeAssets>runtime</ExcludeAssets>
.
回答2:
The easiest way would be to right click the dll and select properties from the solution explorer view under the references tab. From there you can set the flag manually for the whole package.
If that doesn't work in your case, then you might be able to set the Copy Local flag in the package.config format as you had been doing and then migrate that dependency into your project as Package Reference format.
回答3:
I wrote this msbuild target to hack <packagereference ..><privateassets>all</privateassets>...
or <privateassets>runtime;...
to act like the old <reference><private>true</private>...
(copy local property on ref set to false).
Import the below target in your .csproj file or a Directory.Build.targets file in the solution root.
<!--
***********************************************************************************************
RemovePrivatePackageReference.targets
This is a hack to ensure privateassets = all is handled similar to reference
private=true (copy local false) even for legacy nugets.
Note that this hack is only intended to help legacy solutions where nugets owners hasn't
updated their packages. It is not intended as a long-term sustainable solution.
[Anders Laub // Laub+Co]
***********************************************************************************************
-->
<Project>
<Target Name="RemovePrivatePackageReference" AfterTargets="ResolveReferences">
<ItemGroup>
<_PrivatePackagesReferences Include="@(PackageReference)"
Condition="%(PackageReference.PrivateAssets) == 'all' or $([System.String]::Copy('%(PackageReference.PrivateAssets)').Contains('runtime'))">
<NuGetPackageId>%(Identity)</NuGetPackageId>
</_PrivatePackagesReferences>
</ItemGroup>
<ItemGroup>
<_ReferenceCopyLocalPathsFromPackages Include="@(ReferenceCopyLocalPaths)" Condition="%(ReferenceCopyLocalPaths.NuGetPackageId) != ''" />
</ItemGroup>
<ItemGroup>
<_PrivatePackageReferenceCopyLocalPaths
Include="@(_ReferenceCopyLocalPathsFromPackages)" Condition="'%(NuGetPackageId)' != '' and '@(_PrivatePackagesReferences)' != ''" />
</ItemGroup>
<ItemGroup>
<ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" Condition="'%(Identity)' != '' and '@(_PrivatePackageReferenceCopyLocalPaths)' != ''" />
</ItemGroup>
</Target>
</Project>
I am sure the itemgroup merging can be optimized somehow. Hope it helps, feedback is welcome.
来源:https://stackoverflow.com/questions/55101078/nuget-package-references-copies-dll-local