IDEA?下?Gradle?刪除多余無(wú)用依賴(lài)的處理方法
簡(jiǎn)介
項(xiàng)目中經(jīng)過(guò)很久開(kāi)發(fā),會(huì)有很多當(dāng)初引入后來(lái)又不再使用的依賴(lài),靠肉眼很難分辨刪除。
這時(shí)候,我們可以使用分析無(wú)用依賴(lài)插件進(jìn)行處理:gradle-lint-plugin
如何使用
注意: 他可能存在刪除錯(cuò)誤的引用依賴(lài),需要?jiǎng)h除后進(jìn)行檢查和測(cè)試
并且,這里僅支持單模塊項(xiàng)目,如果是多模塊項(xiàng)目請(qǐng)參考官方文檔進(jìn)行處理
官方文檔地址: https://github.com/nebula-plugins/gradle-lint-plugin/wiki
1.引入插件
在項(xiàng)目的 build.gradle
中引用該插件, 最新版本號(hào)可以 點(diǎn)擊這里查看:
plugins { id 'nebula.lint' version '17.7.0' }
如何你項(xiàng)目中本身已經(jīng)有插件,則可以在后面追加,例如我的:
plugins { id 'org.springframework.boot' version '2.3.5.RELEASE' id 'io.spring.dependency-management' version '1.0.10.RELEASE' id 'java' id 'nebula.lint' version '17.7.0' }
2.應(yīng)用插件
在 build.gradle
應(yīng)用 該插件,并在任意位置,配置檢測(cè)規(guī)則:
apply plugin :"nebula.lint" gradleLint.rules=['unused-dependency']
3.使用 Gradle 進(jìn)行重新載入項(xiàng)目
IDEA
使用 Gradle 進(jìn)行重新載入項(xiàng)目,則會(huì)出現(xiàn) Lint
菜單, 如下圖所示:
4.生成報(bào)告
點(diǎn)擊 lint -> generateGradleLintReport
, 可以生成報(bào)告。
報(bào)告僅保留不同類(lèi)型的省略結(jié)果,可以看到有以下四種報(bào)告結(jié)果:
- one or more classes are required by your code directly (no auto-fix available)
- this dependency is unused and can be removed
- this dependency should be removed since its artifact is empty (no auto-fix available)
- this dependency is a service provider unused at compileClasspath time and can be moved to the runtimeOnly configuration (no auto-fix available)
其中, this dependency is unused and can be removed
表示可以刪除的依賴(lài)。
Executing 'generateGradleLintReport'...
> Task :generateGradleLintReport
Generated a report containing information about 83 lint violations in this project
> Task :autoLintGradle
This project contains lint violations. A complete listing of the violations follows.
Because none were serious, the build's overall status was unaffected.
warning unused-dependency one or more classes in org.mockito:mockito-core:3.3.3 are required by your code directly (no auto-fix available)
warning unused-dependency this dependency should be removed since its artifact is empty (no auto-fix available)
build.gradle:59
implementation 'org.springframework.boot:spring-boot-starter-actuator'
warning unused-dependency this dependency is a service provider unused at compileClasspath time and can be moved to the runtimeOnly configuration (no auto-fix available)
build.gradle:69
compileOnly 'org.projectlombok:lombok'
warning unused-dependency this dependency is unused and can be removed
build.gradle:101
compile group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
? 83 problems (0 errors, 83 warnings)
To apply fixes automatically, run fixGradleLint, review, and commit the changes.
5. 刪除無(wú)用依賴(lài)
我們可以看到報(bào)告的最后一句話(huà),
To apply fixes automatically, run fixGradleLint, review, and commit the changes.
最后,可以執(zhí)行 fixGradleLint
自動(dòng)刪除無(wú)用依賴(lài)。
修復(fù)報(bào)告如下,我們可以看到除了無(wú)用的依賴(lài),還有一些其他的依賴(lài)也被刪除了,原因是因?yàn)椋J(rèn)為我們可以直接引入其對(duì)應(yīng)的依賴(lài)而不是整個(gè)依賴(lài)包。
例如,compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.3.1'
中我們只用了整個(gè) starter 包的一部分依賴(lài),可以?xún)H依賴(lài)這一部分依賴(lài),而不是整個(gè) starter 包。這個(gè)也可以不按照他的推薦,直接手動(dòng)選擇保留。
Executing 'fixGradleLint'...
> Task :compileJava
> Task :processResources UP-TO-DATE
> Task :classes
> Task :compileTestJava
> Task :fixGradleLint
This project contains lint violations. A complete listing of my attempt to fix them follows. Please review and commit the changes.
needs fixing unused-dependency one or more classes in com.baomidou:mybatis-plus-core:3.3.1 are required by your code directly
fixed unused-dependency this dependency is unused and can be removed
build.gradle:105
compile 'org.ehcache:ehcache'
build.gradle:106
compile 'javax.cache:cache-api'
build.gradle:107
compile 'org.mybatis:mybatis-typehandlers-jsr310:1.0.2'
build.gradle:112
testImplementation 'org.springframework.security:spring-security-test'
Corrected 17 lint problems
特殊情況
Lombok
Lombok 是一個(gè)編譯時(shí)自動(dòng)生成 Getter/Setter 和構(gòu)造器的工具。
Nebula Lint 依舊會(huì)檢測(cè)無(wú)用的依賴(lài),日志如下:
> Task :lintGradle FAILED
This project contains lint violations. A complete listing of the violations follows.
Because none were serious, the build's overall status was unaffected.
warning unused-dependency this dependency is a service provider unused at compile time and can be moved to the runtime configuration
處理方式(修改版本號(hào)):
gradleLint.ignore('unused-dependency') { compileOnly group: 'org.projectlombok', name: 'lombok', version:'1.16.20' }
相關(guān) Github issue: Nebula Lint does not handle certain @Retention(RetentionPolicy.SOURCE) annotations correctly #170
總結(jié)
使用該插件可以一定程度上幫助我們刪除無(wú)用依賴(lài),但是也可能會(huì)多刪除有用的依賴(lài)。
需要在使用插件自動(dòng)修復(fù)后手動(dòng)檢測(cè)項(xiàng)目,驗(yàn)證是否會(huì)出現(xiàn)問(wèn)題,避免導(dǎo)致上線發(fā)布錯(cuò)誤的負(fù)優(yōu)化。
到此這篇關(guān)于IDEA 下 Gradle 刪除多余無(wú)用依賴(lài)的文章就介紹到這了,更多相關(guān)idea Gradle 刪除依賴(lài)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Intellij?IDEA根據(jù)maven依賴(lài)名查找它是哪個(gè)pom.xml引入的(圖文詳解)
- idea創(chuàng)建maven父子工程導(dǎo)致子工程無(wú)法導(dǎo)入父工程依賴(lài)
- springboot項(xiàng)目之相互依賴(lài)報(bào)錯(cuò)問(wèn)題(基于idea)
- idea重新下載已刪除的maven依賴(lài)包操作
- 如何在IDEA中查看依賴(lài)關(guān)系的方法步驟
- IDEA離線安裝maven helper插件的圖文教程
- IDEA配置使用Maven Helper插件的方法(詳細(xì)配置)
- idea使用Maven Helper插件去掉無(wú)用的poom 依賴(lài)信息(詳細(xì)步驟)
相關(guān)文章
關(guān)于使用POI向word中添加圖片的問(wèn)題
這篇文章主要介紹了關(guān)于使用POI向word中添加圖片的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12java9版本特性資源自動(dòng)關(guān)閉的語(yǔ)法增強(qiáng)
這篇文章主要為大家介紹了java9版本特性資源自動(dòng)關(guān)閉的語(yǔ)法增強(qiáng)的詳細(xì)使用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03ocp開(kāi)閉原則_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了ocp開(kāi)閉原則的相關(guān)資料,ocp開(kāi)閉原則指導(dǎo)我們?nèi)绾谓⒁粋€(gè)穩(wěn)定的、靈活的系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Java集合與數(shù)組區(qū)別簡(jiǎn)介及相互轉(zhuǎn)換實(shí)例
這篇文章主要介紹了Java集合與數(shù)組區(qū)別簡(jiǎn)介及相互轉(zhuǎn)換實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01淺談java 中文件的讀取File、以及相對(duì)路徑的問(wèn)題
今天小編就為大家分享一篇淺談java 中文件的讀取File、以及相對(duì)路徑的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Spring Boot 指定外部啟動(dòng)配置文件詳解
在springboot項(xiàng)目中,也可以使用yml類(lèi)型的配置文件代替properties文件。接下來(lái)通過(guò)本文給大家分享Springboot配置文件的使用,感興趣的朋友一起看看吧2021-09-09