是否可以更改 NuGet 包的位置?
我的大多数项目都有以下约定:
/src
/Solution.sln
/SolutionFolder
/Project1
/Project2
/etc..
/lib
/Moq
moq.dll
license.txt
/Yui-Compressor
yui.compressor.dll
/tools
/ILMerge
ilmerge.exe
您会注意到我没有将外部库保留在源文件夹内部。我也对使用 NuGet 非常感兴趣,但不希望这些外部库位于源文件夹中。 NuGet 是否有一个设置可以更改所有包加载到的目录?
I have the following convention for most of my projects:
/src
/Solution.sln
/SolutionFolder
/Project1
/Project2
/etc..
/lib
/Moq
moq.dll
license.txt
/Yui-Compressor
yui.compressor.dll
/tools
/ILMerge
ilmerge.exe
You'll notice that I do not keep external libraries inside the source folder. I'm also very interested in using NuGet but don't want these external libraries inside the source folder. Does NuGet have a setting to change the directory that all packages are loaded into?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
我刚刚发现的另一个小花絮。 (这可能非常基本,以至于有些人没有提到它,但这对我的解决方案很重要。)“packages”文件夹最终位于与 .sln 文件相同的文件夹中。
我们移动了 .sln 文件,然后修复了其中的所有路径以查找各个项目,瞧!我们的包文件夹最终位于我们想要的位置。
One more little tidbit that I just discovered. (This may be so basic that some haven't mentioned it, but it was important for my solution.) The "packages" folder ends up in the same folder as your .sln file.
We moved our .sln file and then fixed all of the paths inside to find the various projects and voila! Our packages folder ended up where we wanted it.
接受的答案中的配置文件在 VS2012 中适用于我。
然而,对我来说,它仅在我执行以下操作时才有效:
如果我按照这些步骤操作,我可以使用共享包文件夹。
The config file in the accepted answer works for me in VS2012.
However, for me it only works when I do the following:
If I follow those steps I can use a shared package folder.
最一致的方法是使用 nuget config 显式设置配置:
https://learn.microsoft.com/en-us/nuget/consume-packages/configuring-nuget-behavior#changing-config-settings
The most consistent way is by using
nuget config
to explicitly set the config:https://learn.microsoft.com/en-us/nuget/consume-packages/configuring-nuget-behavior#changing-config-settings
VS 2017 更新:
看起来 Nuget 团队的人们终于开始自己使用 Nuget,这帮助他们找到并修复了一些重要的事情。所以现在(如果我没记错的话,因为仍然没有迁移到 VS 2017)下面的内容不再是必要的了。您应该能够将“repositoryPath”设置为本地文件夹并且它将起作用。即使您也可以完全保留它,因为默认情况下还原位置已从解决方案文件夹移至计算机级别。再说一遍 - 我仍然没有自己测试它
VS 2015及更早版本
只是其他答案的提示(特别是 this):
NuGet Package 文件夹的位置可以通过配置更改,但 VisualStudio 仍然相对引用此文件夹中的程序集:
为了解决此问题(直到有更好的解决方案),我使用 subst 命令来创建一个指向 Packages 文件夹新位置的虚拟驱动器:
现在,当添加新的 NuGet 包时,项目引用将使用其绝对位置:
注意:
UPDATE for VS 2017:
Looks people in Nuget team finally started to use Nuget themselves which helped them to find and fix several important things. So now (if I'm not mistaken, as still didn't migrated to VS 2017) the below is not necessary any more. You should be able to set the "repositoryPath" to a local folder and it will work. Even you can leave it at all as by default restore location moved out of solution folders to machine level. Again - I still didn't test it by myself
VS 2015 and earlier
Just a tip to other answers (specifically this):
Location of the NuGet Package folder can be changed via configuration, but VisualStudio still reference assemblies in this folder relatively:
To workaround this (until a better solution) I used subst command to create a virtual drive which points to a new location of the Packages folder:
Now when adding a new NuGet package, the project reference use its absolute location:
Note:
刚刚更新 Nuget 2.8.3。为了更改已安装软件包的位置,我通过右键单击解决方案启用了软件包还原。编辑 NuGet.Config 并添加这些行:
然后重建解决方案,它将所有包下载到我所需的文件夹并自动更新引用。
Just updating with Nuget 2.8.3. To change the location of installed packages , I enabled package restore from right clicking solution. Edited NuGet.Config and added these lines :
Then rebuilt the solution, it downloaded all packages to my desired folder and updated references automatically.
如果您使用的是 Visual Studio 2019 和 NuGet 版本 4(或更高版本),您可以通过编辑 NuGet.config 文件来更改路径。
NuGet.config 文件位于 C:\Users%USER_NAME%\AppData\Roaming\NuGet
将“globalPackagesFolder”和“repositoryPath”键添加到配置文件中。设置您想要的值。
这是一个例子
If you're using Visual Studio 2019 and NuGet version 4 (or above), you may change the path by editing the NuGet.config file.
The NuGet.config file is in C:\Users%USER_NAME%\AppData\Roaming\NuGet
Add "globalPackagesFolder" and "repositoryPath" keys to the config file. Set the values that you wanted.
Here's an example
nuget.config
,并包含以下内容:'packages'
将是恢复所有包的文件夹。nuget.config
in same directory where your solution file is, with following content:'packages'
will be the folder where all packages will be restored.在使用 Visual Studio 2019 和 NuGet v4 或更高版本时,我一直在寻找一种不涉及更改计算机级或用户级配置且不使用绝对路径的解决方案。
根据 NuGet 文档,该值可以是 被当前文件夹(例如解决方案文件夹)或驱动器根目录下的任何文件夹中的 nuget.config 文件覆盖。请注意,NuGet 3.3 及更早版本期望将 nuget.config 文件放置在 .nuget 子文件夹中。一开始这让我很困惑,因为我的设置最初是针对旧版本的,所以我删除了这个子文件夹。
我最终在 Dev 文件夹中放置了一个 nuget.config 文件(其中包含不同 VS 解决方案的子文件夹):
通过此设置,我的 Dev 文件夹下的所有解决方案/项目都将其包安装在名为 installed_packages 的文件夹中strong> 与 nuget.config 文件位于同一级别。
最后,请注意 repositoryPath 由使用 packages.config 的项目使用,而 globalPackagesFolder 由使用 packages.config 的项目使用>封装参考。
While using Visual Studio 2019 and NuGet v4 or later I was looking for a solution that doesn't involve changing the machine-level or user-level config and doesn't use an absolute path.
According to the NuGet Documentation, the value can be overridden by a nuget.config file placed in the current folder (e.g. solution folder) or any folder up to the drive root. Note that NuGet 3.3 and earlier was expecting the nuget.config files to be placed in a .nuget subfolder. This got me at first, as my setup was originally targeting older versions, so I deleted this subfolder.
I ended up with a nuget.config file placed in my Dev folder (which contains subfolders for the different VS solutions):
With this setup, all solutions/projects under my Dev folder install their packages in a folder named installed_packages located at the same level as the nuget.config file.
Finally, note that repositoryPath is used by projects that make use of packages.config, whereas globalPackagesFolder is used by projects that make use of PackageReference.
在解决方案文件所在的同一目录中创建一个包含以下内容的 nuget.config 文件,然后重新启动 Visual Studio。 (我用VS2022测试)[./packages - 将其替换为你的包路径]
Create a nuget.config file in the same directory where your solution file is located with the following content, and restart the visual studio. (I test it with VS2022) [./packages - Replace it with your package path]
现在可以控制软件包安装到哪个文件夹。
http://nuget.codeplex.com/workitem/215
编辑:
请参阅 Phil Haack 于 2010 年 12 月 10 日晚上 11:45 发表的评论(在工作项目/上面的链接中)。该支持在 1.0 中部分实现,但没有记录。
根据@dfowler 的说法:
在解决方案旁边添加一个 nuget.config 文件:
有 一个 nuget 包 用于创建包文件夹覆盖。
2.1 版更新
正如 Azat 所说,现在有关于如何控制软件包位置的官方文档。 2.1 的发行说明 在 nuget.config 文件中指定以下配置(有关放置配置文件的有效位置的说明以及分层配置模型如何工作的说明,请参阅发行说明):
这将更改您配置级别的包文件夹将文件放入(解决方案,如果将其放入解决方案目录,将项目放入项目目录等)。请注意,发行说明中指出:
It's now possible to control which folder the packages are installed into.
http://nuget.codeplex.com/workitem/215
Edit:
See Phil Haack's comment on Dec 10 2010 at 11:45 PM (in the work item/the link above). The support is partially implemented in 1.0, but is not documented.
According to @dfowler:
Add a nuget.config file next to the solution with this:
There is a nuget package for creating the package folder override.
Update for version 2.1
As Azat commented, there is now official documentation on how to control the package locations. The release notes for 2.1 specifies the following configuration in a nuget.config file (see the release notes for a description of valid places to put the config files and how the hierarchical configuration model works):
This would change the packages folder for the configuration level you put the file in (solution if you put it in the solution directory, project in project directory and so on). Note that the release notes state:
这对我不起作用:
这对我有用:
this did NOT work for me:
this did WORK for me:
好吧,为了其他人阅读这篇文章 - 这是我对上面无数答案的理解:
.nuget 文件夹中的 nuget.config 文件相对于该文件夹。这很重要,因为如果您的新文件夹类似于“../Packages”,它将把它放在总是开箱即用的位置。正如 @bruce14 所说,您必须改为执行“../../Packages”
在不启用包还原的情况下,我无法获取最新的 nuget (2.8.5) 来查找标准位置之外的包文件夹。因此,启用包还原后,应将以下内容添加到 .nuget 文件夹内的 nuget.config 文件中以更改位置:
(这很重要) 如果您对nuget.config 文件内的包文件夹位置,您必须重新启动 Visual Studio 或关闭/重新加载解决方案才能使更改生效
Okay for the sake of anyone else reading this post - here is what I understand of the myriad of answers above:
The nuget.config file in the .nuget folder is relative to that folder. This is important because if your new folder is something like '../Packages' that will put it where it always goes out of the box. As @bruce14 states you must do '../../Packages' instead
I could not get the latest nuget (2.8.5) to find a packages folder outside of the standard location without enabling package restore. So once you enable package restore then the following should be added to the nuget.config file inside of the .nuget folder to change the location:
(This is important) If you make ANY changes to the package folder location inside of the nuget.config files you must restart visual studio or close/reload the solution for the changes to take effect
Visual Studio 2015 上的 Nuget 3.2 的解决方案是:
对父文件夹使用正斜杠。
将上述文件(nuget.config)保存在解决方案文件夹中。
可以在此处
A solution for Nuget 3.2 on Visual Studio 2015 is:
Using forward slash for parent folder.
Save above file (nuget.config) in solution folder.
Reference is available here
为了使用 PackageReference 而不是 packages.config 更改项目的路径,您需要使用
来自 https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file
我将 Nuget.config 放在我的解决方案文件旁边并且它起作用了。
In order to change the path for projects using PackageReference instead of packages.config you need to use
globalPackagesFolder
From https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file
I put Nuget.config next to my solution file and it worked.
Visual Studio 2019 和 Nuget 5.9。*
Visual Studio 2019 and Nuget 5.9.*
2.1 发行说明中提出的解决方案并不是开箱即用的。他们忘记提及有代码:
这会阻止它工作。要解决此问题,您需要修改 NuGet.targets 文件并删除“OutputDirectory”参数:
现在,如果您在 NuGet.config 中的某处添加“repositoryPath”配置(请参阅发行说明,了解放置配置文件的有效位置的说明) ),它会将所有包恢复到单个位置,但是...您的 .csproj 仍然包含以相对路径编写的程序集的提示...
我仍然不明白为什么他们要采取艰难的方式而不是更改 PackageManager 所以它会添加提示相对于 PackagesDir 的路径。这就是我手动在本地(在我的桌面上)和构建代理上拥有不同包位置的方式。
The solution proposed in release notes for 2.1 doesn't work out-of-the-box. They forgot to mention that there is code:
which prevents it from working. To fix this you need to modify your NuGet.targets file and remove 'OutputDirectory' parameter:
So now, if you add 'repositoryPath' config somewhere in NuGet.config (see the release notes for a description of valid places to put the config files), it will restore all packages into single location, but... Your .csproj still contains hints to assemblies written as relative paths...
I still don't understand why they went hard way instead of changing PackageManager so it would add hint paths relative to PackagesDir. That's the way I do manually to have different package locations locally (on my desktop) and on build agent.
除了 Shane Kms 的回答之外,如果您已激活 Nuget Package Restore,请编辑位于 .nuget-folder 中的 NuGet.config,如下所示:
注意额外的“..\”,因为它从 .nuget-folder 回溯而不是解决方案文件夹。
In addition to Shane Kms answer, if you've activated Nuget Package Restore, you edit the NuGet.config located in the .nuget-folder as follows:
Notice the extra "..\", as it backtracks from the .nuget-folder and not the solution folder.
由于缺少一些提示,这些答案都不适合我(Nuget 2.8.6),将尝试在此处添加它们,因为它可能对其他人有用。
阅读以下来源后:
https://docs.nuget.org/consume/NuGet-Config-Settings
https://github.com/NuGet/Home/issues/1346
看来要使
repositoryPath 你需要使用正向斜杠,这是因为它们使用Uri对象来解析位置。
例如,
您还可以使用 NuGet 命令来确保语法正确,如下所示:
None of this answers was working for me (Nuget 2.8.6) because of missing some tips, will try to add them here as it might be useful for others.
After reading the following sources:
https://docs.nuget.org/consume/NuGet-Config-Settings
https://github.com/NuGet/Home/issues/1346
It appears that
repositoryPath you need to use forward slashes, it's because they are using Uri object to parse location.
E.g.
You can also use NuGet command to ensure that syntax will be correct like this:
对于 .NET Core 项目和 Visual Studio 2017,我能够通过提供以下配置将所有包恢复到相对路径:
根据我的经验,lib 文件夹是在找到 Nuget.config 的同一级别上创建的,无论 sln 文件位于何处。
我测试过,命令行 dotnet 恢复和 Visual Studio 2017 重建的行为是相同的
For .NET Core projects and Visual Studio 2017 I was able to restore all packages to relative path by providing this configuration:
Based on my experience the lib folder was created on the same level where Nuget.config was found, no matter where sln file was.
I tested and the behavior is same for command line dotnet restore, and Visual Studio 2017 rebuild