简体   繁体   English

NuGet 包引用复制 dll 本地

[英]NuGet Package references copies dll local

I have some requirement to set Copy Local to false for the NuGet dll.我有一些要求将 NuGet dll 的 Copy Local 设置为 false。 Before that, I used the package.config format and everything worked fine.在此之前,我使用了package.config格式,一切正常。 After migration to Package Reference format , I cannot find a way how to do that.迁移到包参考格式后,我找不到如何做到这一点。 Could someone help me?有人可以帮助我吗?

You can use PrivateAssets.您可以使用 PrivateAssets。 Copied from the docs 从文档复制

<ItemGroup>
    <!-- ... -->

    <PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0">
        <PrivateAssets>all</PrivateAssets>
        <ExcludeAssets>runtime</ExcludeAssets>
    </PackageReference>

    <!-- ... -->
</ItemGroup>

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).我写了这个 msbuild 目标来破解<packagereference ..><privateassets>all</privateassets>...<privateassets>runtime;...像旧的<reference><private>true</private>... (复制 ref 上的本地属性设置为 false)。

Import the below target in your .csproj file or a Directory.Build.targets file in the solution root.在您的 .csproj 文件或解决方案根目录中的 Directory.Build.targets 文件中导入以下目标。

<!--
***********************************************************************************************
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.我确信可以以某种方式优化 itemgroup 合并。 Hope it helps, feedback is welcome.希望对您有所帮助,欢迎反馈。

The easiest way would be to right click the dll and select properties from the solution explorer view under the references tab.最简单的方法是右键单击 dll 并从引用选项卡下的解决方案资源管理器视图中选择属性。 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.如果这在您的情况下不起作用,那么您也许可以像以前一样在 package.config 格式中设置 Copy Local 标志,然后将该依赖项作为 Package Reference 格式迁移到您的项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM