是否可以更改 NuGet 包的位置?

发布于 2024-09-30 09:33:47 字数 390 浏览 5 评论 0原文

我的大多数项目都有以下约定:

/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

笑忘罢 2024-10-07 09:33:48

我刚刚发现的另一个小花絮。 (这可能非常基本,以至于有些人没有提到它,但这对我的解决方案很重要。)“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.

世界等同你 2024-10-07 09:33:48

接受的答案中的配置文件在 VS2012 中适用于我。
然而,对我来说,它在我执行以下操作时才有效:

  1. 在 VS 中创建一个新项目。
  2. 退出 VS - 这似乎很重要。
  3. 将配置文件复制到项目文件夹中。
  4. 重启VS并添加包。

如果我按照这些步骤操作,我可以使用共享包文件夹。

The config file in the accepted answer works for me in VS2012.
However, for me it only works when I do the following:

  1. Create a new project in VS.
  2. Exit VS - this seems to be important.
  3. Copy the config files to the project folder.
  4. Restart VS and add packages.

If I follow those steps I can use a shared package folder.

笑梦风尘 2024-10-07 09:33:48

最一致的方法是使用 nuget config 显式设置配置:

nuget config -set repositoryPath=c:\packages -configfile c:\my.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:

nuget config -set repositoryPath=c:\packages -configfile c:\my.config

https://learn.microsoft.com/en-us/nuget/consume-packages/configuring-nuget-behavior#changing-config-settings

倒带 2024-10-07 09:33:48

VS 2017 更新:

看起来 Nuget 团队的人们终于开始自己使用 Nuget,这帮助他们找到并修复了一些重要的事情。所以现在(如果我没记错的话,因为仍然没有迁移到 VS 2017)下面的内容不再是必要的了。您应该能够将“repositoryPath”设置为本地文件夹并且它将起作用。即使您也可以完全保留它,因为默认情况下还原位置已从解决方案文件夹移至计算机级别。再说一遍 - 我仍然没有自己测试它

VS 2015及更早版本

只是其他答案的提示(特别是 this):

NuGet Package 文件夹的位置可以通过配置更改,但 VisualStudio 仍然相对引用此文件夹中的程序集:

<HintPath>..\..\..\..\..\..\SomeAssembly\lib\net45\SomeAssembly.dll</HintPath>

为了解决此问题(直到有更好的解决方案),我使用 subst 命令来创建一个指向 Packages 文件夹新位置的虚拟驱动器:

subst N: C:\Development\NuGet\Packages

现在,当添加新的 NuGet 包时,项目引用将使用其绝对位置:

<HintPath>N:\SomeAssembly\lib\net45\SomeAssembly.dll</HintPath>

注意:

  1. 这样的虚拟驱动器将在重新启动后被删除,因此请确保 处理它
  2. 不要忘记替换项目文件中的现有引用。

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:

<HintPath>..\..\..\..\..\..\SomeAssembly\lib\net45\SomeAssembly.dll</HintPath>

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:

subst N: C:\Development\NuGet\Packages

Now when adding a new NuGet package, the project reference use its absolute location:

<HintPath>N:\SomeAssembly\lib\net45\SomeAssembly.dll</HintPath>

Note:

  1. Such a virtual drive will be deleted after restart, so make sure you handle it
  2. Don't forget to replace existing references in project files.
肤浅与狂妄 2024-10-07 09:33:48

刚刚更新 Nuget 2.8.3。为了更改已安装软件包的位置,我通过右键单击解决方案启用了软件包还原。编辑 NuGet.Config 并添加这些行:

  <config>
    <add key="repositorypath" value="..\Core\Packages" />
  </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 :

  <config>
    <add key="repositorypath" value="..\Core\Packages" />
  </config>

Then rebuilt the solution, it downloaded all packages to my desired folder and updated references automatically.

债姬 2024-10-07 09:33:48

如果您使用的是 Visual Studio 2019 和 NuGet 版本 4(或更高版本),您可以通过编辑 NuGet.config 文件来更改路径。

NuGet.config 文件位于 C:\Users%USER_NAME%\AppData\Roaming\NuGet

将“globalPackagesFolder”和“repositoryPath”键添加到配置文件中。设置您想要的值。

这是一个例子

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
  </packageSources>
  <config>
    <add key="globalPackagesFolder" value="E:\.packages" />
    <add key="repositoryPath" value="E:\.nuget" />
  </config>
</configuration>

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

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Microsoft Visual Studio Offline Packages" value="C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\" />
  </packageSources>
  <config>
    <add key="globalPackagesFolder" value="E:\.packages" />
    <add key="repositoryPath" value="E:\.nuget" />
  </config>
</configuration>
等往事风中吹 2024-10-07 09:33:48
  1. 在解决方案文件所在的同一目录中创建 nuget.config,并包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value="packages" />
  </config>
</configuration>

