1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
| #!/usr/bin/env pwsh # ============================================================ # RuoYi-Vue-Plus 包名重命名脚本 (Windows PowerShell) # 用法: .\rename-package.ps1 -ProjectDir "F:\RuoYi-Vue-Plus" -NewGroupId "com.cicca" -NewArtifactPrefix "cicca" -NewAppName "cicca-attendance" # ============================================================
param( [Parameter(Mandatory=$true)] [string]$ProjectDir,
[Parameter(Mandatory=$true)] [string]$NewGroupId, # 例如: com.cicca
[Parameter(Mandatory=$true)] [string]$NewArtifactPrefix, # 例如: cicca (模块前缀, 替换 ruoyi)
[Parameter(Mandatory=$true)] [string]$NewAppName, # 例如: cicca-attendance (根 artifactId)
[string]$NewAppTitle = "", # 可选: 系统标题, 如 "CICCA考勤系统"
[switch]$DryRun # 仅预览, 不实际修改 )
$ErrorActionPreference = "Stop"
# --- 原始值 --- $OldGroupId = "org.dromara" $OldArtifactPrefix = "ruoyi" $OldPackagePath = "org/dromara" $NewPackagePath = $NewGroupId -replace '\.', '/'
# 第三方包前缀 (不替换) $ThirdPartyPrefixes = @( "org.dromara.warm", "org.dromara.sms4j", "org.dromara.x.file.storage" )
Write-Host "============================================" -ForegroundColor Cyan Write-Host " RuoYi-Vue-Plus 包名重命名工具" -ForegroundColor Cyan Write-Host "============================================" -ForegroundColor Cyan Write-Host "项目目录: $ProjectDir" Write-Host "旧 GroupId: $OldGroupId" Write-Host "新 GroupId: $NewGroupId" Write-Host "旧模块前缀: $OldArtifactPrefix" Write-Host "新模块前缀: $NewArtifactPrefix" Write-Host "新应用名: $NewAppName" Write-Host "DryRun: $DryRun" Write-Host ""
if (-not (Test-Path $ProjectDir)) { Write-Error "项目目录不存在: $ProjectDir" exit 1 }
# ============================================================ # 阶段 0: 去除所有文件的 UTF-8 BOM # ============================================================ Write-Host "[阶段 0] 去除 UTF-8 BOM ..." -ForegroundColor Yellow $bomCount = 0 Get-ChildItem -Path $ProjectDir -Recurse -File | Where-Object { $_.FullName -notlike "*\.git\*" -and $_.FullName -notlike "*\target\*" -and $_.FullName -notlike "*\node_modules\*" } | ForEach-Object { $bytes = [System.IO.File]::ReadAllBytes($_.FullName) if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) { if (-not $DryRun) { $newBytes = $bytes[3..($bytes.Length - 1)] [System.IO.File]::WriteAllBytes($_.FullName, $newBytes) } $bomCount++ } } Write-Host " 去除 BOM: $bomCount 个文件" -ForegroundColor Green
# ============================================================ # 阶段 1: 重命名目录 (ruoyi-xxx -> newprefix-xxx) # ============================================================ Write-Host "[阶段 1] 重命名模块目录 ..." -ForegroundColor Yellow $dirRenameCount = 0
# 从最深层开始重命名, 避免父目录先改导致子目录路径失效 $dirsToRename = Get-ChildItem -Path $ProjectDir -Recurse -Directory | Where-Object { $_.FullName -notlike "*\.git\*" -and $_.FullName -notlike "*\target\*" -and $_.Name -like "$OldArtifactPrefix-*" } | Sort-Object { $_.FullName.Length } -Descending
foreach ($dir in $dirsToRename) { $newName = $dir.Name -replace "^$OldArtifactPrefix-", "$NewArtifactPrefix-" $newPath = Join-Path $dir.Parent.FullName $newName if ($dir.FullName -ne $newPath) { Write-Host " 目录: $($dir.Name) -> $newName" if (-not $DryRun) { Rename-Item -Path $dir.FullName -NewName $newName } $dirRenameCount++ } } Write-Host " 重命名目录: $dirRenameCount 个" -ForegroundColor Green
# ============================================================ # 阶段 2: 重命名 Java 包目录 (org/dromara -> new/package/path) # ============================================================ Write-Host "[阶段 2] 重命名 Java 包目录 ..." -ForegroundColor Yellow $pkgDirCount = 0
# 查找所有 src 下的 org/dromara 目录 $javaPkgDirs = Get-ChildItem -Path $ProjectDir -Recurse -Directory -Filter "dromara" | Where-Object { $_.FullName -notlike "*\.git\*" -and $_.FullName -notlike "*\target\*" -and $_.Parent.Name -eq "org" } | Sort-Object { $_.FullName.Length } -Descending
foreach ($dir in $javaPkgDirs) { $orgDir = $dir.Parent # org 目录 $srcBase = $orgDir.Parent # org 的父目录
# 创建新包目录 $newPkgParts = $NewGroupId -split '\.' $newPkgDir = $srcBase.FullName foreach ($part in $newPkgParts) { $newPkgDir = Join-Path $newPkgDir $part }
Write-Host " 包目录: $($dir.FullName) -> $newPkgDir" if (-not $DryRun) { if (-not (Test-Path $newPkgDir)) { New-Item -ItemType Directory -Path $newPkgDir -Force | Out-Null } # 移动所有子内容 Get-ChildItem -Path $dir.FullName | ForEach-Object { Move-Item -Path $_.FullName -Destination $newPkgDir -Force } # 清理空的旧目录 if ((Get-ChildItem -Path $dir.FullName -Recurse -File).Count -eq 0) { Remove-Item -Path $orgDir.FullName -Recurse -Force -ErrorAction SilentlyContinue } } $pkgDirCount++ } Write-Host " 重命名包目录: $pkgDirCount 个" -ForegroundColor Green
# ============================================================ # 阶段 3: 替换文件内容 # ============================================================ Write-Host "[阶段 3] 替换文件内容 ..." -ForegroundColor Yellow $fileReplaceCount = 0
# 需要处理的文件扩展名 $extensions = @("*.java", "*.xml", "*.yml", "*.yaml", "*.properties", "*.imports", "*.factories", "*.txt", "*.md", "*.json", "*.gradle", "*.kts", "*.bat", "*.sh", "*.ps1", "*.vue", "*.ts", "*.js", "*.scss", "*.css", "*.html", "*.env*", "Dockerfile*")
# 构建替换规则 (顺序很重要) $replacements = @( # 1. 保护第三方包: 先用占位符替换 # (在正则替换中处理)
# 2. Java 包名: org.dromara -> new.group.id (排除第三方) # 3. 模块名: ruoyi-xxx -> newprefix-xxx # 4. artifactId: ruoyi-vue-plus -> new-app-name # 5. 目录路径: org/dromara -> new/group/path )
Get-ChildItem -Path $ProjectDir -Recurse -File -Include $extensions | Where-Object { $_.FullName -notlike "*\.git\*" -and $_.FullName -notlike "*\target\*" -and $_.FullName -notlike "*\node_modules\*" } | ForEach-Object { $filePath = $_.FullName $content = [System.IO.File]::ReadAllText($filePath, [System.Text.Encoding]::UTF8) $original = $content
# --- 保护第三方包: 用占位符临时替换 --- $placeholders = @{} $i = 0 foreach ($prefix in $ThirdPartyPrefixes) { $placeholder = "___THIRDPARTY_${i}___" $placeholders[$placeholder] = $prefix $content = $content -replace [regex]::Escape($prefix), $placeholder $i++ }
# --- 执行替换 --- # Java 包名 $content = $content -replace [regex]::Escape($OldGroupId), $NewGroupId
# 包路径 (目录分隔符) $content = $content -replace [regex]::Escape($OldPackagePath), $NewPackagePath
# 模块前缀 (ruoyi-xxx -> newprefix-xxx) $content = $content -replace "\b$OldArtifactPrefix-", "$NewArtifactPrefix-"
# 根 artifactId $content = $content -replace "$NewArtifactPrefix-vue-plus", $NewAppName
# DromaraApplication -> 新应用类名 $appClassName = ($NewArtifactPrefix.Substring(0,1).ToUpper() + $NewArtifactPrefix.Substring(1)) + "Application" $servletClassName = ($NewArtifactPrefix.Substring(0,1).ToUpper() + $NewArtifactPrefix.Substring(1)) + "ServletInitializer" $content = $content -replace "DromaraApplication", $appClassName $content = $content -replace "DromaraServletInitializer", $servletClassName
# --- 恢复第三方包 --- foreach ($kv in $placeholders.GetEnumerator()) { $content = $content -replace [regex]::Escape($kv.Key), $kv.Value }
# --- 写回文件 --- if ($content -ne $original) { if (-not $DryRun) { [System.IO.File]::WriteAllText($filePath, $content, (New-Object System.Text.UTF8Encoding $false)) } $fileReplaceCount++ } } Write-Host " 替换文件内容: $fileReplaceCount 个文件" -ForegroundColor Green
# ============================================================ # 阶段 4: 重命名 DromaraApplication.java 等文件 # ============================================================ Write-Host "[阶段 4] 重命名启动类文件 ..." -ForegroundColor Yellow $appClassName = ($NewArtifactPrefix.Substring(0,1).ToUpper() + $NewArtifactPrefix.Substring(1))
$filesToRename = @( @{ Old = "DromaraApplication.java"; New = "${appClassName}Application.java" }, @{ Old = "DromaraServletInitializer.java"; New = "${appClassName}ServletInitializer.java" } )
foreach ($fr in $filesToRename) { $found = Get-ChildItem -Path $ProjectDir -Recurse -File -Filter $fr.Old | Where-Object { $_.FullName -notlike "*\.git\*" -and $_.FullName -notlike "*\target\*" } foreach ($f in $found) { $newPath = Join-Path $f.DirectoryName $fr.New Write-Host " 文件: $($f.Name) -> $($fr.New)" if (-not $DryRun) { Rename-Item -Path $f.FullName -NewName $fr.New } } }
# ============================================================ # 阶段 5: 重命名 .run 配置文件 # ============================================================ Write-Host "[阶段 5] 重命名 .run 配置文件 ..." -ForegroundColor Yellow $runDir = Join-Path $ProjectDir ".run" if (Test-Path $runDir) { Get-ChildItem -Path $runDir -File -Filter "$OldArtifactPrefix-*" | ForEach-Object { $newName = $_.Name -replace "^$OldArtifactPrefix-", "$NewArtifactPrefix-" Write-Host " .run: $($_.Name) -> $newName" if (-not $DryRun) { Rename-Item -Path $_.FullName -NewName $newName } } }
# ============================================================ # 阶段 6: 清理 target 目录 # ============================================================ Write-Host "[阶段 6] 清理 target 目录 ..." -ForegroundColor Yellow $targetCount = 0 Get-ChildItem -Path $ProjectDir -Recurse -Directory -Filter "target" | Where-Object { $_.FullName -notlike "*\.git\*" } | ForEach-Object { if (-not $DryRun) { Remove-Item -Recurse -Force $_.FullName } $targetCount++ } Write-Host " 清理 target: $targetCount 个目录" -ForegroundColor Green
# ============================================================ # 阶段 7: 清理 .idea 缓存 (可选) # ============================================================ Write-Host "[阶段 7] 清理 IDEA 缓存 ..." -ForegroundColor Yellow $ideaDir = Join-Path $ProjectDir ".idea" if (Test-Path $ideaDir) { $workspaceXml = Join-Path $ideaDir "workspace.xml" if (Test-Path $workspaceXml) { if (-not $DryRun) { Remove-Item -Force $workspaceXml } Write-Host " 删除 workspace.xml" } }
# ============================================================ Write-Host "" Write-Host "============================================" -ForegroundColor Cyan Write-Host " 完成!" -ForegroundColor Cyan Write-Host "============================================" -ForegroundColor Cyan Write-Host "" Write-Host "后续步骤:" -ForegroundColor Yellow Write-Host " 1. 用 IDEA 打开项目, 等待索引完成" Write-Host " 2. 执行 mvn clean compile 验证编译" Write-Host " 3. 检查启动是否正常" Write-Host ""
|