NuGet Package references copies dll local

2019-08-22 01:18发布

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?

3条回答
一夜七次
2楼-- · 2019-08-22 01:25

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>.

查看更多
三岁会撩人
3楼-- · 2019-08-22 01:29

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.

查看更多
放荡不羁爱自由
4楼-- · 2019-08-22 01:37

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.

查看更多
登录 后发表回答