从你问题的最后一句话和你对直接引用的强调让我觉得我知道你在追求什么:
NuGet 定义了-IncludeReferencedProjects 选项以指示nuget.exe 应如何处理引用的项目,无论是作为依赖项还是作为包的一部分:
- 如果引用的项目具有与该项目同名的对应
.nuspec 文件,则将该引用的项目添加为显式 NuGet 依赖项。
- 否则,引用的项目将作为包的一部分添加。
我的猜测是你追求的是前者。
让我们将问题简化为最基本的形式:假设您有一个解决方案,其中LibraryA 直接引用LibraryB 作为项目引用。构建解决方案时,LibraryA 的程序集输出将复制到 LibraryB
~/
│ Solution.sln
├───LibraryA
│ │ ClassA.cs
│ │ LibraryA.csproj
│ │ LibraryA.nuspec
│ ├───bin
│ │ ├───Debug
│ │ │ LibraryA.dll
│ │ │ LibraryA.pdb
│ │ └───Release
│ └───Properties
│ AssemblyInfo.cs
└───LibraryB
│ ClassB.cs
│ LibraryB.csproj
│ LibraryB.nuspec
├───bin
│ ├───Debug
│ │ LibraryA.dll
│ │ LibraryA.pdb
│ │ LibraryB.dll
│ │ LibraryB.pdb
│ └───Release
└───Properties
AssemblyInfo.cs
设置
出于说明目的,我将在我的AssemblyInfo.cs 文件中使用模式[assembly: AssemblyVersion("1.0.*")],并将在每个项目上进行几个单独的构建,以确保我的程序集得到一些不同的有趣版本。
确保每个项目都包含一个与项目同名的 .nuspec 文件。 这对于项目 LibraryA 尤其重要,因为它是由 LibraryB 引用的项目。我会做这两个作为一个好的做法。现在让我们使用一个基本模板:
在下面的.nuspec 中,当您针对已构建的.csproj 文件运行nuget.exe 时,替换标记$id$ 和$version$ 将获得它们的值推断。
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<authors>The author... (**mandatory element**)</authors>
<description>Your description... (**mandatory element**)</description>
</metadata>
</package>
使用nuget pack -IncludeReferencedProjects
现在,我将在项目LibraryB 的解决方案目录(~)的命令行中运行nuget.exe:
PS> nuget pack .\LibraryB\LibraryB.csproj -IncludeReferencedProjects -Verbosity detailed
Attempting to build package from 'LibraryB.csproj'.
Packing files from '~\LibraryB\bin\Debug'.
Using 'LibraryB.nuspec' for metadata.
Add file '~\LibraryB\bin\Debug\LibraryB.dll' to package as 'lib\net451\LibraryB.dll'
Id: LibraryB
Version: 1.0.5993.6096
Authors: The author... (**mandatory element**)
Description: Your description... (**mandatory element**)
Dependencies: LibraryA (= 1.0.5993.7310)
Added file 'lib\net451\LibraryB.dll'.
Successfully created package '~\LibraryB.1.0.5993.6096.nupkg'.
PS>
生成的包
上述命令将创建一个 NuGet 包 LibraryB.1.0.5993.6096.nupkg,其中包含对 LibraryA.1.0.5993.7310.nupkg 的显式 NuGet 依赖。
当您检查LibraryB.1.0.5993.6096.nupkg 的内容时,您会看到nuget.exe 生成的.nuspec 将所有$version$ 替换标记替换为实际使用的版本。
最后一件事,上面的命令将仅为LibraryB 创建 NuGet 包,但显然您可以通过再次针对LibraryA.csproj 运行它来为LibraryA 创建一个包
我希望这就是您所追求的,或者至少可以说明您可以做什么。