有没有办法根据解决方案进行NuGet软件包源设置?

有没有办法根据解决方案进行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:是

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.

因此,这就是我设法弄清楚的-由一位有用的Twitterer致使我指向此文档:

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 Package Manager > Package Manager Settings: Package Sources选项中编辑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.