'packages' 将是恢复所有包的文件夹。

  1. 关闭 Visual Studio 解决方案并再次打开。
  1. Create nuget.config in same directory where your solution file is, with following content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value="packages" />
  </config>
</configuration>

'packages' will be the folder where all packages will be restored.

  1. Close Visual studio solution and open it again.
梦开始←不甜 2024-10-07 09:33:48

在使用 Visual Studio 2019 和 NuGet v4 或更高版本时,我一直在寻找一种不涉及更改计算机级或用户级配置且不使用绝对路径的解决方案。

根据 NuGet 文档,该值可以是 被当前文件夹(例如解决方案文件夹)或驱动器根目录下的任何文件夹中的 nuget.config 文件覆盖。请注意,NuGet 3.3 及更早版本期望将 nuget.config 文件放置在 .nuget 子文件夹中。一开始这让我很困惑,因为我的设置最初是针对旧版本的,所以我删除了这个子文件夹。

我最终在 Dev 文件夹中放置了一个 nuget.config 文件(其中包含不同 VS 解决方案的子文件夹):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value=".\installed_packages" />
    <add key="globalPackagesFolder" value=".\installed_packages" />
  </config>
</configuration>

通过此设置,我的 Dev 文件夹下的所有解决方案/项目都将其包安装在名为 installed_pa​​ckages 的文件夹中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):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value=".\installed_packages" />
    <add key="globalPackagesFolder" value=".\installed_packages" />
  </config>
</configuration>

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.

战皆罪 2024-10-07 09:33:48

在解决方案文件所在的同一目录中创建一个包含以下内容的 nuget.config 文件,然后重新启动 Visual Studio。 (我用VS2022测试)[./packages - 将其替换为你的包路径]

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <settings>
  <repositoryPath>./packages</repositoryPath>
 </settings>
</configuration>



    

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]

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <settings>
  <repositoryPath>./packages</repositoryPath>
 </settings>
</configuration>



    
雨的味道风的声音 2024-10-07 09:33:47

现在可以控制软件包安装到哪个文件夹。

http://nuget.codeplex.com/workitem/215

编辑:
请参阅 Phil Haack 于 2010 年 12 月 10 日晚上 11:45 发表的评论(在工作项目/上面的链接中)。该支持在 1.0 中部分实现,但没有记录。

根据@dfowler 的说法:
在解决方案旁边添加一个 nuget.config 文件:

<settings>
<repositoryPath>{some path here}</repositoryPath>
</settings>

一个 nuget 包 用于创建包文件夹覆盖。

2.1 版更新

正如 Azat 所说,现在有关于如何控制软件包位置的官方文档。 2.1 的发行说明 在 nuget.config 文件中指定以下配置(有关放置配置文件的有效位置的说明以及分层配置模型如何工作的说明,请参阅发行说明):

<configuration>
  <config>
    <add key="repositoryPath" value="C:\thePathToMyPackagesFolder" />
  </config>
  ... 
</configuration>

这将更改您配置级别的包文件夹将文件放入(解决方案,如果将其放入解决方案目录,将项目放入项目目录等)。请注意,发行说明中指出:

[...] 如果您的解决方案下有现有的包文件夹
root,您需要先删除它,然后 NuGet 将包放入
新位置。

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:

<settings>
<repositoryPath>{some path here}</repositoryPath>
</settings>

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):

<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.

寒冷纷飞旳雪 2024-10-07 09:33:47
  1. 创建了一个名为“nuget.config”的文件。
  2. 将该文件添加到我的解决方案文件夹中,

这对我不起作用:

<configuration>
  <config>
    <add key="repositoryPath" value="..\ExtLibs\Packages" />
  </config>
  ... 
</configuration>

这对我有用:

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <repositoryPath>..\ExtLibs\Packages</repositoryPath>
</settings>
  1. Created a file called "nuget.config".
  2. Added that file to my solutions folder

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>
_失温 2024-10-07 09:33:47

