在构建时将项目引用更改为 NuGet 包引用 [英] Changing a project reference to a NuGet package reference on build

查看:25
本文介绍了在构建时将项目引用更改为 NuGet 包引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个项目的 Visual Studio 解决方案 - 一个包含一些逻辑的 .NET Standard (2.0) 库和一个包含用户界面等的 .NET Framework (4.6.1) 类库.然后 .NET Framework 库具有 .NET Standard 库作为项目引用添加,以便它可以使用逻辑库中包含的方法.

I have a Visual Studio solution with two projects - a .NET Standard (2.0) library containing some logic and a .NET Framework (4.6.1) class library containing user interface etc. The .NET Framework library then has the .NET Standard library added as a project reference so it can use methods contained in the logic library.

然后我在 Azure DevOps 中使用 Azure Pipelines 构建这两个项目,将它们打包为 NuGet 包并将它们推送到 Azure DevOps(Azure Artifacts)提供的 NuGet 服务器.

I am then using Azure Pipelines in Azure DevOps to build these two projects, pack them as NuGet packages and push them to a NuGet server provided by Azure Devops (Azure Artifacts).

但是,发布 NuGet 包时,它们仅包含 NuGet 中对我通过 NuGet 添加到项目的包的依赖项.理想情况下,我想要的是当我的 .NET Standard(逻辑)库打包并推送到 NuGet 服务器时,NuGet 上对它的引用(带有更新的版本号等)被添加到我的 .NET Framework (UI) 库中.

However, when the NuGet packages are published, they only include dependencies in NuGet on packages that I've added to the projects via NuGet. What I want ideally is when my .NET Standard (logic) library is packaged up and pushed to the NuGet server, a reference to it on NuGet (with the updated version number etc) is added to my .NET Framework (UI) library.

有没有其他人遇到过这个问题,可以给我一些如何解决的指导吗?到目前为止,我已经在网上查看并绘制了一张空白.

Has anyone else had this issue that can give me some guidance on how to solve it please? I've had a look online and drawn a blank so far.

谢谢!

推荐答案

我的回答分为两部分.

首先,如果您使用 dotnet packmsbuild -t:pack,而没有 nuspec 文件,则项目引用会自动成为 nuget 依赖项.我使用以下命令验证了这一点:

Firstly, if you're using dotnet pack or msbuild -t:pack, without a nuspec file, then project references automatically become nuget dependencies. I verified this using the following commands:

dotnet new classlib -n ProjectA
dotnet new classlib -n ProjectB
dotnet add ProjectB/ProjectB.csproj reference ProjectA/ProjectA.csproj
dotnet pack ProjectB/ProjectB.csproj

ProjectB.1.0.0.nupkg 依赖于 ProjectA v1.0.0.这适用于 SDK 样式项目开箱即用",但如果您有一个旧样式"项目(导入 Microsoft.CSharp.targets),则需要添加对 NuGet.Build.Tasks.Pack 的包引用.如果使用 PackageReference,则可能需要编辑 csproj 并设置此依赖项以将 PrivateAssets 设置为 all 以避免此包成为 nuget 依赖项.如果您使用的是packages.config,或者甚至可能的话,我不知道等价物是什么.如果您使用的是 msbuild 或 dotnet pack,但您也有自己的 nuspec 文件,我建议您不要使用它.通过在 csproj 中设置正确的属性或项目属性,您可以在 nuspec 中执行任何操作,并让包目标自动生成 nuspec.当您拥有自己的 nuspec 时,部分 nuget 的自动生成功能会被关闭,因此您可能会让事情变得更加困难.

ProjectB.1.0.0.nupkg has a nuget dependency on ProjectA v1.0.0. This works "out of the box" with SDK style projects, but if you have an "old style" project (that imports Microsoft.CSharp.targets), you'll need to add a package reference to NuGet.Build.Tasks.Pack. If you use PackageReference, you may need to edit your csproj and set this dependency to set PrivateAssets to all to avoid this package becoming a nuget dependency. I don't know what the equivalent is if you're using packages.config, or if it's even possible. If you're using msbuild or dotnet pack, but you also have your own nuspec file, I recommend against it. Anything you want to do in the nuspec should be possible by setting the right properties or item attributes in your csproj, and let the pack targets generate the nuspec automatically. When you have your own nuspec, parts of nuget's auto-generation gets turned off, so you may be making things harder for yourself.

我对旧样式 csproj 文件的 nuget pack 的记忆是只要引用的项目具有 .nuspec,nuget 就会自动使项目引用成为 nuget 依赖项 文件与 .csproj 文件位于同一目录中(我几乎可以肯定它已记录在 docs.microsoft.com/nuget 上的某处).但是,如果您的依赖项目是 SDK 样式的项目(例如 .NET Core 或 .NET Standard 项目需要),那么我已经建议不要在这些项目中使用 nuspec 文件,因此您应该真正考虑使用包目标并停止使用 nuget 包.

My memory of nuget pack on old style csproj files is that nuget will automatically make a project reference a nuget dependency as long as the referenced project has a <project name>.nuspec file in the same directory as the <project name>.csproj file (I'm almost certain that it's documented somewhere on docs.microsoft.com/nuget). However, if your dependant project is a SDK style project (like is needed for .NET Core or .NET Standard projects), then I already advised against using a nuspec file with those projects, so you should really consider using the pack targets and stop using nuget pack.

我的回答的第二部分将直接回答您的问题,即如何使项目引用成为某些构建上的包引用.首先,您需要使用包目标,通过使用 SDK 样式项目或通过引用 NuGet.Build.Tasks.Pack 包.这样,您无需使用 nuspec 文件,所有内容都在您的 csproj 中定义,它只是一个 MSBuild 文件.MSBuild 有条件,所以手动编辑你的 csproj 并使你的项目引用有一定的条件,而包引用有相反的条件.我建议使用像 $(CI) 这样的变量,这样你就可以使用 msbuild -t:pack -p:CI=true 测试包,在你的 CI 机器上,只需将 CI 设置为环境变量即可真的.由于 NuGet 已经内置了将项目引用转换为 nuget 依赖项的功能,因此我建议使用它,而不是在包和项目引用之间切换,因此我不打算提供有关如何执行此操作的复制粘贴示例.但是,如果这不是 XY 问题 的情况,并且您确实需要这样做,那么我已经给出了足够的指示,让你能够弄清楚如何做剩下的事情.

The second part of my answer will directly answer your question, which is about how to make a project reference a package reference on certain builds. Firstly, you need to use pack targets, either by using a SDK style project or by referencing the NuGet.Build.Tasks.Pack package. This way, you don't use a nuspec file, and everything is defined in your csproj, which is just a MSBuild file. MSBuild has conditions, so hand edit your csproj and make your project reference have a certain condition and have the package reference have the opposite condition. I suggest use a variable like $(CI), so you can test the pack using msbuild -t:pack -p:CI=true, and on your CI machine just set CI as an environment variable to true. Since NuGet already has functionality built-in for making project references into nuget dependencies, I recommend using that, and not switching between package and project references, so I'm not going to give a copy-paste example on how to do this. But if this isn't a case of a XY-problem and you really need to do this, then I've already given enough pointers for you to be able to figure out how to do the rest.

这篇关于在构建时将项目引用更改为 NuGet 包引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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