在从 VS csproj 生成的 NuGet 包中包含第 3 方 DLL

Include 3rd party DLLs in a NuGet package generated from a VS csproj

我正在尝试从 VS 2017 项目创建一个 NuGet 包,其中包含对第 3 方 DLL (Kendo.mvc.dll) 的引用。无论我尝试什么,我都无法让 nuget pack 自动将该 DLL 包含在我的 NuGet 包的 lib 文件夹中。

我最初使用命令 nuget spec [project's name and path].csproj 从命令行创建了 .nuspec 文件。然后我调整了该文件的设置,生成了这个 .nuspec 文件:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>our names</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>the description</description>
    <releaseNotes>First release</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>entity-framework-6 mvc5</tags>
  </metadata>
</package>

然后我使用 nuget pack 创建包,结果是:

根据我在文档 (MS Docs - Creating NuGet packages) 中阅读的内容,我期望 nuget pack 自动将项目中引用的任何非 nuget 源 DLL 包含在包中,但事实并非如此发生了什么?

我尝试了以下方法,看看是否有任何不同,但都无济于事:

This question 谈论为第 3 方 DLL 添加一个单独的元素,我猜这是指在生成包之前在 .nuspec 文件中显式添加文件,例如

  <files>
    <file src="bin\Debug\Kendo.Mvc.???" target="lib\net461" />
  </files>

确实有效,但是如果从 csproj 文件生成 .nuspec 文件时需要,难道不应该自动添加吗?

but shouldn't this be added automatically if it's needed when generating a .nuspec file from a csproj file?

是的,在从 .csproj 文件生成 .nuspec 文件时,默认情况下不会自动添加 non-nuget-sourced DLL。

当我们从.csproj文件生成.nuspec文件时,.nuspec只包含基本信息,例如标题、描述等。您可以从程序集信息中获取此信息,属性 -> 应用程序 -> 程序集信息:

但默认不包含第三方DLL

因此,为了包含第三方 DLL,我们需要在 .nuspec 文件中显式手动添加文件,就像您所做的那样。

查看 Create nuget package from dlls 了解更多详情。

顺便说一句,如果第3方DLL是项目中引用的nuget-sourced个DLL,你可以在从csproj文件生成.nuspec文件时使用参数-IncludeReferencedProjects自动添加。

文档:pack command (NuGet CLI)

希望这对您有所帮助。

如果你想通过 Visual Studio 或 dotnet 来完成,那么你可以编辑你的 csproj 文件,添加一个 ItemGroup 来包含 dll,如下所示:这会将其他 dll 与你当前的项目 dll 一起打包在 nuget 包中。

<ItemGroup>
    <Content Include="<path to other dll>">
        <Pack>true</Pack>
        <PackagePath>lib$(TargetFramework)</PackagePath>
    </Content>
</ItemGroup>