好吧,为了其他人阅读这篇文章 - 这是我对上面无数答案的理解:

  1. .nuget 文件夹中的 nuget.config 文件相对于该文件夹。这很重要,因为如果您的新文件夹类似于“../Packages”,它将把它放在总是开箱即用的位置。正如 @bruce14 所说,您必须改为执行“../../Packages”

  2. 在不启用包还原的情况下,我无法获取最新的 nuget (2.8.5) 来查找标准位置之外的包文件夹。因此,启用包还原后,应将以下内容添加到 .nuget 文件夹内的 nuget.config 文件中以更改位置:

    
    <配置>
      ...
      <配置>
        <添加 key="repositoryPath" value="..\..\Packages" />
      
      ...
    
    
  3. (这很重要) 如果您对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:

  1. 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

  2. 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>
    
  3. (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

浴红衣 2024-10-07 09:33:47

Visual Studio 2015 上的 Nuget 3.2 的解决方案是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <config>
        <add key="repositoryPath" value="../lib" />
    </config>
</configuration>

对父文件夹使用正斜杠。
将上述文件(nuget.config)保存在解决方案文件夹中。

可以在此处

A solution for Nuget 3.2 on Visual Studio 2015 is:

<?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.

Reference is available here

若无相欠,怎会相见 2024-10-07 09:33:47

为了使用 PackageReference 而不是 packages.config 更改项目的路径,您需要使用

来自 https://learn.microsoft.com/en-us/nuget/reference/nuget-config-file

globalPackagesFolder(仅使用 PackageReference 的项目)

默认全局包文件夹的位置。默认为
%userprofile%.nuget\packages (Windows) 或 ~/.nuget/packages
(苹果机/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>

我将 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

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.

<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.

往事风中埋 2024-10-07 09:33:47

Visual Studio 2019Nuget 5.9。*

  1. 打开 %AppData%\NuGet 文件夹,打开现有的 NuGet.Config 文件。编辑repositoryPath键并设置新的目的地。

<预置><代码> <配置>
<添加 key="repositoryPath" value="D:\.nuget\packages" />

  1. 在环境变量中编辑系统变量 NUGET_PACKAGES 并设置新目标

在此处输入图像描述

  1. 重新启动 VS。无需将 nuget.config 文件放入每个解决方案中。

Visual Studio 2019 and Nuget 5.9.*

  1. Open %AppData%\NuGet folder, open existing NuGet.Config file. Edit repositoryPath key and set new destination.
 <config>
  <add key="repositoryPath" value="D:\.nuget\packages" />
  </config>
  1. In environment variables edit system variable NUGET_PACKAGES and set new destination

enter image description here

  1. Restart VS. No need to put nuget.config file in every solution.
幸福%小乖 2024-10-07 09:33:47

2.1 发行说明中提出的解决方案并不是开箱即用的。他们忘记提及有代码:

internal string ResolveInstallPath()
{
    if (!string.IsNullOrEmpty(this.OutputDirectory))
    {
        return this.OutputDirectory;
    }
    ISettings settings = this._configSettings;

    ...
}

这会阻止它工作。要解决此问题,您需要修改 NuGet.targets 文件并删除“OutputDirectory”参数:

    <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(RequireConsentSwitch)</RestoreCommand>

现在,如果您在 NuGet.config 中的某处添加“repositoryPath”配置(请参阅发行说明,了解放置配置文件的有效位置的说明) ),它会将所有包恢复到单个位置,但是...您的 .csproj 仍然包含以相对路径编写的程序集的提示...

我仍然不明白为什么他们要采取艰难的方式而不是更改 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>

The solution proposed in release notes for 2.1 doesn't work out-of-the-box. They forgot to mention that there is code:

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:

    <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...

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.

<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>
西瓜 2024-10-07 09:33:47

除了 Shane Kms 的回答之外,如果您已激活 Nuget Package Restore,请编辑位于 .nuget-folder 中的 NuGet.config,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <repositoryPath>..\..\ExtLibs\Packages</repositoryPath>
</configuration>

注意额外的“..\”,因为它从 .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:

<?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.

故人爱我别走 2024-10-07 09:33:47

由于缺少一些提示,这些答案都不适合我(Nuget 2.8.6),将尝试在此处添加它们,因为它可能对其他人有用。

阅读以下来源后:
https://docs.nuget.org/consume/NuGet-Config-Settings
https://github.com/NuGet/Home/issues/1346
看来要使

  1. 不同的安装包正常工作
    repositoryPath 你需要使用正向斜杠,这是因为它们使用Uri对象来解析位置。
  2. 开始时没有 $ 它仍然忽略我的设置。
  3. NuGet 会缓存配置文件,因此修改后您需要重新加载解决方案/VS。
  4. 在使用 NuGet.exe 命令设置此选项时,我也遇到了奇怪的问题,因为它修改了 AppData\Roaming\NuGet 下的全局 NuGet.exe 并开始在那里恢复包(因为该文件具有更高的优先级,只是猜测)。

例如,

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <solution>
    <add key="disableSourceControlIntegration" value="true" />
  </solution>
  <config>
    <add key="repositorypath" value="$/../../../Common/packages" />
  </config>
</configuration>

您还可以使用 NuGet 命令来确保语法正确,如下所示:

NuGet.exe config -Set repositoryPath=$/../../../Common/packages -ConfigFile NuGet.Config

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

  1. To make working Install-Package properly with different
    repositoryPath you need to use forward slashes, it's because they are using Uri object to parse location.
  2. Without $ on the begining it was still ignoring my settings.
  3. NuGet caches config file, so after modifications you need to reload solution/VS.
  4. 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).

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.exe config -Set repositoryPath=$/../../../Common/packages -ConfigFile NuGet.Config
转角预定愛 2024-10-07 09:33:47

对于 .NET Core 项目和 Visual Studio 2017,我能够通过提供以下配置将所有包恢复到相对路径:

<configuration>
  <config>
    <add key="globalPackagesFolder" value="lib" />
  </config>
  ... 
</configuration>

根据我的经验,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:

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文