共用方式為


常用的 NuGet 組態

NuGet 的行為是由一或多個組態 (XML) 檔案中累積的設定所驅動,這些設定可以存在於解決方案 (專案,如果未使用解決方案)、使用者和電腦全層級。

設定檔位置和用途

Scope NuGet.Config 檔案位置 Description
Solution 目前資料夾 (又名解決方案資料夾) 或驅動器根目錄之前的任何資料夾。 在解決方案資料夾中,設定會套用至子資料夾中的所有專案。 請注意,如果設定檔放置在專案資料夾中,則它對該專案沒有影響。 在命令列上還原專案時,專案的目錄會被視為解決方案目錄,這可能會導致還原專案與解決方案時行為差異。
User Windows:%appdata%\NuGet\NuGet.Config
Mac/Linux:~/.config/NuGet/NuGet.Config~/.nuget/NuGet/NuGet.Config (因工具而異)
所有平台都支援其他設定。 工具無法編輯這些組態。
視窗:%appdata%\NuGet\config\*.Config
Mac/Linux:~/.config/NuGet/config/*.config~/.nuget/config/*.config
設定會套用至所有操作,但任何方案層級的設定將覆寫這些設定。
Computer Windows:%ProgramFiles(x86)%\NuGet\Config
Mac/Linux:/etc/opt/NuGet/Config (Linux) 或 /Library/Application Support (Mac) 預設為。 如果 $NUGET_COMMON_APPLICATION_DATA 既不是空值也不是空的,則改為使用 $NUGET_COMMON_APPLICATION_DATA/NuGet/Config
設定會套用至電腦上的所有作業,但會被任何使用者或方案層級設定覆寫。

備註

在 Mac/Linux 上,使用者設定檔位置會因工具而異。 .NET CLI 使用 ~/.nuget/NuGet 資料夾,而 Mono 使用 ~/.config/NuGet 資料夾。

在 Mac/Linux 上,使用者層級設定檔位置會因工具而異

在 Mac/Linux 上,使用者設定檔位置會因工具而異。 大多數使用者使用工具在資料夾下 ~/.nuget/NuGet 尋找使用者設定檔。 這些其他工具會在資料夾下 ~/.config/NuGet 尋找使用者設定檔:

  • Mono
  • NuGet.exe
  • Mac 版 Visual Studio 2019 (和舊版)
  • 適用於 Mac 的 Visual Studio 2022 (和更新版本),僅在處理傳統 Mono 專案時。

如果您使用的工具涉及這兩個位置,請考慮遵循下列步驟來合併它們,以允許您僅使用一個使用者層級組態檔:

  1. 檢查兩個使用者層級設定檔的內容,並將您想要的設定檔保留在資料夾下 ~/.nuget/NuGet
  2. 將符號連結從~/.nuget/NuGet設定至~/.config/NuGet。 例如,執行 bash 命令: ln -s ~/.nuget/NuGet ~/.config/NuGet

舊版 NuGet 的注意事項:

  • NuGet 3.3 和更早版本會使用 .nuget 資料夾進行全解決方案設定。 此資料夾不會用於 NuGet 3.4+。
  • 針對 NuGet 2.6 到 3.x,Windows 上的電腦層級組態檔位於 %ProgramData%\NuGet\Config[\{IDE}[\{Version}[\{SKU}]]]\NuGet.Config,其中 {IDE} 可以是 VisualStudio{Version} 是 Visual Studio 版本,例如 14.0,並且 {SKU}CommunityProEnterprise。 若要將設定移轉至 NuGet 4.0+,只要將組態檔複製到 %ProgramFiles(x86)%\NuGet\Config。 在 Linux 上,這個先前的位置是 /etc/opt,而在 Mac 上, /Library/Application Support.

變更設定

NuGet.Config檔案是包含索引鍵/值組的簡單 XML 文字檔,如 NuGet 組態設定主題中所述。

設定是使用 NuGet CLI 設定命令來管理:

  • 依預設,會變更使用者層級設定檔。 (在 Mac/Linux 上,使用者層級設定檔的位置會因工具而異)
  • 若要變更不同檔案中的設定,請使用 -configFile 切換。 在這種情況下,文件可以使用任何文件名。
  • 按鍵一律區分大小寫。
  • 需要提升權限才能變更電腦層級的設定檔案設定。

警告

雖然您可以在任何文字編輯器中修改檔案,但 NuGet (v3.4.3 和更新版本) 會以無訊息方式忽略整個組態檔,如果它包含格式不正確的 XML (不相符的標籤、無效的引號等) 。 這就是為什麼最好使用 nuget config管理設定。

設定值

窗戶:

# Set globalPackagesFolder in the user-level config file
dotnet nuget config set globalPackagesFolder "C:\packages"

# Set repositoryPath (available for packages.config only) in the user-level config file
dotnet nuget config set repositoryPath "C:\packages"

# Set repositoryPath in solution-level files
dotnet nuget config set repositoryPath "C:\packages" --configfile "C:\my.config"
dotnet nuget config set repositoryPath "c:\packages" --configfile "..\..\my.config"

# Set repositoryPath in the computer-level file (requires elevation)
dotnet nuget config set repositoryPath "c:\packages" --configfile "%appdata%\NuGet\NuGet.Config"

Mac/Linux:

# Set globalPackagesFolder in the user-level config file
dotnet nuget config set globalPackagesFolder /home/packages

# Set repositoryPath (available for packages.config only) in the user-level config file
dotnet nuget config set repositoryPath /home/packages

# Set repositoryPath in solution-level files
dotnet nuget config set repositoryPath /home/projects/packages --configfile /home/my.Config
dotnet nuget config set repositoryPath /home/packages --configfile home/myApp/NuGet.Config

# Set repositoryPath in the computer-level file (requires elevation)
dotnet nuget config set repositoryPath /home/packages --configfile $XDG_DATA_HOME/NuGet.Config

備註

在 NuGet 3.4 和更新版本中,您可以使用任何值中的環境變數,例如 repositoryPath=%PACKAGEHOME% (Windows) 和 repositoryPath=$PACKAGEHOME (Mac/Linux)。

移除一個值

若要移除值,請指定具有空白值的索引鍵。

# Windows
nuget config -set repositoryPath= -configfile c:\my.Config

# Mac/Linux
nuget config -set repositoryPath= -configfile /home/my.Config

建立新的設定檔

使用 .NET CLI,執行 dotnet new nugetconfig來建立預設 nuget.config。 如需詳細資訊,請參閱 dotnet CLI 命令

或者,手動將下面的模板複製到新文件中,然後使用 nuget config -configFile <filename> 來設置值:

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

如何套用設定

多個 NuGet.Config 檔案可讓您將設定儲存在不同的位置,以便將它們套用至單一解決方案或一組解決方案。 這些設定會共同套用至從命令列或 Visual Studio 呼叫的任何 NuGet 作業,其中位置「最接近」解決方案或當前資料夾的設定會優先適用。 如果在專案檔案上使用命令列工具,而不是解決方案檔案,則專案目錄會用作「解決方案目錄」,當解決方案檔案的子目錄中有檔案時 NuGet.Config ,可能會導致行為不一致。

具體而言,當未在命令列上明確指定組態檔時,NuGet 會依下列順序從不同的組態檔載入設定:

  1. 不常見)此 NuGetDefaults.Config 檔案包含僅與套件來源相關的設定。
  2. 電腦層級檔案。
  3. 使用者層級檔案。
  4. 在從磁碟驅動器根目錄到目前資料夾的路徑中的每個資料夾中找到的檔案 (叫用的位置 nuget.exe 或包含 Visual Studio 解決方案的資料夾)。 例如,如果在 c:\A\B\C 中叫用命令,NuGet 會先在 c:\、接著 c:\A、然後 c:\A\B,最後在 c:\A\B\C 尋找並載入組態檔。

當在命令列上明確指定設定檔時 (例如 nuget -configFile my.configdotnet restore --configfile my.config),只會使用指定檔案中的設定。

當 NuGet 在這些檔案中尋找設定時,會套用這些設定,如下所示:

  1. 針對單一專案元素,NuGet 會取代相同索引鍵的任何先前找到的值。 這表示「最接近」目前資料夾或解決方案的設定會覆寫先前找到的任何其他設定。 例如,在 NuGetDefaults.Config 中的 defaultPushSource 設定如果存在於任何其他設定檔中,則會被覆寫。
  2. 針對集合元素 (例如 <packageSources>),NuGet 會將所有組態檔的值合併成單一集合。
  3. 指定節點具有 <clear /> 時,NuGet 會忽略先前針對該節點所定義的組態值。

小提示

在解決方案存放庫的根目錄中新增檔案 nuget.config 。 這被視為最佳做法,因為它會提升可重複性,並確保不同的使用者具有相同的 NuGet 設定。

設定導覽

假設您在兩個不同的磁碟機上具有下列資料夾結構:

disk_drive_1
    User
disk_drive_2
    Project1
        Source
    Project2
        Source
    tmp

然後,您會在下列位置有四個 NuGet.Config 檔案,其中包含指定的內容。 (電腦層級檔案不包含在此範例中,但其行為與使用者層級檔案類似。

檔案 A. 使用者層級檔案 (%appdata%\NuGet\NuGet.Config 在 Windows 上, ~/.config/NuGet/NuGet.Config 在 Mac/Linux 上):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

檔案 B. disk_drive_2/NuGet.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <config>
        <add key="repositoryPath" value="disk_drive_2/tmp" />
    </config>
    <packageRestore>
        <add key="enabled" value="True" />
    </packageRestore>
</configuration>

檔案 C. disk_drive_2/Project1/NuGet.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <config>
        <add key="repositoryPath" value="External/Packages" />
        <add key="defaultPushSource" value="https://MyPrivateRepo/ES/api/v2/package" />
    </config>
    <packageSources>
        <clear /> <!-- ensure only the sources defined below are used -->
        <add key="MyPrivateRepo - ES" value="https://MyPrivateRepo/ES/nuget" />
    </packageSources>
</configuration>

檔案 D. disk_drive_2/Project2/NuGet.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <!-- Add this repository to the list of available repositories -->
        <add key="MyPrivateRepo - DQ" value="https://MyPrivateRepo/DQ/nuget" />
    </packageSources>
</configuration>

然後,NuGet 會載入並套用設定,如下所示,視叫用的位置而定:

  • disk_drive_1/users觸發:只會使用用戶層級的設定檔(A)中列出的預設儲存庫,因為這是disk_drive_1上找到的唯一檔案。

  • disk_drive_2/disk_drive_/tmp 呼叫: 先載入使用者層級檔案(A),然後 NuGet 會移至 disk_drive_2 的根目錄並找到檔案(B)。 NuGet 也會在中 /tmp 尋找組態檔,但找不到。 因此,使用nuget.org上的預設儲存庫,啟用套件還原,並在disk_drive_2/tmp中展開套件。

  • 用來源 disk_drive_2/Project1disk_drive_2/Project1/Source:會先載入使用者層級檔案 (A),然後 NuGet 會從根目錄disk_drive_2載入檔案 (B),然後是檔案 (C)。 (C)中的設定會覆寫(B)和(A)中的設定,因此套件的安裝位置是disk_drive_2/Project1/External/Packages而不是disk_drive_2/tmp。 此外,由於 (C) 清除 <packageSources>,nuget.org 不再作為來源,僅留下 https://MyPrivateRepo/ES/nuget

  • disk_drive_2/Project2被叫用disk_drive_2/Project2/Source後:會先載入使用者層級檔案(A),然後依序載入檔案(B)和檔案(D)。 因為未清除packageSourcesnuget.orghttps://MyPrivateRepo/DQ/nuget都可以作為來源。 按照(B)中的指定,套件會在disk_drive_2/tmp展開。

其他使用者範圍的設定

從 5.7 開始,NuGet 已新增其他使用者範圍組態檔的支援。 這可讓第三方廠商新增其他使用者組態檔,而無需提高權限。 這些組態檔位於子資料夾內 config 的標準使用者範圍組態資料夾中。 所有以.config.Config結尾的檔案都會被考慮。 這些檔案無法由標準工具編輯。

作業系統平台 其他配置
窗戶 %appdata%\NuGet\config\*.Config
Mac/Linux ~/.config/NuGet/config/*.config~/.nuget/config/*.config

NuGet 預設檔案

NuGetDefaults.Config 是不常見的,只能指定安裝和更新套件的來源,或使用 nuget push 設定發佈套件的預設目標。

因為系統管理員可以方便地 (例如使用群組原則) 將一致 NuGetDefaults.Config 的檔案部署到開發人員和建置電腦,所以他們可以確保組織中的每個人都使用一致的套件來源,無論這是否包含 nuget.org。

這很重要

NuGetDefaults.Config 檔案絕不會導致從開發人員的 NuGet 設定中移除套件來源。 這表示如果開發人員已使用 NuGet,因此已註冊 nuget.org 套件來源,則在建立 NuGetDefaults.Config 檔案之後不會移除它。

此外,NuGet 中的任何其他機制都無法 NuGetDefaults.Config 阻止存取套件來源,例如 nuget.org。如果組織希望阻止此類訪問,則必須使用防火牆等其他方式來執行此操作。

NuGetDefaults.Config 位置

下表說明檔案的儲存位置 NuGetDefaults.Config ,視目標作業系統而定:

作業系統平台 NuGetDefaults.Config 位置
窗戶 Visual Studio 2017 或 NuGet 4.x+:%ProgramFiles(x86)%\NuGet
Visual Studio 2015 和更早版本或 NuGet 3.x 和更早版本:%PROGRAMDATA%\NuGet
Mac/Linux $XDG_DATA_HOME (通常 ~/.local/share/usr/local/share,視作業系統發行版本而定)

NuGetDefaults.Config 設定

  • packageSources:此集合與一般設定檔中的含義 packageSources 相同,並指定預設來源。 NuGet 會在專案使用 packages.config 管理格式時,依序使用來源以安裝或更新套件。 對於使用 PackageReference 格式的專案,NuGet 會先使用本機來源,然後使用網路共用上的來源,然後使用 HTTP 來源,而不論組態檔中的順序為何。 NuGet 一律會忽略具有還原作業的來源順序。

  • disabledPackageSources:此集合也具有與檔案中的 NuGet.Config 相同含義,其中每個受影響的來源都按其名稱和 true/false 指示其是否已停用的值列出。 這允許來源名稱和 URL 保留在 packageSources 中,且預設不會開啟。 然後,個別開發人員可以將來源的值設定為 在 false 其他 NuGet.Config 檔案中,以重新啟用來源,而不需要再次尋找正確的 URL。 這對於為開發人員提供組織內部來源 URL 的完整清單也很有用,同時預設情況下僅啟用個別團隊的來源。

  • defaultPushSource:針對 nuget push 作業指定預設目標,覆寫內建的 nuget.org 預設值。 管理員可以部署此設置,以避免意外將內部套件發佈給公眾 nuget.org ,因為開發人員特別需要使用 nuget push -Source 發佈到 nuget.org.

NuGetDefaults.Config 和應用程式範例

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- defaultPushSource key works like the 'defaultPushSource' key of NuGet.Config files. -->
    <!-- This can be used by administrators to prevent accidental publishing of packages to nuget.org. -->
    <config>
        <add key="defaultPushSource" value="https://contoso.com/packages/" />
    </config>

    <!-- Default Package Sources; works like the 'packageSources' section of NuGet.Config files. -->
    <!-- This collection cannot be deleted or modified but can be disabled/enabled by users. -->
    <packageSources>
        <add key="Contoso Package Source" value="https://contoso.com/packages/" />
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    </packageSources>

    <!-- Default Package Sources that are disabled by default. -->
    <!-- Works like the 'disabledPackageSources' section of NuGet.Config files. -->
    <!-- Sources cannot be modified or deleted either but can be enabled/disabled by users. -->
    <disabledPackageSources>
        <add key="nuget.org" value="true" />
    </disabledPackageSources>
</configuration>