有没有办法为每个解决方案进行 NuGet 包源设置? [英] Is there a way to make NuGet Package Source settings per solution?

查看:20
本文介绍了有没有办法为每个解决方案进行 NuGet 包源设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道让 Visual Studio 为每个解决方案而不是所有解决方案应用 NuGet 包源配置的方法?我一直遇到版本控制问题,因为我处理多个项目,每个项目都有自己的私有 NuGet 存储库.一直记住哪个 NuGet 存储库属于哪个项目并返回并将正确的存储库应用于正确的项目是一种痛苦.

Does anyone know of a way to make Visual Studio apply the NuGet package sources configuration per solution instead of across all solutions? I keep having versioning problems because I work on multiple projects that each have their own private NuGet repositories. It's a pain in the *** to keep remembering which NuGet repo belongs with which project and going back and applying the correct one to the correct project.

推荐答案

TLDR: Yes

NuGet 使用包源的分层应用程序,从 Windows 用户配置文件级别的 NuGet.config 开始,然后从包含解决方案的文件路径的根目录开始应用越来越精细的配置,最后以包含您的解决方案文件的目录.

NuGet uses a hierarchical application of package sources starting with the NuGet.config at the level of your Windows User Profile and then applying more and more granular configuration starting at the root of the file path that contains your solution, finally ending with the directory containing your solution file.

这就是我设法弄清楚的——感谢一位乐于助人的 Twitter 用户向我指出这份文件:

So here's what I've managed to figure out - courtesy of a helpful Twitterer pointing me to this document:

https://docs.nuget.org/consume/nuget-config-file

当您在 Visual Studio 的 Tools > 中编辑 NuGet 包源时NuGet 包管理器 >包管理器设置:包源 选项,默认情况下将这些更改应用到您的 %APPDATA%NuGet 目录中的 NuGet.config 文件.要在每个解决方案(或每个解决方案组)的基础上覆盖这些设置,您需要在解决方案路径的某个位置添加一个战略性放置的 NuGet.config 文件.

When you edit the NuGet package sources in Visual Studio's Tools > NuGet Package Manager > Package Manager Settings: Package Sources option, it applies those changes by default to the NuGet.config file found in your %APPDATA%NuGet directory. To override these settings on a per-solution (or per group of solutions) basis, you need to add a strategically placed NuGet.config file somewhere along the path of your solution or solutions.

如果您阅读 NuGet 文档,一切都会变得清晰,我在下面提供的解决方案将允许您快速为单个 Visual Studio 解决方案指定配置:

All will become clear if you read the NuGet document, the solution I provide below will quickly allow you to specify a configuration for a single Visual Studio solution:

  1. 导航到 %APPDATA%NuGet 并获取 NuGet.config 的副本
  2. 将副本转储到解决方案的根目录 - 即 Application.sln 所在的位置.
  3. 通过编辑副本覆盖应用到您的用户配置文件的默认设置,使其仅包含与此解决方案相关的 NuGet 包源 - 例如,包含此解决方案的专有包但不应应用的私有 NuGet 源到其他项目 - 例如:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>

  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>

  <packageSources>

    <!-- Ditch all the Global NuGet package sources we only want a 
         single private NuGet repo for this project -->
    <clear />

    <!-- Add the private NuGet package source for this solution -->
    <add key="My Private NuGet Server" value="http://myprivatenuget.com:8080/nuget" />

  </packageSources>

  <disabledPackageSources>

    <!-- Add any package sources to ignore here using the same keys as 
         defined in the packageSources list above-->

    <!--<add key="nuget.org" value="true" />-->

    <add key="Microsoft and .NET" value="true" />

  </disabledPackageSources>

</configuration>

如果您希望将配置应用于多个解决方案,请确保您的解决方案文件夹都包含在一个公共目录中,并将与这些解决方案相关的包源的 NuGet.config 放在该公共目录中,确保所有解决方案文件夹用于使用这些包源的项目不包含在这个公共文件夹中.

If you want a configuration to apply to multiple solutions, ensure your solution folders are all contained within a common directory and put the NuGet.config for the package sources relevant for those solutions in that common directory, ensuring that any solution folders for projects that aren't to use these package sources are not contained in this common folder.

这篇关于有没有办法为每个解决方案进行 NuGet 包源设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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