从同一解决方案中的项目引用 Nuget 包所需的解决方案 [英] A solution needed for referencing Nuget packages from projects in the same solution

查看:42
本文介绍了从同一解决方案中的项目引用 Nuget 包所需的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完整的解决方案,其中包含要在我的组织中作为通用 nuget 包共享的项目.该解决方案包含在一个 git 存储库中,我们让 TeamCity 为我们运行构建,尽管我们并不太先进,因为我们在准备为给定项目生成/发布新的 Nuget 包时手动启动 Teamcity 构建解决方案,每个项目都有自己的 TeamCity 构建配置.

I have a single solution full of projects that are to be shared amongst my organization as general nuget packages. The solution is encompassed by one git repository and we have TeamCity running our builds for us, although we are not overly advanced in that we manually kick off the Teamcity builds when we are ready to generate/publish a new Nuget package for a given project in the solution, every project has its own TeamCity build configuration.

构建时,项目通过 .csproj 标签生成 nuget 包:GeneratePackageOnBuild 我们还通过 version 标记.这很好用.我们遇到问题的地方是管理项目对自身的项目引用/依赖关系.我似乎无法理解如何正确执行此操作.例如:

When built, the projects generate nuget packages via the .csproj <project/> tag: GeneratePackageOnBuild we also control the versioning via version tags that are populated through build properties from TeamCity. This works great. Where we run into problems is managing the project references/dependencies that the projects have on themselves. I can't seem to understand how to do this correctly. For example:

-- Solution
----- Project A v1.0.6
----- Project B v1.0.1

项目 A (v1.0.6) 依赖于项目 B (v1.0.1).假设我同时更改了项目 A (v1.0.7) 和项目 B (v1.0.2).我不能让项目 A 引用项目 B 的 nuget 包,因为它尚未构建,所以我使用项目引用.但是,这会导致包 A (v1.0.7) 的 nuget 包假定它具有与项目 B (v1.0.2) 相同的内部版本号 - 而事实并非如此.因此,当有人使用项目 A 时,他们被告知要查找不存在的依赖项项目 B 的版本(示例中为 v1.0.7).为了解决这个问题,我在项目 A .csproj 中添加了以下内容:<ProjectReference Include="..Company.ProjectBCompany.ProjectB.csproj" ExcludeAssets="All"/>

Project A (v1.0.6) has a dependency on Project B (v1.0.1). Let's say I change Project A (v1.0.7) and Project B (v1.0.2) at the same time. I can't have Project A reference the nuget package of Project B since it isn't built yet so I use Project References. However, that causes the nuget package of Package A (v1.0.7) to assume it has the same build number as Project B (v1.0.2) - which it doesn't. Thus, when someone consumes Project A they are told to look for a version of the dependency Project B that doesn't exist (v1.0.7 in the example). To resolve this, I added the following in Project A .csproj: <ProjectReference Include="..Company.ProjectBCompany.ProjectB.csproj" ExcludeAssets="All" />

但是,现在消费者不知道项目 B 依赖项(因为它不再出现在 Nuget 包依赖项中)并且当他们发现他们需要它时,他们会在使用项目 A 时收到运行时错误声明它仍在寻找显然不存在的 Project B v1.0.7.

However, now the consumer doesn't know of the Project B dependency (as it no longer shows up in the Nuget package dependency) and when they figure out they need it, they'll get a runtime error when using Project A stating that it is still looking for Project B v1.0.7 which obviously doesn't exist.

在没有nuspec的情况下生成nuget包时如何智能处理项目引用?我希望尽可能少的人工干预.

How does one intelligently handle project referencing when generating nuget packages without a nuspec? I'd like as little manual intervention as possible.

我的另一种解决方案是使用 Nuget 包引用,但这意味着必须先构建和部署项目 B,然后开发人员才能开始处理项目 A.

My one other solution is to use Nuget package references but that means Project B has to build and deploy before a developer can get to work on Project A.

推荐答案

按照设计,当你打包一个有项目引用的项目时,那些依赖项目会被添加为 NuGet 依赖项,最低版本是每个项目在打包时间.要了解原因,请假设您对 ProjectB 进行了重大更改,并修复了 ProjectA 以使用它.如果您发布 ProjectA,但具有旧版本 ProjectB 的 NuGet 依赖项,则 ProjectA 的 NuGet 用户将在运行时崩溃,因为他们使用的 ProjectB 版本不兼容.NuGet 无法知道这一点.

By design, when you pack a project that has project references, those dependent projects are added as NuGet dependencies, with the minimum version being each project's current version at the time of pack. To understand why, imagine you made a breaking change to ProjectB, and fixed ProjectA to work with it. If you publish ProjectA, but with a NuGet dependency of an old version of ProjectB, then NuGet users of ProjectA will crash at runtime because the version of ProjectB they're using is incompatible. There's no way for NuGet to know this.

因此,如果您想在不增加 ProjectB 的依赖版本的情况下增加 ProjectA 的版本,请将它们放在单独的提交中.否则,同时发布 ProjectA 和 ProjectB 的新版本.

Therefore, if you want to increment ProjectA's version without incrementing the dependency version of ProjectB, make them in separate commits. Otherwise, publish new versions of both ProjectA and ProjectB at the same time.

这篇关于从同一解决方案中的项目引用 Nuget 包所需的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