多模块复合构建项目中如何排除PandaDataAccessLayer的Checkstyle检查
我有一个基于Gradle的多模块复合构建项目,项目结构如下:
Panda. // 根项目 -PandaService. // 服务模块 -PandaDataAccessLayer // 由JOOQ自动生成代码的DAL模块
由于PandaDataAccessLayer的代码是JOOQ自动生成的,我希望将该模块完全排除在Checkstyle检查之外。我的checkstyle.xml位于Panda/config/checkstyle/checkstyle.xml,根项目Panda的build.gradle配置如下:
repositories { mavenCentral() // Use Maven Central for resolving dependencies. } subprojects { apply plugin: 'java' apply plugin: 'checkstyle' checkstyle { toolVersion = '10.7.0' configFile = rootProject.file('config/checkstyle/checkstyle.xml') checkstyleMain.exclude '**/PandaDataAccessLayer/**' } sourceCompatibility = '11' targetCompatibility = '11' }
执行./gradlew build后,Checkstyle正常运行,但生成的报告显示仍对PandaDataAccessLayer模块的代码进行了检查:
Checkstyle rule violations were found. See the report at:file:///****/panda/PandaDataAccessLayer/build/reports/checkstyle/main.html
请问如何将PandaDataAccessLayer模块完全排除在Checkstyle检查之外?
方法1:仅给指定模块应用Checkstyle插件
修改根项目的build.gradle,跳过给PandaDataAccessLayer模块应用Checkstyle插件:
repositories { mavenCentral() } subprojects { apply plugin: 'java' sourceCompatibility = '11' targetCompatibility = '11' // 仅对非PandaDataAccessLayer的模块启用Checkstyle if (project.name != 'PandaDataAccessLayer') { apply plugin: 'checkstyle' checkstyle { toolVersion = '10.7.0' configFile = rootProject.file('config/checkstyle/checkstyle.xml') } } }
方法2:直接禁用目标模块的Checkstyle任务
在PandaDataAccessLayer模块的build.gradle中添加以下配置,禁用该模块所有Checkstyle相关任务:
// 禁用当前模块的所有Checkstyle任务 tasks.withType(com.puppycrawl.tools.checkstyle.Checkstyle).configureEach { enabled = false }
方法3:在根项目中针对性禁用任务
如果不想修改子模块配置,也可以在根项目的build.gradle中直接对PandaDataAccessLayer模块禁用Checkstyle任务:
repositories { mavenCentral() } subprojects { apply plugin: 'java' apply plugin: 'checkstyle' checkstyle { toolVersion = '10.7.0' configFile = rootProject.file('config/checkstyle/checkstyle.xml') } sourceCompatibility = '11' targetCompatibility = '11' // 对PandaDataAccessLayer模块禁用所有Checkstyle任务 if (project.name == 'PandaDataAccessLayer') { tasks.withType(com.puppycrawl.tools.checkstyle.Checkstyle).configureEach { enabled = false } } }
原配置失效原因
你之前使用的checkstyleMain.exclude '**/PandaDataAccessLayer/**'是用来排除当前模块内部路径匹配的文件,但PandaDataAccessLayer是独立的子模块,这个配置无法跨模块生效,因此该模块的代码仍会被检查。
内容的提问来源于stack exchange,提问作者Yang Liu

