- A+
参考:dotnet publish 命令 - .NET CLI | Microsoft Docs
最近使用VS2019和VS2022,发布 AOT时,总是提示失败,要好几回才成功,没得办法,自己搞吧,反复重试总是能成功的,改改就可以在持续集成中打包了,也免得每个项目都要创建发布脚本FolderProfile.pubxml
拿走不谢
在外部工具中创建一个命令
命令和参数如下
@@@code
C:WindowsSystem32WindowsPowerShellv1.0powershell.exe
-ExecutionPolicy RemoteSigned -File "c:qoushuidlltoolspublish_core.ps1" "$(ProjectDir)$(ProjectFileName)"
@@#
PS脚本内容如下:
@@@code
Param(
[string]$path
)
# $path="D:codeZooyPatrolCoresrcPatrolPluginsPatrolPlugins.DBCorePatrolPlugins.DBCore.csproj "
# 变量定义
$rarTool="C:Program FilesWinRARwinrar.exe"
$publishDir="d:temppublish"
$dotnetVer="net5.0" # netcoreapp3.1 net48
$toolPath= Split-Path -Parent $MyInvocation.MyCommand.Definition
$slnPath =[System.IO.Path]::GetDirectoryName($path)
$name = [System.IO.Path]::GetFileNameWithoutExtension($path)
# 以项目名为发布文件夹名
$publishDir = [System.IO.Path]::Combine($publishDir,$name)
Write-Host "发布NETCORE项目" $path "至" $publishDir -ForegroundColor Yellow
# 选择发布模式
Write-Host "选择发布模式,1 可移植 2 单文件 4 单文件独立运行时 7 全部 "
$type = Read-Host "请选择"
Set-Location $slnPath
$cmd = " -c Release "
[System.Collections.Generic.Dictionary[int,String]]$mDic = @{}
# 发布可移植包
$mDic.Add(1,"-f $dotnetVer")
# 发布可移植单一包
$mDic.Add(2,"-r win-x64 --self-contained false /p:PublishSingleFile=true")
# 发布包含运行时的可移植单一包
$mDic.Add(4,"-r win-x64 --self-contained true /p:PublishSingleFile=true")
[System.Collections.Generic.Dictionary[int,String]]$mDicOutput = @{}
$mDicOutput.Add(1,"portable")
# 发布可移植单一包
$mDicOutput.Add(2,"portable_aot")
# 发布包含运行时的可移植单一包
$mDicOutput.Add(4,"aot")
$numbers = (1,2,4)
foreach($i in $numbers){
if($type -eq $i -or $type -eq 7)
{
$retry =5
$v= $mDicOutput[$i]
$trg= [System.IO.Path]::Combine($publishDir, $v)
# 清除现有文件
if ([System.IO.Directory]::Exists($trg))
{
Remove-Item $trg -Recurse -Force
}
$v = $mDic[$i]
# Write-Host $v
$tmp =" dotnet publish `"$path`" -o `"$trg`" $cmd $v "
# Write-Host $tmp
# 检查是否失败,重试5次
do {
$retry = $retry -1
$b =0
$v= $mDicOutput[$i]
if(-not [System.IO.Directory]::Exists($trg)){
$b=1
Write-Host "$v 生成" -ForegroundColor Yellow
}else{
if ([System.IO.Directory]::GetFiles($trg,"*").Length -lt 2){
$b=2
Write-Host "$v 生成失败,重试" -ForegroundColor Red
}
}
if($b -gt 0)
{
& cmd /c " taskkill /F /IM `"dotnet.exe`""
& timeout /T 5 /NOBREAK
& cmd /c " $tmp "
}
} while( $retry -gt 0)
}
}
# 统计汇报以及压缩
foreach($i in $numbers){
$v= $mDicOutput[$i]
$trg= [System.IO.Path]::Combine($publishDir, $v)
if($type -eq $i -or $type -eq 7)
{
$b =1
if(-not [System.IO.Directory]::Exists($trg)){
$b= 0
Write-Host "$v 未生成" -ForegroundColor Red
}else{
if ([System.IO.Directory]::GetFiles($trg,"*").Length -lt 2){
$b=2
Write-Host "$v 生成失败" -ForegroundColor Red
}
}
if ($b -eq 1){
$zip="$publishDir$name_$v.zip"
if ([System.IO.Directory]::Exists($zip))
{
Remove-Item $zip -Force
}
& cmd /c " `"$rarTool`" a -EP1 -r -x*.Development.json -x*.pdb $zip $trg* "
}
}
}
Start-Process explorer "$publishDir"
pause
@@#