NuGet 自定义配置

时间:2024-09-10 08:34:50

  默认配置:

  默认配置文件的路径%APPDATA%\NuGet\NuGet.Config (DOS) 或 $ENV:APPDATA\NuGet\NuGet.Config (PowerShell),(例如  D:\Users\username\AppData\Roaming\NuGet\NuGet.config)我们可以增加配置文件修改默认配置,

默认的配置文件如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<activePackageSource>
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
</activePackageSource>
</configuration>

  配置文件说明:(http://docs.nuget.org/docs/reference/nuget-config-settings)

  1. 自定义nuget 包路径。

  <config>
       <add key="repositorypath" value="C:\Temp" />
     </config>

  2。自动检查缺失包和自动恢复

<packageRestore>
        <!-- Allow NuGet to download missing packages -->
        <add key="enabled" value="True" />

<!-- Automatically check for missing packages during build in Visual Studio -->
        <add key="automatic" value="True" />
    </packageRestore>

  3.执行包寻找路径

  PackageSources:指定包来源地址

  DisabledPackageSources:禁止的来源地址

  ActivePackageSource:指定为”(Aggregate source)“为除了禁止的来源地址的所有包来源地址

<packageSources>
        <add key="NuGet official package source" value="https://nuget.org/api/v2/" />
        <add key="TestSource" value="C:\Temp" />
    </packageSources>
    <disabledPackageSources />
    <activePackageSource>
        <add key="All" value="(Aggregate source)"  />
    </activePackageSource>

  4。禁止源代码管理整合

  <configuration>
    <solution>
      <add key="disableSourceControlIntegration" value="true" />
    </solution>
  </configuration>

  5. 其他,代理设置 ,身份验证设置,设置API Key 访问包来源

  完整的实例

  

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<!--
Used to specify the default location to expand packages.
See: NuGet.exe help install
See: NuGet.exe help update
-->
<add key="repositorypath" value="External\Packages" />
<!--
Used to specify default source for the push command.
See: NuGet.exe help push
-->
<add key="DefaultPushSource" value="http://MyRepo/ES/api/v2/package" />
<!--
Proxy settings
-->
<add key="http_proxy" value="host" />
<add key="http_proxy.user" value="username" />
<add key="http_proxy.password" value="encrypted_password" />
</config>
<packageRestore>
<!-- Allow NuGet to download missing packages -->
<add key="enabled" value="True" /> <!-- Automatically check for missing packages during build in Visual Studio -->
<add key="automatic" value="True" />
</packageRestore>
<!--
Used to specify the default Sources for list, install and update.
See: NuGet.exe help list
See: NuGet.exe help install
See: NuGet.exe help update
-->
<packageSources>
<add key="NuGet official package source" value="https://nuget.org/api/v2/" />
<add key="MyRepo - ES" value="http://MyRepo/ES/nuget" />
</packageSources>
<!-- used to store credentials -->
<packageSourceCredentials />
<!-- Used to specify which one of the sources are active -->
<activePackageSource>
<!-- this tells only one given source is active -->
<add key="NuGet official package source" value="https://nuget.org/api/v2/" />
<!-- this tells that all of them are active -->
<add key="All" value="(Aggregate source)" />
</activePackageSource>
<!-- Used to disable package sources -->
<disabledPackageSources />
<!--
Used to specify default API key associated with sources.
See: NuGet.exe help setApiKey
See: NuGet.exe help push
See: NuGet.exe help mirror
-->
<apikeys>
<add key="http://MyRepo/ES/api/v2/package" value="encrypted_api_key" />
</apikeys>
</configuration>

  (http://docs.nuget.org/docs/reference/nuget-config-settings)

  自定义配置文件寻找规则:

  1.nuget默认配置文件链,默认配置文件(nuget.config)路径——当前磁盘根目录——当前目录。

  当前目录指:nuget.exe 执行的目录或者当前解决方案加载的目录。

  2.nuget.config配置文件读取顺序,

  例:当前解决方案的路径为c:\a\b\c,那么配置文件的加载加载规则如下 

  • c:\a\b\c\.nuget\nuget.config
  • c:\a\b\c\nuget.config
  • c:\a\b\nuget.config
  • c:\a\nuget.config
  • c:\nuget.config
  • 指定的配置文件源, %AppData%\NuGet\nuget.config.
  • 用户通过配置指定的文件

  nuget 2.6以后扩展了部分加载规则,除了上述的路径外,还会从以下路径寻找

  • %ProgramData%\NuGet\Config{IDE}{Version}{SKU}*.config, e.g. %ProgramData%\NuGet\Config\VisualStudio{VSVersion}\Pro\a.config
  • %ProgramData%\NuGet\Config{IDE}{Version}*.config
  • %ProgramData%\NuGet\Config{IDE}*.config
  • %ProgramData%\NuGet\Config*.config

  

  ( http://docs.nuget.org/docs/reference/nuget-config-file)