Skip to content

发布Nuget包

约 1181 字大约 4 分钟

NET

2024-12-27

打包

不是 netframework 的版本 直接通过项目内属性中的包选项即可打包

  • 1.下载netget工具NuGet Gallery | Downloads 下载后将工具目录加入环境变量中
  • 2.生成包,运行nuget spec 修改生成的.nuspec文件内容
    <?xml version="1.0" encoding="utf-8"?>
    <package >
    	<metadata>
    		<id>Yoloczita</id>
    		<version>1.0.1</version>
    		<title>Yoloczita</title>
    		<authors>yuchen</authors>
    		<readme>README.md</readme>
    		<requireLicenseAcceptance>false</requireLicenseAcceptance>
    		<license type="expression">MIT</license>
    		<!-- <icon>icon.png</icon> -->
    		<projectUrl>https://github.com/YuChenDayCode/Yoloczita</projectUrl>
    		<description>.NET Framework 4.6.1下使用Yolov5、v11的onnx模型进行目标检测。</description>
    		<releaseNotes>添加依赖包</releaseNotes>
    		<copyright>Copyright © 2024 Yuchen</copyright>
    		<tags>netframework yolov5 yolov11</tags>
    		<dependencies>
    			<group targetFramework=".NETFramework4.6.1">
    				<dependency id="Microsoft.ML.OnnxRuntime" version="1.20.1" exclude="Compile,Build,Analyzers" />
    
    			</group>
    		</dependencies>
    	</metadata>
    	<files>
    		<file src="README.md"  />
    	</files>
    </package>
  • 3.打包 nuget pack 注意依赖包的联动和版本
  • 4.发布
    • 在nuget网站直接上传 Nuget Upload
    • 使用netget cli 上传

dotnet nuget

推荐使用这个,更新且跨平台


dotnet nuget add source https://192.168.3.60:8081/repository/nuget-hosted/index.json -n NexusNuget --username admin --password df@20231208  --store-password-in-clear-text

dotnet push <name> -s <sources> -k <key> #设置了账号密码就不用-k了

################ help ############################
  -h|--help                      Show help information
  --force-english-output         使用不变的基于英语的区域性强制应用程序运行。
  -s|--source <source>           要使用的包源(URL、UNC/文件夹路径或包源名称)。如果在 NuGet.Config 中指定,则默认为 DefaultPushSource。
  -ss|--symbol-source <source>   要使用的符号服务器 URL。
  -t|--timeout <timeout>         推送到服务器的超时值(以秒为单位)。默认为 300(5 分钟)
  -k|--api-key <apiKey>          服务器的 API 密钥。
  -sk|--symbol-api-key <apiKey>  符号服务器的 API 密钥。
  -d|--disable-buffering         推送到 HTTP(S) 服务器时禁用缓存可减少内存使用。
  -n|--no-symbols                如果存在符号包,系统不会将该符号包推送到符号服务器。
  --no-service-endpoint          请勿将 "api/v2/package" 追加到源 URL。
  --interactive                  对于身份验证等操作,允许命令阻止并要求手动操作。
  --skip-duplicate               如果包和版本已存在,则跳过它并继续推送中的下一个包(若有)

结合Directory.Build.props对项目进行nuget构建

Directory.Build.props

Directory.Build.props 是 MSBuild(Microsoft Build Engine)的一个特殊文件,用于在 .NET 项目构建过程中定义共享的构建属性和配置。它通常放置在解决方案或项目的根目录,供多个项目(.csproj 文件)自动继承,从而简化配置管理、确保一致性并减少重复代码。

主要作用
  • 统一配置:为所有项目设置相同的版本号、目标框架、包元数据等。
  • 减少重复:避免在每个 .csproj 文件中重复定义相同的属性。
  • 一致性保证:确保所有项目使用相同的编译选项(如 Nullable、TreatWarningsAsErrors)。
  • 简化维护:集中管理依赖版本、打包规则等。
  • 自定义构建:添加共享的 MSBuild 目标或任务。

工作原理

