问题描述
是否有 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
的项目只会将其包提取到全局包文件夹中.
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.
因此,如果您在内部连接的计算机上恢复所有解决方案,那么全局包文件夹可能包含您需要的所有包,但从技术上讲,您可能还需要从解决方案包文件夹中获取包.
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
项目的 objproject.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 objproject.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
的文件夹code>.nupkg 作为 包源一>.此 nuget.config
文件不需要在您的存储库中.您可以利用 NuGet 分层累积配置这一事实,并在离线机器的根目录上创建此 nuget.config
(或者,如果您将所有存储库克隆到一个位置,则可以将其放在那里),因为只有离线机器需要它.只要您的任何项目都没有清除 packageSources
部分的 nuget.config
,那么 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:
- 在连接 Internet 的计算机上,创建一个
nuget.config
文件(dotnet new nugetconfig
,尽管它创建了一个可怕的nuget.config
清除包源,但我找不到删除<clear/>
XML 元素的好方法)- 将
globalPackagesFolder
设置为自定义路径 (nuget.exe config -set globalPackagesFolder=usedPackages -configfile nuget.config
),该路径当前为空 - 将
packageSource
添加到您的用户配置文件的全局包文件夹(nuget.exe 源 add -name globalPackagesFolder -Source %USERPROFILE%.nugetpackages -configfile .uget.config
),以提高性能
- 将
- On your internet connected computer, create a
nuget.config
file (dotnet new nugetconfig
, although it creates a horriblenuget.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%.nugetpackages -configfile .uget.config
), to improve performance
- Sets the
这篇关于将包含所有依赖项的 Nuget 包下载到文件夹的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!