-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaven.VersionRelease.Win.XFlowEngine-Client.ps1
More file actions
83 lines (71 loc) · 3.2 KB
/
Copy pathMaven.VersionRelease.Win.XFlowEngine-Client.ps1
File metadata and controls
83 lines (71 loc) · 3.2 KB
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
$pomPath = "pom.xml"
$targetDir = ".." # 要替换的目标目录
$targetArtifactId = "hy.xflow.engine-client" # 要匹配的artifactId
$currentDir = $PSScriptRoot # 当前目录
$currentDirPrefix = "$currentDir\" # 拼接路径前缀(结尾加\,确保匹配“当前目录/子目录”的格式)
$newVersion = "" # 新版本号
# 检查文件是否存在
if (Test-Path $pomPath) {
# 加载XML文件并解析版本号
$pomXml = [xml](Get-Content $pomPath -Encoding UTF8)
$newVersion = $pomXml.project.version
# 显示版本号
if ($newVersion) {
Write-Host "$targetArtifactId version: $newVersion" -ForegroundColor Green
} else {
# 兼容父模块依赖的情况(version可能在parent节点)
$parentVersion = $pomXml.project.parent.version
if ($parentVersion) {
Write-Host "pom.xml parentVersion: $parentVersion" -ForegroundColor Green
return
} else {
Write-Host "not find pom version node" -ForegroundColor Red
return
}
}
} else {
Write-Host "not find pom: $pomPath" -ForegroundColor Red
return
}
# ========== 批量更新目标pom.xml文件 ==========
# 递归查找目标目录下所有pom.xml(排除源pom.xml,避免自更新)
$targetPomFiles = Get-ChildItem -Path $targetDir -Filter "pom.xml" -Recurse |
Where-Object {
# 条件1:排除源pom.xml文件
$_.FullName -ne $sourcePomPath -and
# 条件2:排除当前目录下的所有pom.xml
$_.FullName -notlike "$currentDirPrefix*"
}
if ($targetPomFiles.Count -eq 0) {
Write-Host "not any pom.xml" -ForegroundColor Yellow
return
}
# 遍历每个目标pom.xml
foreach ($pomFile in $targetPomFiles) {
Write-Host "`nFinding: $($pomFile.FullName)" -ForegroundColor Cyan
try {
# 读取文件内容(Raw模式保留格式,避免XML解析破坏缩进)
$content = Get-Content -Path $pomFile.FullName -Encoding UTF8 -Raw
# 正则匹配:<artifactId>...</artifactId> 后紧跟的 <version>...</version>
# 兼容换行/空格/缩进的多种格式
$pattern = '(?s)(?<=<artifactId>' + $targetArtifactId + '</artifactId>\s*<version>)([^<]+)(?=</version>)'
if ($content -match $pattern) {
$oldVersion = $matches[1] # 提取旧版本号
if ($oldVersion -ne $newVersion) {
# 直接替换匹配到的旧版本号为新版本号(无任何$分组引用)
$content = $content -replace $pattern, $newVersion
$content = $content.TrimEnd("`r", "`n", " ") # 去除末尾的回车、换行、空格
Set-Content $pomFile.FullName -Value $content -Encoding UTF8
Write-Host "Updated: $oldVersion -> $newVersion" -ForegroundColor Green
} else {
Write-Host "Skipped: No change (version already $oldVersion)" -ForegroundColor Gray
}
}
else {
Write-Host "Skipped: Not find" -ForegroundColor Gray
}
}
catch {
Write-Host "Error : $($_.Exception.Message)" -ForegroundColor Red
}
}