将具有所有依赖项的Nuget软件包下载到文件夹的命令 [英] Command to download a Nuget package with all dependencies to a folder

查看:80
本文介绍了将具有所有依赖项的Nuget软件包下载到文件夹的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Windows命令将带有依赖项的Nuget软件包下载到文件夹,以便我可以将其传输到脱机计算机?
我的意思是我可以给Nuget包命名的命令,它将把包及其所有依赖项下载为 nupkg 文件到一个文件夹。

Is there a windows command to download a Nuget packages with its dependencies to a folder so i can transfer them to an offline computer? What i mean is a command that i can give the Nuget package's name and it will download the package and all it's dependencies to a folder as nupkg files.

我知道有一些镜像工具,但实际上,如果可能的话,我正在使用命令行为我的特定问题寻找答案。

I know that there are some mirroring tools but actually i'm looking an answer for my specific question using command line if possible.

推荐答案

如果您正在寻找仅需要少量脚本编写的预先构建的解决方案,则我不知道这种工具。但是,我认为您要实现自己的目标并不困难。下面的长答案是因为我提供了很多细节,而不是让我变得更加困难。

If you're looking for a pre-built solution that requires you to only do a little bit of scripting, I'm not aware of such a tool. However, I don't believe it will be particularly difficult for you to do what you want to achieve. The long answer below is because I'm giving lots of details, rather than it being more difficult that I'm making it out to be.

将所有项目存储在连接到互联网的计算机上的存储库中。如果您有任何使用 packages.config 的项目,则它们的软件包已在解决方案软件包文件夹中为您收集。它们也应该位于全局软件包文件夹中,但是如果您清除全局软件包文件夹并在解决方案软件包文件夹中已经存在软件包时进行还原,则不会将它们下载/提取到全局软件包文件夹中。使用 PackageReference 的项目只会将其软件包解压缩到全局package文件夹中。

Restore all the projects in your repo on an internet-connected computer. If you have any projects using packages.config, their packages are already collected for you in the solution packages folder. They should also be in the global packages folder, but if you clear your global packages folder and restore when the packages already exist in the solution packages folder, they will not be downloaded/extracted into the global packages folder. Projects that use PackageReference will only have their packages extracted to the global packages folder.

因此,如果您还原了所有您的解决方案在内部连接的计算机上,则global packages文件夹可能会包含您需要的所有软件包,但是从技术上讲,您可能还需要从解决方案软件包文件夹中获取软件包。

So, if you restore all your solutions on an internal-connected computer, then the global packages folder will probably contain all the packages you need, but technically you may need to also get packages from the solution packages folder.

一种选择是简单地从全局程序包文件夹中复制所有程序包,但可能包含解决方案不需要的程序包。如果您只想复制解决方案使用的软件包,请先复制解决方案软件包文件夹中的软件包,因为它们肯定被某些东西引用,否则它们将不存在。然后,编写一个程序,该程序将读取每个 PackageReference 项目的 obj\project.assets.json 文件,因为其中包含完整的依赖图。让您的项目从所有这些文件中获取NuGet软件包的列表,然后从全局软件包文件夹中复制需要复制的软件包的列表。

One option is to simply copy all packages from your global packages folder, but will probably contain packages that are not required by your solution. If you want to only copy packages used by your solution, then start by copying the packages in the solution packages folder, because they're definitely referenced by something, otherwise they wouldn't exist. Then, write a program that will read every PackageReference project's obj\project.assets.json file, because that contains the full dependency graph. Have your project get the list of NuGet packages from all these files, then you have your list of packages you need to copy from the global packages folder.

另一种选择是创建 nuget.config 文件并在执行还原之前,请指定自定义 globalPackagesFolder 。如果这样做,我建议您也将个人资料的全局软件包文件夹添加为 package source ,因此还原操作要快得多。只要您的自定义全局软件包文件夹在开始之前为空,那么它将仅包含您的解决方案使用的软件包。

