|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# 批量修复Groovy编译配置脚本 |
| 4 | +# 用于将groovy-eclipse-compiler替换为标准Java编译器或GMavenPlus |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | +PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 10 | + |
| 11 | +cd "$PROJECT_ROOT" |
| 12 | + |
| 13 | +echo "==========================================" |
| 14 | +echo "批量修复Groovy编译配置" |
| 15 | +echo "==========================================" |
| 16 | +echo "" |
| 17 | + |
| 18 | +# 查找所有包含groovy-eclipse-compiler的pom.xml文件 |
| 19 | +FILES=$(grep -r "groovy-eclipse-compiler" --include="pom.xml" . | cut -d: -f1 | sort -u) |
| 20 | + |
| 21 | +TOTAL=$(echo "$FILES" | wc -l | tr -d ' ') |
| 22 | +CURRENT=0 |
| 23 | +FIXED=0 |
| 24 | +SKIPPED=0 |
| 25 | + |
| 26 | +# 标准Java编译器配置(用于没有Groovy源文件的模块) |
| 27 | +STANDARD_JAVA_CONFIG=' <configuration> |
| 28 | + <source>${project.java.version}</source> |
| 29 | + <target>${project.java.version}</target> |
| 30 | + <debug>true</debug> |
| 31 | + <parameters>true</parameters> |
| 32 | + </configuration>' |
| 33 | + |
| 34 | +# GMavenPlus配置(用于有Groovy源文件的模块) |
| 35 | +GMAVENPLUS_CONFIG=' <!-- 标准Java编译器 --> |
| 36 | + <plugin> |
| 37 | + <groupId>org.apache.maven.plugins</groupId> |
| 38 | + <artifactId>maven-compiler-plugin</artifactId> |
| 39 | + <version>${project.compiler.version}</version> |
| 40 | + <configuration> |
| 41 | + <source>${project.java.version}</source> |
| 42 | + <target>${project.java.version}</target> |
| 43 | + <debug>true</debug> |
| 44 | + <parameters>true</parameters> |
| 45 | + </configuration> |
| 46 | + </plugin> |
| 47 | + <!-- GMavenPlus 用于编译Groovy代码 --> |
| 48 | + <plugin> |
| 49 | + <groupId>org.codehaus.gmavenplus</groupId> |
| 50 | + <artifactId>gmavenplus-plugin</artifactId> |
| 51 | + <version>4.2.1</version> |
| 52 | + <executions> |
| 53 | + <execution> |
| 54 | + <goals> |
| 55 | + <goal>generateStubs</goal> |
| 56 | + <goal>compile</goal> |
| 57 | + <goal>generateTestStubs</goal> |
| 58 | + <goal>compileTests</goal> |
| 59 | + </goals> |
| 60 | + </execution> |
| 61 | + </executions> |
| 62 | + <configuration> |
| 63 | + <groovyVersion>${groovy.version}</groovyVersion> |
| 64 | + <targetBytecode>${project.java.version}</targetBytecode> |
| 65 | + </configuration> |
| 66 | + </plugin>' |
| 67 | + |
| 68 | +for FILE in $FILES; do |
| 69 | + CURRENT=$((CURRENT + 1)) |
| 70 | + echo "[$CURRENT/$TOTAL] 处理: $FILE" |
| 71 | + |
| 72 | + # 检查模块目录是否有Groovy源文件 |
| 73 | + MODULE_DIR=$(dirname "$FILE") |
| 74 | + HAS_GROOVY=$(find "$MODULE_DIR" -name "*.groovy" -type f 2>/dev/null | head -1) |
| 75 | + |
| 76 | + if [ -n "$HAS_GROOVY" ]; then |
| 77 | + echo " ⚠️ 发现Groovy源文件,需要手动配置GMavenPlus" |
| 78 | + echo " 📝 请手动编辑此文件,参考test/pom.xml的配置" |
| 79 | + SKIPPED=$((SKIPPED + 1)) |
| 80 | + continue |
| 81 | + fi |
| 82 | + |
| 83 | + # 读取文件内容 |
| 84 | + if [ ! -f "$FILE" ]; then |
| 85 | + echo " ❌ 文件不存在,跳过" |
| 86 | + SKIPPED=$((SKIPPED + 1)) |
| 87 | + continue |
| 88 | + fi |
| 89 | + |
| 90 | + # 创建备份 |
| 91 | + BACKUP_FILE="${FILE}.backup.$(date +%Y%m%d_%H%M%S)" |
| 92 | + cp "$FILE" "$BACKUP_FILE" |
| 93 | + echo " 💾 备份文件: $BACKUP_FILE" |
| 94 | + |
| 95 | + # 使用Python进行精确替换(因为sed处理多行和缩进比较复杂) |
| 96 | + python3 <<PYTHON_SCRIPT |
| 97 | +import re |
| 98 | +import sys |
| 99 | +
|
| 100 | +file_path = "$FILE" |
| 101 | +
|
| 102 | +with open(file_path, 'r', encoding='utf-8') as f: |
| 103 | + content = f.read() |
| 104 | +
|
| 105 | +# 匹配groovy-eclipse-compiler配置块 |
| 106 | +# 匹配从<plugin>开始到</plugin>结束,包含groovy-eclipse-compiler的整个块 |
| 107 | +pattern = r'''\s*<plugin>\s* |
| 108 | +\s*<groupId>org\.apache\.maven\.plugins</groupId>\s* |
| 109 | +\s*<artifactId>maven-compiler-plugin</artifactId>\s* |
| 110 | +\s*<version>.*?</version>\s* |
| 111 | +\s*<configuration>\s* |
| 112 | +\s*<compilerId>groovy-eclipse-compiler</compilerId>\s* |
| 113 | +\s*<source>\$\{project\.java\.version\}</source>\s* |
| 114 | +\s*<target>\$\{project\.java\.version\}</target>\s* |
| 115 | +\s*<debuglevel>lines,vars,source</debuglevel>\s* |
| 116 | +\s*<debug>true</debug>\s* |
| 117 | +\s*</configuration>\s* |
| 118 | +\s*<dependencies>\s* |
| 119 | +\s*<dependency>\s* |
| 120 | +\s*<groupId>org\.codehaus\.groovy</groupId>\s* |
| 121 | +\s*<artifactId>groovy-eclipse-compiler</artifactId>\s* |
| 122 | +\s*<version>.*?</version>\s* |
| 123 | +\s*</dependency>\s* |
| 124 | +\s*<dependency>\s* |
| 125 | +\s*<groupId>org\.codehaus\.groovy</groupId>\s* |
| 126 | +\s*<artifactId>groovy-eclipse-batch</artifactId>\s* |
| 127 | +\s*<version>.*?</version>\s* |
| 128 | +\s*</dependency>\s* |
| 129 | +\s*</dependencies>\s* |
| 130 | +\s*</plugin>''' |
| 131 | +
|
| 132 | +# 更灵活的匹配模式(处理不同的缩进和换行) |
| 133 | +pattern2 = r'''(\s*)<plugin>\s* |
| 134 | +\1<groupId>org\.apache\.maven\.plugins</groupId>\s* |
| 135 | +\1<artifactId>maven-compiler-plugin</artifactId>\s* |
| 136 | +\1<version>([^<]+)</version>\s* |
| 137 | +\1<configuration>\s* |
| 138 | +\1\s*<compilerId>groovy-eclipse-compiler</compilerId>\s* |
| 139 | +\1\s*<source>\$\{project\.java\.version\}</source>\s* |
| 140 | +\1\s*<target>\$\{project\.java\.version\}</target>\s* |
| 141 | +\1\s*<debuglevel>lines,vars,source</debuglevel>\s* |
| 142 | +\1\s*<debug>true</debug>\s* |
| 143 | +\1</configuration>\s* |
| 144 | +\1<dependencies>\s* |
| 145 | +\1\s*<dependency>\s* |
| 146 | +\1\s*<groupId>org\.codehaus\.groovy</groupId>\s* |
| 147 | +\1\s*<artifactId>groovy-eclipse-compiler</artifactId>\s* |
| 148 | +\1\s*<version>[^<]+</version>\s* |
| 149 | +\1\s*</dependency>\s* |
| 150 | +\1\s*<dependency>\s* |
| 151 | +\1\s*<groupId>org\.codehaus\.groovy</groupId>\s* |
| 152 | +\1\s*<artifactId>groovy-eclipse-batch</artifactId>\s* |
| 153 | +\1\s*<version>[^<]+</version>\s* |
| 154 | +\1\s*</dependency>\s* |
| 155 | +\1</dependencies>\s* |
| 156 | +\1</plugin>''' |
| 157 | +
|
| 158 | +# 尝试匹配并替换 |
| 159 | +def replace_plugin(match): |
| 160 | + indent = match.group(1) |
| 161 | + version = match.group(2).strip() |
| 162 | + replacement = f'''{indent}<plugin> |
| 163 | +{indent} <groupId>org.apache.maven.plugins</groupId> |
| 164 | +{indent} <artifactId>maven-compiler-plugin</artifactId> |
| 165 | +{indent} <version>{version}</version> |
| 166 | +{indent} <configuration> |
| 167 | +{indent} <source>${{project.java.version}}</source> |
| 168 | +{indent} <target>${{project.java.version}}</target> |
| 169 | +{indent} <debug>true</debug> |
| 170 | +{indent} <parameters>true</parameters> |
| 171 | +{indent} </configuration> |
| 172 | +{indent}</plugin>''' |
| 173 | + return replacement |
| 174 | +
|
| 175 | +# 使用更简单的方法:逐行处理 |
| 176 | +lines = content.split('\n') |
| 177 | +new_lines = [] |
| 178 | +i = 0 |
| 179 | +in_plugin = False |
| 180 | +in_config = False |
| 181 | +in_dependencies = False |
| 182 | +plugin_start = -1 |
| 183 | +indent = "" |
| 184 | +
|
| 185 | +while i < len(lines): |
| 186 | + line = lines[i] |
| 187 | + |
| 188 | + # 检测plugin开始 |
| 189 | + if '<plugin>' in line and 'maven-compiler-plugin' in '\n'.join(lines[max(0, i-3):i+3]): |
| 190 | + # 向前查找groupId和artifactId |
| 191 | + if i > 0 and 'maven-compiler-plugin' in lines[i-1]: |
| 192 | + plugin_start = len(new_lines) |
| 193 | + indent = line[:len(line) - len(line.lstrip())] |
| 194 | + in_plugin = True |
| 195 | + new_lines.append(line) |
| 196 | + i += 1 |
| 197 | + continue |
| 198 | + |
| 199 | + # 如果在plugin块中,检测groovy-eclipse-compiler |
| 200 | + if in_plugin: |
| 201 | + if '<compilerId>groovy-eclipse-compiler</compilerId>' in line: |
| 202 | + # 找到了,需要替换整个plugin块 |
| 203 | + # 回退到plugin开始位置 |
| 204 | + new_lines = new_lines[:plugin_start] |
| 205 | + # 添加新的标准配置 |
| 206 | + version_line = None |
| 207 | + for j in range(plugin_start, i): |
| 208 | + if '<version>' in lines[j] and '</version>' in lines[j]: |
| 209 | + version_line = lines[j] |
| 210 | + break |
| 211 | + |
| 212 | + version = '${project.compiler.version}' |
| 213 | + if version_line: |
| 214 | + version_match = re.search(r'<version>([^<]+)</version>', version_line) |
| 215 | + if version_match: |
| 216 | + version = version_match.group(1).strip() |
| 217 | + |
| 218 | + new_lines.append(f'{indent}<plugin>') |
| 219 | + new_lines.append(f'{indent} <groupId>org.apache.maven.plugins</groupId>') |
| 220 | + new_lines.append(f'{indent} <artifactId>maven-compiler-plugin</artifactId>') |
| 221 | + new_lines.append(f'{indent} <version>{version}</version>') |
| 222 | + new_lines.append(f'{indent} <configuration>') |
| 223 | + new_lines.append(f'{indent} <source>${{project.java.version}}</source>') |
| 224 | + new_lines.append(f'{indent} <target>${{project.java.version}}</target>') |
| 225 | + new_lines.append(f'{indent} <debug>true</debug>') |
| 226 | + new_lines.append(f'{indent} <parameters>true</parameters>') |
| 227 | + new_lines.append(f'{indent} </configuration>') |
| 228 | + new_lines.append(f'{indent}</plugin>') |
| 229 | + |
| 230 | + # 跳过到</plugin> |
| 231 | + while i < len(lines) and '</plugin>' not in lines[i]: |
| 232 | + i += 1 |
| 233 | + if i < len(lines): |
| 234 | + i += 1 # 跳过</plugin> |
| 235 | + in_plugin = False |
| 236 | + continue |
| 237 | + elif '</plugin>' in line: |
| 238 | + in_plugin = False |
| 239 | + new_lines.append(line) |
| 240 | + i += 1 |
| 241 | + continue |
| 242 | + |
| 243 | + new_lines.append(line) |
| 244 | + i += 1 |
| 245 | +
|
| 246 | +new_content = '\n'.join(new_lines) |
| 247 | +
|
| 248 | +# 如果内容有变化,写入文件 |
| 249 | +if new_content != content: |
| 250 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 251 | + f.write(new_content) |
| 252 | + print(" ✅ 修复完成") |
| 253 | + sys.exit(0) |
| 254 | +else: |
| 255 | + print(" ⚠️ 未找到需要替换的内容(可能已修复或格式不同)") |
| 256 | + sys.exit(1) |
| 257 | +
|
| 258 | +PYTHON_SCRIPT |
| 259 | + |
| 260 | + if [ $? -eq 0 ]; then |
| 261 | + FIXED=$((FIXED + 1)) |
| 262 | + echo " ✅ 修复成功" |
| 263 | + else |
| 264 | + echo " ⚠️ 修复失败或无需修复,请手动检查" |
| 265 | + SKIPPED=$((SKIPPED + 1)) |
| 266 | + fi |
| 267 | + echo "" |
| 268 | +done |
| 269 | + |
| 270 | +echo "==========================================" |
| 271 | +echo "修复完成!" |
| 272 | +echo "==========================================" |
| 273 | +echo "总计: $TOTAL 个文件" |
| 274 | +echo "修复: $FIXED 个文件" |
| 275 | +echo "跳过: $SKIPPED 个文件" |
| 276 | +echo "" |
| 277 | +echo "备份文件已保存,如需恢复请使用:" |
| 278 | +echo " cp <file>.backup.* <file>" |
| 279 | +echo "" |
| 280 | +echo "建议运行验证:" |
| 281 | +echo " mvn clean compile -DskipTests" |
| 282 | +echo " 或" |
| 283 | +echo " bash ./build/verify-java17.sh" |
0 commit comments