【问题标题】:Create nuget package for a solution with multiple projects为具有多个项目的解决方案创建 nuget 包
【发布时间】:2011-11-09 04:05:48
【问题描述】:

我们目前正在构建一个包含多个项目的解决方案。

我们有这样的东西:

- Common
  - Logging
     - Logging.NLog
- Threading

所以Logging.NLog依赖于Logging,Logging on Common...等

当我们打包 Logging.NLog 时,我希望 nuget 发现 Loggin 和 Common 依赖项。

目前,我用 Common 创建了一个包,然后在 Logging 中安装了这个包 Common with

install-package Common

但是每当我对 Common 进行修改时,我都必须更新包,它们是由我们的持续集成系统 (Hudson) 创建的,所以在我们开发时非常烦人。

我只想有一个项目参考(添加参考 -> 项目...),然后 nuget 无论如何都会发现依赖关系。

有办法实现吗?

【问题讨论】:

标签: c# .net dependencies nuget nuget-package


【解决方案1】:

我认为 Charles 的意思是,如果引用的项目也用于构建 NuGet 包,他希望 NuGet 自动将项目引用解析为包依赖项,对吧?

例子:

  1. 设置日志以生成 NuGet 包
  2. Lo​​gging.Nlog 设置为生成 NuGet 包
  3. Lo​​gging.Nlog 有一个对 Logging 的项目引用。
  4. 生成的 Logging.Nlog 包应该依赖于生成的 Logging 包。

这也是我自己一直在寻找的东西,但遗憾的是我发现它目前不受支持。上面有一个work item,计划用于 NuGet 1.7,但还没有关于如何处理这个问题的设计。

【讨论】:

    【解决方案2】:

    这个帖子有个好建议:NuGet and multiple solutions

    基本上,将通用组件分解为自己的解决方案,并具有自己的发布生命周期。

    【讨论】:

      【解决方案3】:

      目前无法完全按照您的要求进行操作,但以下内容将帮助您简化更新。

      听起来您需要将 nuspec 文件添加到您的解决方案中。类似于以下三个文件。注意后两个中的依赖关系。这些通过 [$version$] 引用相同的 dll 版本。这意味着当您运行以下命令时,它会更新所有三个,因为依赖项上的方括号需要特定版本的依赖包。

      PM> 更新包通用

      在 Hudson 中,您需要使用 nuget pack 命令 (see Nuget command reference) 执行这些 nuspec 文件,并将生成的包包含在您的工件中,并将它们部署到您的本地 nuget 服务器。我会把它留给你。

      您需要做的另一件事是确保您的所有程序集都为相同的构建获得相同的版本。同样,Hudson 可以处理此问题,或者您可以使用通用的 AssemblyInfo 文件。

      Common.nuspec

      <?xml version="1.0"?>
      <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
      <metadata>
          <version>$version$</version>
          <authors>Charles Ouellet</authors>
          <owners />
          <iconUrl>http://domain/Content/images/LOGO_32x32.png</iconUrl>
          <id>Common</id>
          <title>Common</title>
          <requireLicenseAcceptance>false</requireLicenseAcceptance>
          <description>full description here</description>
      </metadata>
      <files>
          <file src="..\Common\bin\Release\Common.dll" target="lib\net40" />
          <file src="..\Common\bin\Release\Common.pdb" target="lib\net40" />
      </files>
      </package>
      

      Logging.nuspec

      <?xml version="1.0"?>
      <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
      <metadata>
          <version>$version$</version>
          <authors>Charles Ouellet</authors>
          <owners />
          <iconUrl>http://domain/Content/images/LOGO_32x32.png</iconUrl>
          <id>Logging</id>
          <title>Logging</title>
          <requireLicenseAcceptance>false</requireLicenseAcceptance>
          <description>full description here</description>
          <dependencies>
              <dependency id="Common" version="[$version$]" />
          </dependencies>        
      </metadata>
      <files>
          <file src="..\Logging\bin\Release\Logging.dll" target="lib\net40" />
          <file src="..\Logging\bin\Release\Logging.pdb" target="lib\net40" />
      </files>
      </package>
      

      Logging.NLog

      <?xml version="1.0"?>
      <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
      <metadata>
          <version>$version$</version>
          <authors>Charles Ouellet</authors>
          <owners />
          <iconUrl>http://domain/Content/images/LOGO_32x32.png</iconUrl>
          <id>Logging.NLog</id>
          <title>Logging.NLog</title>
          <requireLicenseAcceptance>false</requireLicenseAcceptance>
          <description>full description here</description>
          <dependencies>
              <dependency id="Logging" version="[$version$]" />
          </dependencies>        
      </metadata>
      <files>
          <file src="..\Logging.NLog\bin\Release\Logging.NLog.dll" target="lib\net40" />
          <file src="..\Logging.NLog\bin\Release\Logging.NLog.pdb" target="lib\net40" />
      </files>
      </package>
      

      【讨论】:

      • 是的!这也许是我需要的。我现在将尝试它,但这样我可以拥有一个 VS 解决方案,其中包含 3 个项目,每个项目都可以在解决方案中引用其他项目,但是当我打包它们时,它们都是独立的包,依赖于其他包。好的去试试谢谢
      【解决方案4】:

      有一个计划中的 feature 针对这个确切的场景。

      这就是它的外观:

      > nuget.exe pack proj.csproj -IncludeReferencedProjects
      

      显然是implemented仅仅之前,但有bugsstillbeingironedout

      目前的功能允许:

      • 将多个项目的工件打包到一个 nuget 包中(通过递归遍历 project 引用),

      • 如果引用的项目具有随附的 .nuspec 文件,则创建对这些项目关联包的 nuget 包引用。

      功能请求可以追溯到 1.5,但一直在下滑。不过最近,它收集了足够的质量(请求),计划在Nuget 2.3 发布。

      发布计划将版本 2.3 与“2013 年 4 月结束”挂钩,敬请期待。
      (目前,最新的 Nuget 版本是 2.2.1)。

      【讨论】:

      • 这似乎在我的版本 (2.5.40416.9020) 中工作正常,不需要 .nuspec 文件中的任何 元素(本例中为 proj.nuspec)。
      【解决方案5】:

      我是这样实现的:

      <ProjectReference Include="MyProject2.csproj" PrivateAssets="All" />
      

      在 MyProject.csproj 中为每个项目添加 PrivateAssets="All"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多