升级Spring Boot3+与Java17后Spotless removeUnusedImports报错
问题:Spring Boot 3+(Java 17)下Spotless removeUnusedImports功能报错
将Spring Boot应用升级到3.1.3版本并切换到Java 17后,使用Spotless插件的removeUnusedImports功能时出现错误,该问题与已报告的同类问题情况一致。
已尝试的解决方案
- 按照Google Java Format的IntelliJ配置指南,向IntelliJ的VM选项添加以下参数:
-Xmx4096m --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
- 在Git终端中设置
MAVEN_OPTS参数,尝试将上述配置添加到Maven中(原命令存在截断):
export MAVEN_OPTS="-Xmx4096m --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.u $ git config --global credential.helper "cache --timeout=3600"
当前使用的版本信息
<spotless-plugin.version>2.34.0</spotless-plugin.version> <spotless-eclipse.version>4.18.0</spotless-eclipse.version> <spring-boot.version>3.1.3</spring-boot.version>
Spotless Maven插件配置
<plugin> <groupId>com.diffplug.spotless</groupId> <artifactId>spotless-maven-plugin</artifactId> <version>${spotless-plugin.version}</version> <configuration> <formats> <format> <includes> <include>*.java</include> <include>*.md</include> </includes> <trimTrailingWhitespace/> <endWithNewline/> <indent> <tabs>true</tabs> <spacesPerTab>4</spacesPerTab> </indent> </format> </formats> <java> <removeUnusedImports/> <eclipse> <version>${spotless-eclipse.version}</version> <file>${project.basedir}/eclipse-formatter.xml</file> </eclipse> </java> </configuration> </plugin>
具体报错信息
[INFO] Spotless.Format is keeping 1 files clean - 0 were changed to be clean, 1 were already clean, 0 were skipped because caching determined they were already clean [ERROR] Step 'removeUnusedImports' found problem in 'src\test\java\com\myproject\amlar\client\DBClient.java': null java.lang.reflect.InvocationTargetException at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:566) at com.diffplug.spotless.java.GoogleJavaFormatStep$State.lambda$constructRemoveUnusedFunction$4 (GoogleJavaFormatStep.java:211) Caused by: com.google.googlejavaformat.java.FormatterException: 37:25: error: unclosed string literal at com.google.googlejavaformat.java.FormatterException.fromJavacDiagnostics (FormatterException.java:51) at com.google.googlejavaformat.java.RemoveUnusedImports.parse (RemoveUnusedImports.java:262) at com.google.googlejavaformat.java.RemoveUnusedImports.removeUnusedImports (RemoveUnusedImports.java:219) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:566) at com.diffplug.spotless.java.GoogleJavaFormatStep$State.lambda$constructRemoveUnusedFunction$4 (GoogleJavaFormatStep.java:211) at com.diffplug.spotless.java.GoogleJavaFormatStep$State.lambda$createRemoveUnusedImportsOnly$2 (GoogleJavaFormatStep.java:188) at com.diffplug.spotless.FormatterFunc.apply (FormatterFunc.java:32) at com.diffplug.spotless.FormatterStepImpl$Standard.format (FormatterStepImpl.java:82) at com.diffplug.spotless.FormatterStep$Strict.format (FormatterStep.java:88) at com.diffplug.spotless.Formatter.compute (Formatter.java:246) at com.diffplug.spotless.PaddedCell.calculateDirtyState (PaddedCell.java:203) at com.diffplug.spotless.PaddedCell.calculateDirtyState (PaddedCell.java:190) at com.diffplug.spotless.maven.SpotlessApplyMojo.process (SpotlessApplyMojo.java:48) at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute (AbstractSpotlessMojo.java:225) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156) [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.439 s [INFO] Finished at: 2023-09-18T12:43:17+01:00
解决方案
修复目标文件语法错误
报错明确指出DBClient.java第37行第25位存在未闭合的字符串字面量,这是Spotless解析失败的直接原因。先手动修复该文件的语法问题,再重新运行插件。升级Spotless插件版本
当前使用的2.34.0版本对Java 17兼容性支持不足,建议升级到最新稳定版(如3.x系列),新版本已修复大量Java 17相关的解析兼容问题。修正MAVEN_OPTS配置
原命令存在截断,完整配置应为:export MAVEN_OPTS="-Xmx4096m --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"配置完成后重启终端,再执行Maven命令。
切换到Eclipse的移除未使用导入功能
将removeUnusedImports配置移到Eclipse节点下,使用Eclipse的解析逻辑替代Google Java Format的实现,避免兼容问题:<java> <eclipse> <version>${spotless-eclipse.version}</version> <file>${project.basedir}/eclipse-formatter.xml</file> <removeUnusedImports/> </eclipse> </java>
内容的提问来源于stack exchange,提问作者Francislainy Campos
相关产品推荐
相关产品推荐

