配置mycila版license-maven-plugin后Maven误引codehaus版本报错
问题
我在一个遗留项目的根模块中添加了mycila版本的license-maven-plugin,配置如下:
<plugin> <groupId>com.mycila</groupId> <artifactId>license-maven-plugin</artifactId> <version>4.3</version> <configuration>...</configuration> <executions> <execution> <goals> <goal>format</goal> </goals> </execution> </executions> </plugin>
执行命令 mvn license:format 时,出现错误:
Could not find goal 'format' in plugin org.codehaus.mojo:license-maven-plugin:2.4.0 among available goals
排查细节:
- 工作区所有pom.xml中未定义org.codehaus.mojo版本的license-maven-plugin,甚至未出现"codehaus"字样
- 依赖层级分析中找不到codehaus相关的license插件,执行
mvn org.apache.maven.plugins:maven-dependency-plugin:3.6.3:resolve-plugins后,仅列出com.mycila版本的license-maven-plugin - 删除本地.m2仓库中所有codehaus依赖后,执行
mvn license:format仍会自动下载org.codehaus.mojo的license-maven-plugin及相关依赖(mojo-parent、animal-sniffer-parent等) - 执行
mvn license:format -X的调试日志显示:[DEBUG] Resolving plugin prefix license from [org.apache.maven.plugins, org.codehaus.mojo],完全未提及mycila
解决方案
核心原因是Maven默认仅从org.apache.maven.plugins和org.codehaus.mojo两个groupId下解析插件前缀,com.mycila不在默认搜索范围内,导致Maven找不到前缀license对应的目标插件,转而调用默认搜索到的codehaus版本(该版本没有format目标)。
可通过以下两种方式解决:
方式1:执行命令时指定完整插件坐标
直接在命令中明确插件的groupId、artifactId和版本,跳过前缀解析步骤:
mvn com.mycila:license-maven-plugin:4.3:format
方式2:在pom.xml中配置插件前缀映射
在根模块的pom.xml中添加<pluginGroups>配置,将com.mycila加入Maven的插件搜索组,这样Maven解析license前缀时会优先查找该组下的插件:
<project> <!-- 其他项目配置 --> <pluginGroups> <pluginGroup>com.mycila</pluginGroup> </pluginGroups> <!-- 已有的plugin配置 --> </project>
配置完成后,直接执行 mvn license:format 即可正常调用mycila版本的插件。
内容的提问来源于stack exchange,提问作者Anna Koskinen
相关产品推荐
相关产品推荐

