多模块Maven项目配置Checkstyle引用palantir多XML配置方案咨询
解决方案:多模块Maven项目中配置Checkstyle + Palantir Java Format共享多文件配置
最优方案:依赖插件提取JAR内配置文件(方案2优化版)
该方案无需修改现有配置文件,也不用调整项目类型,完美适配当前结构:
- 在聚合POM中配置Maven Dependency Plugin,将
example-company-configJAR中的Checkstyle配置文件提取到构建目录:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <id>extract-checkstyle-config</id> <phase>validate</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>你的groupId</groupId> <artifactId>example-company-config</artifactId> <version>你的版本</version> <type>jar</type> <overWrite>false</overWrite> <outputDirectory>${project.build.directory}/checkstyle-config</outputDirectory> <!-- 仅提取需要的三个配置文件,避免冗余 --> <includes>**/checkstyle.xml,**/palantir-checks.xml,**/import-control.xml</includes> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin>
- 配置Checkstyle Plugin,指定提取后的配置文件路径,并设置
config_loc为提取目录:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>checkstyle</id> <phase>verify</phase> <goals> <goal>check</goal> </goals> </execution> </executions> <configuration> <configLocation>${project.build.directory}/checkstyle-config/checkstyle.xml</configLocation> <propertyExpansion>config_loc=${project.build.directory}/checkstyle-config</propertyExpansion> <encoding>UTF-8</encoding> </configuration> <dependencies> <!-- 引入palantir的checkstyle依赖 --> <dependency> <groupId>com.palantir.java</groupId> <artifactId>palantir-java-format-checkstyle</artifactId> <version>你的版本</version> </dependency> </dependencies> </plugin>
- 执行顺序验证:
validate阶段早于verify阶段,提取操作会先完成,确保Checkstyle能读取到所有配置文件。
备选方案:将配置项目转为POM类型(方案3简化版)
若不想使用提取操作,可将example-company-config改为pom类型项目,直接把配置文件发布到仓库:
- 修改example-company-config的POM:
<packaging>pom</packaging> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>3.1.1</version> <configuration> <createChecksum>true</createChecksum> <!-- 将配置文件安装到仓库指定目录 --> <fileSets> <fileSet> <directory>src/main/resources</directory> <outputDirectory>checkstyle</outputDirectory> <includes> <include>**/*.xml</include> </includes> </fileSet> </fileSets> </configuration> </plugin> </plugins> </build>
- 在聚合POM中引用配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.3.0</version> <configuration> <configLocation>classpath:/checkstyle/checkstyle.xml</configLocation> <propertyExpansion>config_loc=classpath:/checkstyle/</propertyExpansion> </configuration> <dependencies> <dependency> <groupId>你的groupId</groupId> <artifactId>example-company-config</artifactId> <version>你的版本</version> <type>pom</type> </dependency> <!-- palantir依赖同上 --> </dependencies> </plugin>
方案1不可行的原因
Checkstyle不支持直接引用JAR归档内的其他XML文件作为配置依赖,config_loc仅能指向本地文件系统路径或classpath根目录,多文件引用时classpath路径无法被正确解析为相对路径,因此特殊语法的思路无法实现。
额外提示
- 后续迁移到Spotless时,可保留
example-company-config项目,仅添加Spotless配置文件即可,无需重构现有共享配置结构。 - 可将插件配置放到聚合POM的
<pluginManagement>中,让子模块继承,减少重复配置。
内容的提问来源于stack exchange,提问作者Florian Wolters
相关产品推荐
相关产品推荐

