gpt4 book ai didi

c# - NuGet 直接项目引用作为版本依赖项

转载 作者:太空狗 更新时间:2023-10-29 22:57:35 24 4
gpt4 key购买 nike

我的示例解决方案 > Visual Studio 中有 2 个项目库。我直接在另一个中引用其中一个。当我现在发布我的 nuget 包时,在依赖项概述中我得到我直接引用的 nuget 是 >= 1.0.0.0,当我通过 nuget 执行它时,引用意味着没有直接引用,因为我得到了相同的解决方案>= 依赖项概述下的正确版本号。我不会更改默认的依赖行为 lowest。我尝试过的是用 dependencies/references/files 元素更新我的 nuspec 文件,它们都不适合我。我希望在直接引用的 nuget 中看到相同版本的给定 nuget 作为依赖项。

最佳答案

从你问题的最后一句话和你对直接引用的强调让我觉得我知道你在找什么:

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

设置

For illustration purposes I will use the pattern [assembly: AssemblyVersion("1.0.*")] in my AssemblyInfo.cs files and will do a couple of separate builds on each project to ensure my assemblies get some different interesting versions.

确保每个项目都包含一个与项目同名的.nuspec 文件。这对于项目LibraryA 尤为重要。 ,因为它是由 LibraryB 引用的项目.作为良好做法,我会为两者都这样做。现在让我们使用一个基本模板:

.nuspec下面,替换 token $id$$version$当你运行 nuget.exe 时,将得到它们的值推断针对 .csproj已构建的文件。

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

现在,我要运行 nuget.exe在项目 ~ 的解决方案目录 ( LibraryB) 的命令行中:

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 的内容时, 你会看到 .nuspecnuget.exe 生成将替换所有 $version$将 token 替换为实际使用的版本。

最后一件事,上面的命令将为 LibraryB 创建 NuGet 包但显然你可以为 LibraryA 创建一个只需再次针对 LibraryA.csproj 运行它


我希望这就是您所追求的,或者至少对您可以做什么有所帮助。

关于c# - NuGet 直接项目引用作为版本依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37445144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com