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

查看:653
本文介绍了在同一解决方案中引用项目中的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 <project/>标签生成nuget包:GeneratePackageOnBuild我们还通过version标签控制版本控制,这些标签通过TeamCity的构建属性进行填充.这很好用.我们遇到问题的地方是管理项目对自身的引用/依赖性.我似乎无法理解如何正确执行此操作.例如:

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

但是,现在用户不知道Project B依赖项(因为它不再显示在Nuget包依赖项中),当他们发现需要它时,在使用Project 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.

因此,如果要增加ProjectA的版本而不增加ProjectB的依赖版本,请将它们分开提交.否则,请同时发布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天全站免登陆