build-java.ps1 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # DMS 一体化构建脚本(Java 版)
  2. # 用法:在项目根目录执行 .\build\build-java.ps1
  3. # 产物:dist-package-java\dms-server\(dms-server.jar 内置前端)
  4. [CmdletBinding()]
  5. param(
  6. [switch]$SkipFrontend
  7. )
  8. $ErrorActionPreference = "Stop"
  9. $ProjectRoot = Split-Path $PSScriptRoot -Parent
  10. $Backend = Join-Path $ProjectRoot "backend-java"
  11. $Frontend = Join-Path $ProjectRoot "frontend"
  12. $Dist = Join-Path $ProjectRoot "dist-package-java"
  13. $Templates = Join-Path $PSScriptRoot "java-templates"
  14. Write-Host "========== DMS 一体化构建(Java 版) ==========" -ForegroundColor Cyan
  15. Write-Host "项目根目录: $ProjectRoot"
  16. # ---------- 1. 前端构建 ----------
  17. if (-not $SkipFrontend) {
  18. Write-Host "`n[1/4] 构建前端..." -ForegroundColor Yellow
  19. Push-Location $Frontend
  20. try {
  21. if (-not (Test-Path "node_modules")) {
  22. Write-Host " npm install..." -ForegroundColor DarkGray
  23. npm install
  24. if ($LASTEXITCODE -ne 0) { throw "npm install 失败" }
  25. }
  26. npm run build
  27. if ($LASTEXITCODE -ne 0) { throw "前端构建失败" }
  28. } finally { Pop-Location }
  29. } else {
  30. Write-Host "`n[1/4] 跳过前端构建 (-SkipFrontend)" -ForegroundColor DarkGray
  31. }
  32. $indexHtml = Join-Path $Frontend "dist\index.html"
  33. if (-not (Test-Path $indexHtml)) {
  34. throw "前端产物不存在: $indexHtml - 请先执行 npm run build"
  35. }
  36. # ---------- 2. Maven 打包(-Pfrontend 把前端 dist 合入 jar 的 static) ----------
  37. Write-Host "`n[2/4] Maven 打包..." -ForegroundColor Yellow
  38. Push-Location $Backend
  39. try {
  40. mvn -q clean package -Pfrontend
  41. if ($LASTEXITCODE -ne 0) { throw "Maven 打包失败" }
  42. } finally { Pop-Location }
  43. $JarFile = Join-Path $Backend "target\dms-server.jar"
  44. if (-not (Test-Path $JarFile)) { throw "未找到打包产物: $JarFile" }
  45. # ---------- 3. 整理发行包 ----------
  46. Write-Host "`n[3/4] 整理发行包..." -ForegroundColor Yellow
  47. $ReleaseDir = Join-Path $Dist "dms-server"
  48. # 暂存现有 .env:删除发行目录前移出,重打包后自动还原,避免用户配置每次丢失
  49. $KeepEnv = Join-Path $Dist ".env.keep"
  50. if (Test-Path (Join-Path $ReleaseDir ".env")) {
  51. Copy-Item (Join-Path $ReleaseDir ".env") $KeepEnv -Force
  52. }
  53. if (Test-Path $ReleaseDir) {
  54. try {
  55. Remove-Item $ReleaseDir -Recurse -Force -ErrorAction Stop
  56. } catch {
  57. # 目录被其他进程占用(如命令行窗口停留在该目录内):保留目录节点,清空内容
  58. Write-Warning "发行目录被其他进程占用,改为清空目录内容后继续"
  59. Get-ChildItem $ReleaseDir -Force | Remove-Item -Recurse -Force
  60. }
  61. }
  62. New-Item $ReleaseDir -ItemType Directory -Force | Out-Null
  63. Copy-Item $JarFile $ReleaseDir -Force
  64. # 配置模板与 Python 版共用同一份 .env.example
  65. Copy-Item (Join-Path $ProjectRoot "backend\.env.example") $ReleaseDir -Force
  66. # 启动/升级脚本与说明
  67. Copy-Item (Join-Path $Templates "run.bat") $ReleaseDir -Force
  68. Copy-Item (Join-Path $Templates "run.sh") $ReleaseDir -Force
  69. Copy-Item (Join-Path $Templates "upgrade-db.bat") $ReleaseDir -Force
  70. Copy-Item (Join-Path $Templates "upgrade-db.sh") $ReleaseDir -Force
  71. Copy-Item (Join-Path $Templates "README.txt") $ReleaseDir -Force
  72. # 示例数据库 SQL(与后端语言无关,复用同一导出工具)
  73. # 工具三参数均必填:--env-file 提供库连接、--storage 提供文件根、--output 为输出目录
  74. # (产物含 full_demo_data.sql 与 storage 文件副本),仅取 SQL 进发行包,临时目录用完即删
  75. $ExportTool = Join-Path $ProjectRoot "tools\export_full_demo_data.py"
  76. $ExportedSql = Join-Path $ReleaseDir "database-init.sql"
  77. if (Test-Path $ExportTool) {
  78. # 导出工具是 Python 版组件:env/storage 按 Python 后端目录与运行时 .env 位置查找
  79. # 运行时 .env 不在 backend 目录:按优先级取最新的现有配置(暂存 > 备份 > backend)
  80. $PyBackend = Join-Path $ProjectRoot "backend"
  81. $ExportEnv = @(
  82. $KeepEnv,
  83. (Join-Path $ProjectRoot "dist-package-java-bak\dms-server\.env"),
  84. (Join-Path $PyBackend ".env")
  85. ) | Where-Object { Test-Path $_ } | Select-Object -First 1
  86. $ExportStorage = Join-Path $PyBackend "dms-storage"
  87. if (-not $ExportEnv) {
  88. Write-Warning "未找到可用的 .env(暂存 .env.keep / 备份目录 / backend\.env),跳过自动导出"
  89. } elseif (-not (Test-Path $ExportStorage)) {
  90. Write-Warning "未找到存储目录 $ExportStorage,跳过自动导出"
  91. } else {
  92. Write-Host " 导出示例数据库 SQL..." -ForegroundColor DarkGray
  93. $ExportTmp = Join-Path ([System.IO.Path]::GetTempPath()) "dms-demo-export"
  94. if (Test-Path $ExportTmp) { Remove-Item $ExportTmp -Recurse -Force }
  95. try {
  96. python $ExportTool --env-file $ExportEnv --storage $ExportStorage --output $ExportTmp 2>&1 | Out-Host
  97. $ExportedSource = Join-Path $ExportTmp "full_demo_data.sql"
  98. if (Test-Path $ExportedSource) {
  99. Copy-Item $ExportedSource $ExportedSql -Force
  100. Write-Host " 已生成 database-init.sql" -ForegroundColor DarkGray
  101. } else {
  102. Write-Warning "导出工具未生成 full_demo_data.sql,请手动准备 SQL 文件"
  103. }
  104. } catch {
  105. Write-Warning "导出工具执行失败:$_"
  106. } finally {
  107. if (Test-Path $ExportTmp) { Remove-Item $ExportTmp -Recurse -Force }
  108. }
  109. }
  110. } else {
  111. Write-Warning "未找到 tools\export_full_demo_data.py,跳过自动导出"
  112. }
  113. # ---------- 4. 从备份恢复 .env 和 dms-storage ----------
  114. Write-Host "`n[4/4] 从备份恢复 .env 和 dms-storage..." -ForegroundColor Yellow
  115. $BackupDir = Join-Path $ProjectRoot "dist-package-java-bak\dms-server"
  116. if (-not (Test-Path $BackupDir)) {
  117. Write-Host " 备份目录不存在: $BackupDir - 跳过恢复" -ForegroundColor DarkGray
  118. } else {
  119. $BackupEnv = Join-Path $BackupDir ".env"
  120. if (Test-Path $BackupEnv) {
  121. Copy-Item $BackupEnv $ReleaseDir -Force
  122. Write-Host " 已恢复 .env" -ForegroundColor DarkGray
  123. } else {
  124. Write-Warning "备份中没有 .env,跳过"
  125. }
  126. $BackupStorage = Join-Path $BackupDir "dms-storage"
  127. if (Test-Path $BackupStorage) {
  128. $TargetStorage = Join-Path $ReleaseDir "dms-storage"
  129. if (Test-Path $TargetStorage) {
  130. Remove-Item $TargetStorage -Recurse -Force
  131. }
  132. Copy-Item $BackupStorage $TargetStorage -Recurse -Force
  133. Write-Host " 已恢复 dms-storage" -ForegroundColor DarkGray
  134. } else {
  135. Write-Warning "备份中没有 dms-storage,跳过"
  136. }
  137. }
  138. # 还原打包前暂存的 .env(优先级高于备份目录,始终保留用户最新配置)
  139. if (Test-Path $KeepEnv) {
  140. Copy-Item $KeepEnv (Join-Path $ReleaseDir ".env") -Force
  141. Remove-Item $KeepEnv -Force
  142. Write-Host " 已保留重打包前的 .env" -ForegroundColor DarkGray
  143. }
  144. Write-Host "`n========== 构建完成(Java 版) ==========" -ForegroundColor Green
  145. Write-Host "发行包目录: $ReleaseDir" -ForegroundColor Green
  146. $sizeMB = [math]::Round((Get-ChildItem $ReleaseDir -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB, 1)
  147. Write-Host "包大小: ${sizeMB} MB" -ForegroundColor Green
  148. Write-Host "将该目录整体压缩即可分发给最终用户" -ForegroundColor Green