执行mvn clean install时maven-scr-plugin报错求助
问题场景
执行mvn clean install时触发以下错误:
[ERROR] Failed to execute goal org.apache.felix:maven-scr-plugin:1.7.4:scr (generate-scr-descriptor) on project companyname-bundle: Execution generate-scr-descriptor of goal org.apache.felix:maven-scr-plugin:1.7.4:scr failed: org.apache.felix.scrplugin.tags.annotation.AnnotationTagProvider is not an ImageIO SPI class -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn-rf :companyname-bundle
当前环境与配置
- Maven 3.5.4
- Java 11
- 子模块pom.xml插件配置:
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> <executions> <execution> <id>generate-scr-descriptor</id> <goals> <goal>scr</goal> </goals> </execution> </executions> </plugin>
- 主pom.xml插件配置:
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> <version>1.7.4</version> </plugin>
已尝试的无效操作
- 切换至Java 1.8 + Maven 3.6.3,触发surefire-plugin相关错误
- 将插件版本改为1.22.0,触发依赖解析错误:
[ERROR] Plugin org.apache.felix:maven-scr-plugin:1.22.0 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.felix:maven-scr-plugin:jar:1.22.0: Could not transfer artifact org.apache.felix:maven-scr-plugin:pom:1.22.0 from/to barclays (https://nexus.barcapint.com/nexus/content/groups/barclays/): Connection reset -> [Help 1]
解决方案
方案1:升级插件到Java11兼容版本并修复依赖问题
maven-scr-plugin 1.7.4不支持Java11,需升级到1.20.0及以上版本(该版本开始适配Java9+)。针对依赖解析失败问题,可按以下操作处理:
- 检查公司Nexus仓库是否有对应版本插件,若缺失联系运维同步;或在pom中添加Apache官方仓库作为补充:
<repositories> <repository> <id>apache-releases</id> <url>https://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>apache-releases</id> <url>https://repo.maven.apache.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories>
- 清理本地Maven缓存并强制更新依赖:执行
mvn clean install -U - 若网络限制无法访问官方仓库,手动下载插件jar包到本地仓库:
mvn install:install-file -Dfile=maven-scr-plugin-1.20.0.jar -DgroupId=org.apache.felix -DartifactId=maven-scr-plugin -Dversion=1.20.0 -Dpackaging=jar
方案2:在Java11环境下为插件添加JVM参数
若无法升级插件版本,可通过JVM参数屏蔽ImageIO SPI加载异常:
修改主pom.xml中的插件配置,添加参数:
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> <version>1.7.4</version> <executions> <execution> <id>generate-scr-descriptor</id> <goals> <goal>scr</goal> </goals> <configuration> <jvmArguments>-Djava.awt.headless=true -Djavax.imageio.spi.classNotFoundDebug=true</jvmArguments> </configuration> </execution> </executions> </plugin>
该参数会跳过ImageIO相关SPI的错误加载,避免触发构建异常。
方案3:修复Java8环境下的surefire-plugin错误
若坚持使用Java8,针对surefire-plugin报错,升级插件版本到2.22.2及以上(兼容Java8),在主pom中配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin>
同时确保测试代码兼容Java8语法与API规范。
内容的提问来源于stack exchange,提问作者aniketh-avula

