.net 是否可以更改 NuGet 包的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4092759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to change the location of packages for NuGet?
提问by TheCloudlessSky
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 notkeep external libraries insidethe 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?
你会注意到,我没有保持外部库中的源文件夹。我也对使用 NuGet 非常感兴趣,但不希望在源文件夹中使用这些外部库。NuGet 是否有更改所有包加载到的目录的设置?
采纳答案by PHeiberg
It's now possible to control which folder the packages are installed into.
现在可以控制软件包安装到哪个文件夹中。
http://nuget.codeplex.com/workitem/215
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.
编辑:请参阅 Phil Haack 于 2010 年 12 月 10 日晚上 11:45 发表的评论(在工作项目/上面的链接中)。该支持在 1.0 中部分实现,但没有记录。
According to @dfowler: Add a nuget.config file next to the solution with this:
根据@dfowler:在解决方案旁边添加一个 nuget.config 文件:
<settings>
<repositoryPath>{some path here}</repositoryPath>
</settings>
There is a nuget packagefor creating the package folder override.
Update for version 2.1
2.1 版更新
As Azat commented, there is now official documentation on how to control the package locations. The release notes for 2.1specifies 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):
正如 Azat 评论的那样,现在有关于如何控制包位置的官方文档。2.1的发行说明在 nuget.config 文件中指定了以下配置(有关放置配置文件的有效位置的说明以及分层配置模型的工作原理,请参阅发行说明):
<configuration>
<config>
<add key="repositoryPath" value="C:\thePathToMyPackagesFolder" />
</config>
...
</configuration>
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:
这将更改您放置文件的配置级别的包文件夹(解决方案,如果您将它放在解决方案目录中,项目放在项目目录中等等)。请注意,发行说明指出:
[...] if you have an existing packages folder underneath your solution root, you will need to delete it before NuGet will place packages in the new location.
[...] 如果您的解决方案根目录下有一个现有的包文件夹,则需要先将其删除,然后 NuGet 才会将包放置在新位置。
回答by ShaneKm
- Created a file called "nuget.config".
- Added that file to my solutions folder
- 创建了一个名为“nuget.config”的文件。
- 将该文件添加到我的解决方案文件夹中
this did NOT work for me:
这对我不起作用:
<configuration>
<config>
<add key="repositoryPath" value="..\ExtLibs\Packages" />
</config>
...
</configuration>
this did WORK for me:
这对我有用:
<?xml version="1.0" encoding="utf-8"?>
<settings>
<repositoryPath>..\ExtLibs\Packages</repositoryPath>
</settings>
回答by Robert Petz
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 relativeto 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:
<?xml version="1.0" encoding="utf-8"?> <configuration> ... <config> <add key="repositoryPath" value="..\..\Packages" /> </config> ... </configuration>(This is important)If you make ANY changes to the package folder location inside of the nuget.config files you mustrestart visual studio or close/reload the solution for the changes to take effect
在.nuget文件夹中的文件nuget.config是相对于该文件夹。这很重要,因为如果您的新文件夹类似于“../Packages”,它将把它放在总是开箱即用的地方。正如@ bruce14 所说,你必须改为 '../../Packages'
在不启用包还原的情况下,我无法获得最新的 nuget (2.8.5) 以在标准位置之外找到包文件夹。因此,一旦启用包还原,则应将以下内容添加到 .nuget 文件夹内的 nuget.config 文件中以更改位置:
<?xml version="1.0" encoding="utf-8"?> <configuration> ... <config> <add key="repositoryPath" value="..\..\Packages" /> </config> ... </configuration>(这很重要)如果对 nuget.config 文件内的包文件夹位置进行任何更改,则必须重新启动 Visual Studio 或关闭/重新加载解决方案以使更改生效
回答by phuongnd
A solution for Nuget 3.2 on Visual Studio 2015 is:
Visual Studio 2015 上 Nuget 3.2 的解决方案是:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="../lib" />
</config>
</configuration>
Using forward slash for parent folder. Save above file (nuget.config) in solution folder.
对父文件夹使用正斜杠。将上述文件 (nuget.config) 保存在解决方案文件夹中。
Reference is available here
参考可在此处获得
回答by Dmitry Naumov
The solution proposed in release notes for 2.1 doesn't work out-of-the-box. They forgot to mention that there is code:
2.1 发行说明中提出的解决方案不能开箱即用。他们忘了说有代码:
internal string ResolveInstallPath()
{
if (!string.IsNullOrEmpty(this.OutputDirectory))
{
return this.OutputDirectory;
}
ISettings settings = this._configSettings;
...
}
which prevents it from working. To fix this you need to modify your NuGet.targets file and remove 'OutputDirectory' parameter:
这会阻止它工作。要解决此问题,您需要修改 NuGet.targets 文件并删除“OutputDirectory”参数:
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(RequireConsentSwitch)</RestoreCommand>
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...
所以现在,如果您在 NuGet.config 中的某处添加“repositoryPath”配置(有关放置配置文件的有效位置的描述,请参阅发行说明),它会将所有包还原到一个位置,但是...您的 .csproj 仍然包含对编写为相对路径的程序集的提示...
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 manuallyto have different package locations locally (on my desktop) and on build agent.
我仍然不明白他们为什么要努力而不是改变 PackageManager,所以它会添加相对于 PackagesDir 的提示路径。这就是我手动在本地(在我的桌面上)和构建代理上拥有不同包位置的方式。
<Reference Include="Autofac.Configuration, Version=2.6.3.862, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>$(PackagesDir)\Autofac.2.6.3.862\lib\NET40\Autofac.Configuration.dll</HintPath>
</Reference>
回答by user
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:
除了 Shane Kms 回答之外,如果您已激活 Nuget Package Restore,您可以编辑位于 .nuget-folder 中的 NuGet.config,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<repositoryPath>..\..\ExtLibs\Packages</repositoryPath>
</configuration>
Notice the extra "..\", as it backtracks from the .nuget-folder and not the solution folder.
注意额外的“..\”,因为它从 .nuget 文件夹而不是解决方案文件夹回溯。
回答by Roman Badiornyi
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.
由于缺少一些提示,这些答案都不适用于我(Nuget 2.8.6),将尝试在此处添加它们,因为它可能对其他人有用。
After reading the following sources:
https://docs.nuget.org/consume/NuGet-Config-Settings
https://github.com/NuGet/Home/issues/1346
It appears that
阅读以下来源后:
https: //docs.nuget.org/consume/NuGet-Config-Settings
https://github.com/NuGet/Home/issues/1346
看来
- To make working Install-Package properly with different repositoryPath you need to use forwardslashes, it's because they are using Uri object to parse location.
- Without $on the begining it was still ignoring my settings.
- NuGet caches config file, so after modifications you need to reload solution/VS.
- I had also strange issue while using command of NuGet.exe to set this option, as it modified my global NuGet.exe under AppData\Roaming\NuGet and started to restore packages there (Since that file has higher priority, just guessing).
- 要使用不同的 repositoryPath 使 Install-Package 正常工作,您需要使用正斜杠,这是因为它们使用 Uri 对象来解析位置。
- 没有$开始它仍然忽略我的设置。
- NuGet 缓存配置文件,因此修改后需要重新加载解决方案/VS。
- 我在使用 NuGet.exe 命令设置此选项时也遇到了奇怪的问题,因为它修改了 AppData\Roaming\NuGet 下的全局 NuGet.exe 并开始在那里恢复包(因为该文件具有更高的优先级,只是猜测)。
E.g.
例如
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<config>
<add key="repositorypath" value="$/../../../Common/packages" />
</config>
</configuration>
You can also use NuGet command to ensure that syntax will be correct like this:
您还可以使用 NuGet 命令来确保语法正确,如下所示:
NuGet.exe config -Set repositoryPath=$/../../../Common/packages -ConfigFile NuGet.Config
回答by Kirill Chilingarashvili
For .NET Core projects and Visual Studio 2017 I was able to restore all packages to relative path by providing this configuration:
对于 .NET Core 项目和 Visual Studio 2017,我能够通过提供以下配置将所有包恢复到相对路径:
<configuration>
<config>
<add key="globalPackagesFolder" value="lib" />
</config>
...
</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
根据我的经验,无论 sln 文件在哪里,lib 文件夹都是在找到 Nuget.config 的同一级别创建的。我进行了测试,命令行 dotnet restore 和 Visual Studio 2017 重建的行为相同
回答by Harald
The config file in the accepted answer works for me in VS2012. However, for me it onlyworks when I do the following:
接受的答案中的配置文件在 VS2012 中对我有用。但是,对我来说,它仅在我执行以下操作时才有效:
- Create a new project in VS.
- Exit VS - this seems to be important.
- Copy the config files to the project folder.
- Restart VS and add packages.
- 在 VS 中创建一个新项目。
- 退出 VS - 这似乎很重要。
- 将配置文件复制到项目文件夹。
- 重启VS并添加包。
If I follow those steps I can use a shared package folder.
如果我按照这些步骤操作,我可以使用共享包文件夹。
回答by Manny
In order to change the path for projects using PackageReference instead of packages.config you need to use globalPackagesFolder
为了使用 PackageReference 而不是 packages.config 更改项目的路径,您需要使用 globalPackagesFolder
From https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file
来自https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file
globalPackagesFolder (projects using PackageReference only)
The location of the default global packages folder. The default is %userprofile%.nuget\packages (Windows) or ~/.nuget/packages (Mac/Linux). A relative path can be used in project-specific nuget.config files. This setting is overridden by the NUGET_PACKAGES environment variable, which takes precedence.
repositoryPath (packages.config only)
The location in which to install NuGet packages instead of the default $(Solutiondir)/packages folder. A relative path can be used in project-specific nuget.config files. This setting is overridden by the NUGET_PACKAGES environment variable, which takes precedence.
globalPackagesFolder(仅使用 PackageReference 的项目)
默认全局包文件夹的位置。默认为 %userprofile%.nuget\packages (Windows) 或 ~/.nuget/packages (Mac/Linux)。可以在特定于项目的 nuget.config 文件中使用相对路径。此设置由优先的 NUGET_PACKAGES 环境变量覆盖。
repositoryPath(仅packages.config)
安装 NuGet 包的位置,而不是默认的 $(Solutiondir)/packages 文件夹。可以在特定于项目的 nuget.config 文件中使用相对路径。此设置由优先的 NUGET_PACKAGES 环境变量覆盖。
<config>
<add key="globalPackagesFolder" value="c:\packageReferences" />
<add key="repositoryPath" value="c:\packagesConfig" />
</config>
I put Nuget.config next to my solution file and it worked.
我将 Nuget.config 放在我的解决方案文件旁边并且它起作用了。

