NuGet:Don';不包括来自包的参考

本文关键字:参考 不包括 Don NuGet | 更新日期: 2023-09-27 18:20:50

摘要

当我使用NuGet打包一个库并在另一个项目中引用它时,引用的项目将把其他文件拉到构建目录中。

工作案例

项目:ReferenceLibrary输出:ReferenceLibrary.dll项目:衍生库输出:DerivedLibrary.dll参考:ReferenceLibrary(Copy Local=False)项目:ConsoleApplication输出:ConsoleApplication.exe参考文献:衍生库

编辑:未复制引用库,因为它是在运行时解析的。根据目标的不同,有几个版本。派生项目中的引用。是的,所以我可以针对它进行编码。

如果我构建它,那么只有DerivedLibrary.dll被复制到ConsoleApplication构建文件夹(即bin/Release)。

非工作案例

Project: ConsoleApplication
  Output: ConsoleApplication.exe
  Package: DerivedLibrary.nupkg (depends on ReferenceLibrary.nupkg)

项目引用被添加到DerivedLibray.dll。DerivedLibrary.dll和ReferenceLibrary.dll都是从它们的包中复制的。

我可以在MSBUILD日志中看到它正在被复制。

_CopyFilesMarkedCopyLocal:
    Copying file from "c:'...'ReferenceLibrary.dll" to "bin'Debug'ReferenceLibrary.dll"

尽管.csproj中没有引用它。

我不知道这是NuGet问题(由于它如何解包)还是Visual Studio项目(它如何复制引用的程序集并编码其他程序集中的需求)。

NuGet:Don';不包括来自包的参考

我找到的一个可能的解决方案是使用构建后目标来删除有问题的引用。

在派生库中添加一个DerivedLibrary.targets文件。

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="RemoveUnwantedReferences" AfterTargets="Build">
      <Message Text="Removing unwanted references"/>
      <Delete Files="$(OutputPath)ReferenceLibrary.dll"/>
    </Target>
</Project>

然后在.nuspec中包括

<package>
  ...
  <files>
    <file src="Targets/DerivedLibrary.targets" target="/build/DerivedLibrary.targets" />
  </files>
</package>

然后,当有人安装包时,将添加构建后挂钩。当他们构建时,被复制的文件将被自动删除。