为具有多个项目的解决方案创建nuget包

时间:2011-09-02 12:58:44

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

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

我们有这样的事情:

- Common
  - Logging
     - Logging.NLog
- Threading

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

当我们打包Logging.NLog时,我希望nuget能够发现Loggin和Common dependecies。

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

install-package Common

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

我想简单地有一个项目参考(添加引用 - >项目......),然后nuget会发现依赖项。

有没有办法实现它?

5 个答案:

答案 0 :(得分:14)

计划feature针对此确切方案。

这显然是这样的:

> nuget.exe pack proj.csproj -IncludeReferencedProjects

显然implemented只是以前,但有bugs still being ironed {{3} }。

目前的功能允许:

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

OR

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

功能请求一直追溯到1.5,但它一直在滑动。最近,它收集了足够的质量(请求)以便在out中发布。

发布计划为“2013年4月底”制定版本2.3,敬请关注 (目前,最新的Nuget版本是2.2.1)。

答案 1 :(得分:3)

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

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

PM> update-package common

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

答案 2 :(得分:1)

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

示例:

  1. 记录设置为生成NuGet包
  2. Logging.Nlog设置为生成NuGet包
  3. Logging.Nlog具有对Logging的项目引用。
  4. 生成的Logging.Nlog包应该依赖于生成的Logging包。
  5. 这也是我一直在寻找自己的东西,但遗憾的是我发现目前还没有支持。它上面有一个work item,计划用于NuGet 1.7,但是还没有关于如何处理它的设计。

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

我是这样实现的:

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

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