【问题标题】:NuGet direct project references as versioned dependenciesNuGet 直接项目引用作为版本化依赖项
【发布时间】:2016-05-25 18:37:31
【问题描述】:

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

【问题讨论】:

    标签: c# visual-studio nuget nuget-package nuspec


    【解决方案1】:

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

    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 创建一个包


    我希望这就是您所追求的,或者至少可以说明您可以做什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      相关资源
      最近更新 更多