# BaGet 私有 NuGet 伺服器安裝與使用指南 ## 概述 BaGet 是一個輕量級的 NuGet 和符號伺服器,可以用來建立私有的 NuGet 套件倉庫。 ## 📋 前置需求 ### 1. 安裝 ASP.NET Core 3.1 Hosting Bundle(若放到Docker上則不用) - 下載連結:[ASP.NET Core 3.1.3 Hosting Bundle](https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/runtime-aspnetcore-3.1.3-windows-hosting-bundle-installer) - 安裝完成後重啟 IIS ### 2. 環境需求 - Windows Server 或 Windows 10 - IIS 已安裝並啟用 - .NET Core 3.1 Runtime ## 🚀 BaGet 安裝步驟 ### 步驟 1: 取得 BaGet 原始碼 ```bash git clone https://github.com/loic-sharma/BaGet.git ``` ### 步驟 2: 編譯專案 ```bash cd C:\YourClonePath\BaGet\src\BaGet dotnet build --configuration Release dotnet publish --configuration Release --output ./publish ``` ### 步驟 3: 設定 BaGet 配置 編輯 appsettings.json 文件,修改以下關鍵設定: ```json { "ApiKey": "abc123", // 自行設定 API 金鑰 "Database": { "Type": "Sqlite", "ConnectionString": "Data Source=C:\\BagetDB\\baget.db" }, "Storage": { "Type": "FileSystem", "Path": "C:\\BagetPkg" // 套件儲存路徑 }, "AllowPackageOverwrites": false, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning" } } } ``` ### 步驟 4: 建立必要目錄 ```bash mkdir C:\BagetDB mkdir C:\BagetPkg ``` ### 步驟 5: 部署到 IIS 1. **建立 IIS 應用程式**: - 開啟 IIS 管理員 - 右鍵點擊「Default Web Site」 - 選擇「Add Application」 - 設定 Alias: `baget` - 設定 Physical path: `C:\ProgramRelease\BaGet` 2. **設定應用程式集區**: - 建立新的應用程式集區或使用現有的 - 設定 .NET CLR Version 為「No Managed Code/沒有受控碼」 ![image](https://hackmd.io/_uploads/r1mTn3mkZl.png) 3. **驗證安裝**: - 瀏覽器開啟:`https://localhost/baget` - 如果成功會看到 BaGet 首頁 ![image](https://hackmd.io/_uploads/r1dHFnXJ-g.png) ## 📦 建立與上傳 NuGet 套件 ### SDK 專案 (推薦,方便設定多個版本Target) #### 專案檔設定 (.csproj) ```xml <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net6.0;net8.0</TargetFrameworks> <PackageId>YourLibrary</PackageId> <Version>1.0.0</Version> <Authors>Your Name</Authors> <Description>Your library description</Description> <PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageProjectUrl>https://github.com/your/repo</PackageProjectUrl> <RepositoryUrl>https://github.com/your/repo</RepositoryUrl> <GeneratePackageOnBuild>true</GeneratePackageOnBuild> </PropertyGroup> </Project> ``` #### 建立套件 ```bash dotnet pack --configuration Release ``` ### 傳統 .NET Framework 專案 #### 建立 .nuspec 檔案 ```xml <?xml version="1.0" encoding="utf-8"?> <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata> <id>YourLibrary</id> <version>1.0.0</version> <authors>Your Name</authors> <description>Your library description</description> <licenseUrl>https://opensource.org/licenses/MIT</licenseUrl> <projectUrl>https://github.com/your/repo</projectUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <dependencies> <group targetFramework=".NETFramework4.8"> <!-- 相依套件 --> </group> </dependencies> </metadata> <files> <file src="bin\Release\YourLibrary.dll" target="lib\net48\YourLibrary.dll" /> <file src="bin\Release\YourLibrary.pdb" target="lib\net48\YourLibrary.pdb" /> </files> </package> ``` #### 專案檔設定 (.csproj) - 傳統格式 ```xml <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{GUID}</ProjectGuid> <OutputType>Library</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>YourLibrary</RootNamespace> <AssemblyName>YourLibrary</AssemblyName> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> </PropertyGroup> <!-- 加入 NuGet 相關設定 --> <PropertyGroup> <PackageVersion>1.0.0</PackageVersion> <PackageId>YourLibrary</PackageId> <Authors>Your Name</Authors> <Description>Your library description</Description> </PropertyGroup> <!-- 編譯後自動產生 NuGet 套件 --> <Target Name="Pack" AfterTargets="Build"> <Exec Command="nuget pack $(ProjectPath) -Properties Configuration=$(Configuration)" /> </Target> </Project> ``` #### 建立套件 (傳統專案) ```bash # 方法 1: 使用 .nuspec nuget pack YourLibrary.nuspec # 方法 2: 直接從專案檔 nuget pack YourLibrary.csproj -Properties Configuration=Release ``` ## 📤 上傳套件到 BaGet ### 上傳指令 ```bash dotnet nuget push .\YourLibrary.1.0.0.nupkg \ --source "https://localhost:443/v3/index.json" \ --api-key "abc123" ``` ### 驗證上傳 - 重新整理 BaGet 網頁 - 應該可以看到剛上傳的套件 ## 🔧 在 Visual Studio 中使用私有 NuGet 來源 ### 步驟 1: 新增套件來源 1. 開啟 Visual Studio 2. 工具 → NuGet 套件管理員 → 套件管理員設定 3. 套件來源 → 新增 4. 名稱:`LocalBaget` 5. 來源:`https://localhost:443/v3/index.json` ### 步驟 2: 安裝套件 1. 右鍵專案 → 管理 NuGet 套件 ![image](https://hackmd.io/_uploads/rJQo5nQ1-l.png) 2. 套件來源選擇「LocalBaget」 ![image](https://hackmd.io/_uploads/Sy8-inmJWg.png) 3. 安裝您的套件 ## 🛠️ 常見問題排除 ### 權限問題 確保 IIS 應用程式集區身份對以下目錄有讀寫權限: - `C:\BagetDB` - `C:\BagetPkg` ### 套件版本衝突 如果需要覆寫套件,在 appsettings.json 中設定: ```json { "AllowPackageOverwrites": true } ```