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

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

问题描述

我有一个带有两个项目的Visual Studio解决方案-一个包含一些逻辑的.NET标准(2.0)库和一个包含用户界面等的.NET Framework(4.6.1)类库.然后,.NET Framework库具有.NET标准库已添加为项目参考,因此可以使用逻辑库中包含的方法.

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提供的NuGet服务器(Azure Artifacts).

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标准(逻辑)库打包并推送到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 pack msbuild -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 具有nuget依赖性.这对于SDK样式的项目可以开箱即用",但是,如果您有一个旧样式"项目(导入Microsoft.CSharp.targets),则需要添加对NuGet.Build.Tasks.Pack的程序包引用.如果使用PackageReference,则可能需要编辑csproj并设置此依赖项以将PrivateAssets设置为all,以避免此程序包成为nuget依赖项.我不知道如果您正在使用packages.config,或者甚至可以做到的话.如果您使用的是msbuild或dotnet pack,但您也有自己的nuspec文件,建议不要使用它.您可以通过在csproj中设置正确的属性或项属性来执行nuspec中的任何操作,并让pack目标自动生成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 的记忆是,只要引用的项目具有< project name> .nuspec,nuget就会自动使项目引用nuget依赖项.与<项目名称> .csproj 文件位于同一目录中的文件(我几乎可以肯定它记录在docs.microsoft.com/nuget上的某个位置).但是,如果您的从属项目是SDK样式的项目(例如.NET Core或.NET Standard项目所需的项目),那么我已经建议不要在这些项目中使用nuspec文件,因此您应该真正考虑使用pack目标并停止使用 nuget pack .

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天全站免登陆