Another option is to create a nuget.config file and specify a custom globalPackagesFolder before doing the restore. If you do this, I recommend you also add your profile's global packages folder as a package source, so that doing the restore is much quicker. As long as your custom global packages folder is empty before you start, then it will contain only the packages used by your solution(s).

从技术上讲,您可以简单地将解决方案软件包文件夹从源计算机复制到目标计算机,然后将全局软件包文件夹从源计算机到目标计算机。但是,工具总是有机会在不同版本的工具之间改变行为,因此我更愿意让工具管理自己的数据。一个示例是全局软件包文件夹包含一个 .nupkg.sha512 文件,在最新版本的NuGet中,还包含一个 .nupkg.metadata 文件,该文件在解决方案软件包文件夹中不存在。由于NuGet的工作方式,如果将解决方案程序包文件夹复制到全局程序包文件夹中,它将无法工作。将来这可能会改变,但是在我写这篇文章时,它是行不通的。

Technically, you can simply copy the solution packages folder from the source computer to the destination computer and copy the global packages folders from the source computer to the destination computer. However, there's always the chance that a tool changes behaviour across different versions of the tool, so I prefer to let tools manage their own data. One example is that the global packages folder contains a .nupkg.sha512 file, and in recent versions of NuGet, also a .nupkg.metadata file, which do not exist in the solution packages folder. Due to the way NuGet works, it will not work if you copy a solution packages folder into a global packages folder. This may change in the future, but at the time I'm writing this, it won't work.

因此,我建议仅复制 .nupkg 文件,然后在脱机计算机上创建一个 nuget.config 文件,该文件将文件夹添加到您复制所有 .nupkg 作为包来源。此 nuget.config 文件不需要在您的存储库中。您可以利用NuGet分层累积配置并在离线计算机的根目录上创建此 nuget.config 的事实(或者,如果将所有存储库克隆到单个位置,您可以将其放在此处),因为只有离线计算机才需要它。只要您的所有项目都没有 nuget.config 来清除 packageSources 部分,然后将所有项目放在一个子目录中 nuget.config 所在的文件夹将自动将其用作程序包源。

Therefore, I recommend copying only the .nupkg files, and on your offline computer, create a nuget.config file that adds the folder where you copy all the .nupkg's as a package source. This nuget.config file doesn't need to be in your repo. You can take advantage of the fact that NuGet accumulates configuration hierarchically and create this nuget.config on the root of your offline machine (or if you clone all repos to a single location, you can put it there), since it's only required by the offline machine. As long as none of your projects has a nuget.config that clears the packageSources section, then all projects in a sub-folder of where the nuget.config is will automatically use it as a package source.

最简单的方法(例如您要编写脚本)是:

The easiest way (say you want to script it) is:


  1. 您的互联网连接计算机,创建一个 nuget.config 文件( dotnet new nugetconfig ,尽管它创建了一个可怕的 nuget.config 清除程序包源,我找不到删除< clear /> XML的好方法元素),即


    • globalPackagesFolder 设置为自定义路径( nuget。 exe config -set globalPackagesFolder = usedPackages -configfile nuget.config ),当前为空

    • 添加 packageSource 到用户配置文件的全局软件包文件夹( nuget.exe源添加-name globalPackagesF -Source%USERPROFILE%\.nuget\packages -configfile .\nuget.config ),以提高性能

  1. On your internet connected computer, create a nuget.config file (dotnet new nugetconfig, although it creates a horrible nuget.config that clears package sources, and I can't find a good way to remove the <clear/> XML element) that
    • Sets the globalPackagesFolder to a custom path (nuget.exe config -set globalPackagesFolder=usedPackages -configfile nuget.config), that is currently empty
    • Add a packageSource to your user profile's global packages folder (nuget.exe sources add -name globalPackagesFolder -Source %USERPROFILE%\.nuget\packages -configfile .\nuget.config), to improve performance

这篇关于将具有所有依赖项的Nuget软件包下载到文件夹的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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