# DMS 一体化构建脚本(Java 版) # 用法:在项目根目录执行 .\build\build-java.ps1 # 产物:dist-package-java\dms-server\(dms-server.jar 内置前端) [CmdletBinding()] param( [switch]$SkipFrontend ) $ErrorActionPreference = "Stop" $ProjectRoot = Split-Path $PSScriptRoot -Parent $Backend = Join-Path $ProjectRoot "backend-java" $Frontend = Join-Path $ProjectRoot "frontend" $Dist = Join-Path $ProjectRoot "dist-package-java" $Templates = Join-Path $PSScriptRoot "java-templates" Write-Host "========== DMS 一体化构建(Java 版) ==========" -ForegroundColor Cyan Write-Host "项目根目录: $ProjectRoot" # ---------- 1. 前端构建 ---------- if (-not $SkipFrontend) { Write-Host "`n[1/4] 构建前端..." -ForegroundColor Yellow Push-Location $Frontend try { if (-not (Test-Path "node_modules")) { Write-Host " npm install..." -ForegroundColor DarkGray npm install if ($LASTEXITCODE -ne 0) { throw "npm install 失败" } } npm run build if ($LASTEXITCODE -ne 0) { throw "前端构建失败" } } finally { Pop-Location } } else { Write-Host "`n[1/4] 跳过前端构建 (-SkipFrontend)" -ForegroundColor DarkGray } $indexHtml = Join-Path $Frontend "dist\index.html" if (-not (Test-Path $indexHtml)) { throw "前端产物不存在: $indexHtml - 请先执行 npm run build" } # ---------- 2. Maven 打包(-Pfrontend 把前端 dist 合入 jar 的 static) ---------- Write-Host "`n[2/4] Maven 打包..." -ForegroundColor Yellow Push-Location $Backend try { mvn -q clean package -Pfrontend if ($LASTEXITCODE -ne 0) { throw "Maven 打包失败" } } finally { Pop-Location } $JarFile = Join-Path $Backend "target\dms-server.jar" if (-not (Test-Path $JarFile)) { throw "未找到打包产物: $JarFile" } # ---------- 3. 整理发行包 ---------- Write-Host "`n[3/4] 整理发行包..." -ForegroundColor Yellow $ReleaseDir = Join-Path $Dist "dms-server" # 暂存现有 .env 与 dms-storage:删除发行目录前移出,重打包后自动还原, # 避免用户配置与运行数据每次丢失(dms-storage 用移动而非复制,同卷瞬时完成) $KeepEnv = Join-Path $Dist ".env.keep" $KeepStorage = Join-Path $Dist "dms-storage.keep" if (Test-Path (Join-Path $ReleaseDir ".env")) { Copy-Item (Join-Path $ReleaseDir ".env") $KeepEnv -Force } # 备份管理配置(界面保存的 rsync 地址等)同样暂存,避免重打包丢失 $KeepBackupCfg = Join-Path $Dist "dms-backup-config.keep" if (Test-Path (Join-Path $ReleaseDir "dms-backup-config.json")) { Copy-Item (Join-Path $ReleaseDir "dms-backup-config.json") $KeepBackupCfg -Force } if (Test-Path (Join-Path $ReleaseDir "dms-storage")) { if (Test-Path $KeepStorage) { Remove-Item $KeepStorage -Recurse -Force } try { Move-Item (Join-Path $ReleaseDir "dms-storage") $KeepStorage -ErrorAction Stop } catch { # dms-storage 被运行中的服务占用(文件句柄未释放):中止打包而非删数据 throw "dms-storage 暂存失败(可能被运行中的服务占用):$_ - 请先停止 dms-server 再打包" } } if (Test-Path $ReleaseDir) { try { Remove-Item $ReleaseDir -Recurse -Force -ErrorAction Stop } catch { # 目录被其他进程占用(如命令行窗口停留在该目录内):保留目录节点,清空内容 Write-Warning "发行目录被其他进程占用,改为清空目录内容后继续" Get-ChildItem $ReleaseDir -Force | Remove-Item -Recurse -Force } } New-Item $ReleaseDir -ItemType Directory -Force | Out-Null Copy-Item $JarFile $ReleaseDir -Force # 配置模板:现役 Java 版专属 .env.example(Python 版已废弃且两份已漂移, # backend-java 版含 DMS_SSO_* 单点登录代理段,勿再指向 backend\) Copy-Item (Join-Path $ProjectRoot "backend-java\.env.example") $ReleaseDir -Force # 启动/升级脚本与说明 Copy-Item (Join-Path $Templates "run.bat") $ReleaseDir -Force Copy-Item (Join-Path $Templates "run.sh") $ReleaseDir -Force Copy-Item (Join-Path $Templates "upgrade-db.bat") $ReleaseDir -Force Copy-Item (Join-Path $Templates "upgrade-db.sh") $ReleaseDir -Force Copy-Item (Join-Path $Templates "README.txt") $ReleaseDir -Force # rsync 备份工具:Windows 发行包随包携带 cwRsync(rsync.exe + cygwin dll), # RSYNC 备份模式开箱即用;Linux 用系统 rsync 无需二进制(探测顺序见 # RsyncBackupTarget.locateExecutable:DMS_RSYNC_EXECUTABLE → PATH → tools/rsync) $RsyncTools = Join-Path $PSScriptRoot "tools\rsync" if (Test-Path $RsyncTools) { Copy-Item $RsyncTools (Join-Path $ReleaseDir "tools\rsync") -Recurse -Force Write-Host " 已内置 rsync 备份工具(tools\rsync)" -ForegroundColor DarkGray } else { Write-Warning "未找到 $RsyncTools,发行包不含内置 rsync(RSYNC 备份模式需目标机自装或配置 DMS_RSYNC_EXECUTABLE)" } # 示例数据库 SQL(与后端语言无关,复用同一导出工具) # 工具三参数均必填:--env-file 提供库连接、--storage 提供文件根、--output 为输出目录 # (产物含 full_demo_data.sql 与 storage 文件副本),仅取 SQL 进发行包,临时目录用完即删 $ExportTool = Join-Path $ProjectRoot "tools\export_full_demo_data.py" $ExportedSql = Join-Path $ReleaseDir "database-init.sql" if (Test-Path $ExportTool) { # 导出工具是 Python 版组件:env/storage 按 Python 后端目录与运行时 .env 位置查找 # 运行时 .env 不在 backend 目录:按优先级取最新的现有配置(暂存 > 备份 > backend) $PyBackend = Join-Path $ProjectRoot "backend" $ExportEnv = @( $KeepEnv, (Join-Path $ProjectRoot "dist-package-java-bak\dms-server\.env"), (Join-Path $PyBackend ".env") ) | Where-Object { Test-Path $_ } | Select-Object -First 1 $ExportStorage = Join-Path $PyBackend "dms-storage" if (-not $ExportEnv) { Write-Warning "未找到可用的 .env(暂存 .env.keep / 备份目录 / backend\.env),跳过自动导出" } elseif (-not (Test-Path $ExportStorage)) { Write-Warning "未找到存储目录 $ExportStorage,跳过自动导出" } else { Write-Host " 导出示例数据库 SQL..." -ForegroundColor DarkGray $ExportTmp = Join-Path ([System.IO.Path]::GetTempPath()) "dms-demo-export" if (Test-Path $ExportTmp) { Remove-Item $ExportTmp -Recurse -Force } try { python $ExportTool --env-file $ExportEnv --storage $ExportStorage --output $ExportTmp 2>&1 | Out-Host $ExportedSource = Join-Path $ExportTmp "full_demo_data.sql" if (Test-Path $ExportedSource) { Copy-Item $ExportedSource $ExportedSql -Force Write-Host " 已生成 database-init.sql" -ForegroundColor DarkGray } else { Write-Warning "导出工具未生成 full_demo_data.sql,请手动准备 SQL 文件" } } catch { Write-Warning "导出工具执行失败:$_" } finally { if (Test-Path $ExportTmp) { Remove-Item $ExportTmp -Recurse -Force } } } } else { Write-Warning "未找到 tools\export_full_demo_data.py,跳过自动导出" } # ---------- 4. 恢复 .env 和 dms-storage ---------- # 优先级:打包前暂存(原地数据,Move 回) > 备份目录;发行目录已有的 dms-storage 绝不覆盖 Write-Host "`n[4/4] 恢复 .env 和 dms-storage..." -ForegroundColor Yellow $BackupDir = Join-Path $ProjectRoot "dist-package-java-bak\dms-server" if (-not (Test-Path $BackupDir)) { Write-Host " 备份目录不存在: $BackupDir - 跳过备份恢复" -ForegroundColor DarkGray } else { $BackupEnv = Join-Path $BackupDir ".env" if (Test-Path $BackupEnv) { Copy-Item $BackupEnv $ReleaseDir -Force Write-Host " 已恢复 .env(备份)" -ForegroundColor DarkGray } else { Write-Warning "备份中没有 .env,跳过" } } if (Test-Path $KeepStorage) { Move-Item $KeepStorage (Join-Path $ReleaseDir "dms-storage") -Force Write-Host " 已保留重打包前的 dms-storage" -ForegroundColor DarkGray } elseif ((Test-Path $BackupDir) -and (Test-Path (Join-Path $BackupDir "dms-storage"))) { Copy-Item (Join-Path $BackupDir "dms-storage") (Join-Path $ReleaseDir "dms-storage") -Recurse -Force Write-Host " 已恢复 dms-storage(备份)" -ForegroundColor DarkGray } else { Write-Host " 无 dms-storage 可恢复(暂存与备份均无),跳过" -ForegroundColor DarkGray } # 还原打包前暂存的 .env(优先级高于备份目录,始终保留用户最新配置) if (Test-Path $KeepEnv) { Copy-Item $KeepEnv (Join-Path $ReleaseDir ".env") -Force Remove-Item $KeepEnv -Force Write-Host " 已保留重打包前的 .env" -ForegroundColor DarkGray } # 还原打包前暂存的备份管理配置 if (Test-Path $KeepBackupCfg) { Copy-Item $KeepBackupCfg (Join-Path $ReleaseDir "dms-backup-config.json") -Force Remove-Item $KeepBackupCfg -Force Write-Host " 已保留重打包前的 dms-backup-config.json" -ForegroundColor DarkGray } Write-Host "`n========== 构建完成(Java 版) ==========" -ForegroundColor Green Write-Host "发行包目录: $ReleaseDir" -ForegroundColor Green $sizeMB = [math]::Round((Get-ChildItem $ReleaseDir -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB, 1) Write-Host "包大小: ${sizeMB} MB" -ForegroundColor Green Write-Host "将该目录整体压缩即可分发给最终用户" -ForegroundColor Green