加载机制
  • MSBuild 在构建项目时,会从项目文件所在的目录开始,向上递归查找 Directory.Build.props 文件。
  • 找到的第一个 Directory.Build.props 文件会被导入到所有子目录的 .csproj 文件中。
  • 属性和项组(PropertyGroup 和 ItemGroup)会自动应用于项目,除非项目文件显式覆盖。

使用Nuget cli上传

前置

  • 推送格式: http://your-nexus-server:8081/repository/nuget-hosted/
  • 使用API Key
    • 启用 API Key Realm
      • Security > Realms
      • NuGet API-Key Realm 从“Available”移到“Active”
      • 设置APIkey

nuget push MorphoScan.Core.1.0.0.nupkg -Source NexusNuGet -Apikey 账号:密码nuget push MorphoScan.Core.1.0.0.nupkg -Source dfhost -Apikey xx:**

dotnet nuget push MorphoScan.Common.Core.0.0.1.nupkg -s dfhost -k de969a59-0aa1-3ca5-a9f1-258fdd0d85e6

自动化脚本

# build-and-pack.ps1
$defaultVersion = "0.0.1"
$source = "NexusNuget"

# 1.从 Directory.Build.props 读取默认版本
$dirPropsPath = "Directory.Build.props"
if (Test-Path $dirPropsPath) {
    $xml = [xml](Get-Content $dirPropsPath)
    $versionPrefixNode = $xml.Project.PropertyGroup | Where-Object { $_.VersionPrefix -ne $null } | Select-Object -First 1 -ExpandProperty VersionPrefix
    if ($versionPrefixNode -and $versionPrefixNode.InnerText) {
        $defaultVersion = $versionPrefixNode.InnerText.Trim()
        Write-Host "从 Directory.Build.props 读取默认版本: $defaultVersion" -ForegroundColor Blue
    } else {
        Write-Host "Directory.Build.props 中未设置 VersionPrefix,使用备用默认版本: $defaultVersion" -ForegroundColor DarkGray
    }
}

# 2.生成项目
dotnet build -c Release

 Write-Host "--------------------开始生成打包--------------------------" -ForegroundColor Cyan

$projects = @(
    "MorphoScan.Common.Core",
    "MorphoScan.Common.Infrastructure",
    "MorphoScan.Common.UiCore"
)

foreach ($proj in $projects) {
 
    $projPath = "$proj\$proj.csproj"
    
    $xml = [xml](Get-Content $projPath)
    $versionNode = $xml.Project.PropertyGroup | Where-Object { $_.Version -ne $null } | Select-Object -First 1 -ExpandProperty Version
    
    if ($versionNode -and $versionNode.Trim()) {
        $actualVersion = $versionNode.Trim()
        Write-Host "📌 [$proj] 使用项目自定义版本: $actualVersion" -ForegroundColor Blue
    } else {
        $actualVersion = $defaultVersion
        Write-Host "📌 [$proj] 未设置版本,使用默认版本: $actualVersion" -ForegroundColor DarkGray
    }


    # 📦 打包(传递实际版本)
    # 设置了GeneratePackageOnBuild build时会自动打包
    #Write-Host "📦 Packing $proj v$actualVersion..." -ForegroundColor Green
    #dotnet pack $projPath -c Release -p:Version=$actualVersion

    # 🚀 推送(注意路径和版本号匹配)
    $nupkgPath = "$proj/bin/Release/$proj.$actualVersion.nupkg"

    #Write-Host "📦 Packing $proj..." -ForegroundColor Green
    # dotnet pack "$proj\$proj.csproj" -c Release -p:Version=$version
    if (Test-Path $nupkgPath) {
        Write-Host "🚀 推送 【$proj v$actualVersion】 到 【$source】..." -ForegroundColor Red
        dotnet nuget push $nupkgPath -s $source
    } else {
        Write-Error "❌ 未找到包: $nupkgPath"
        #exit 1
    }
    Write-Host "`n `n "
    #Write-Host "🚀 Pushing $proj..." -ForegroundColor Yellow
    #dotnet nuget push "$proj/bin/Release/$proj.$version.nupkg" --source $source
}
Read-Host "按 Enter 键继续..."