欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IDEA?下?Gradle?刪除多余無用依賴的處理方法

 更新時間:2022年03月31日 12:00:45   作者:assember  
這篇文章主要介紹了IDEA下Gradle刪除多余無用依賴,使用該插件可以一定程度上幫助我們刪除無用依賴,但是也可能會多刪除有用的依賴,需要在使用插件自動修復后手動檢測項目,驗證是否會出現(xiàn)問題,避免導致上線發(fā)布錯誤的負優(yōu)化

簡介

項目中經(jīng)過很久開發(fā),會有很多當初引入后來又不再使用的依賴,靠肉眼很難分辨刪除。

這時候,我們可以使用分析無用依賴插件進行處理:gradle-lint-plugin

如何使用

注意: 他可能存在刪除錯誤的引用依賴,需要刪除后進行檢查和測試

并且,這里僅支持單模塊項目,如果是多模塊項目請參考官方文檔進行處理

官方文檔地址: https://github.com/nebula-plugins/gradle-lint-plugin/wiki

1.引入插件

在項目的 build.gradle 中引用該插件, 最新版本號可以 點擊這里查看:

plugins {
  id 'nebula.lint' version '17.7.0'
}

如何你項目中本身已經(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.應用插件

build.gradle 應用 該插件,并在任意位置,配置檢測規(guī)則:

apply plugin :"nebula.lint"
gradleLint.rules=['unused-dependency']

3.使用 Gradle 進行重新載入項目

IDEA 使用 Gradle 進行重新載入項目,則會出現(xiàn) Lint 菜單, 如下圖所示:

4.生成報告

點擊 lint -> generateGradleLintReport, 可以生成報告。

報告僅保留不同類型的省略結果,可以看到有以下四種報告結果:

  • 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 表示可以刪除的依賴。

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. 刪除無用依賴

我們可以看到報告的最后一句話,

To apply fixes automatically, run fixGradleLint, review, and commit the changes.

最后,可以執(zhí)行 fixGradleLint 自動刪除無用依賴。

修復報告如下,我們可以看到除了無用的依賴,還有一些其他的依賴也被刪除了,原因是因為,他認為我們可以直接引入其對應的依賴而不是整個依賴包。

例如,compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.3.1' 中我們只用了整個 starter 包的一部分依賴,可以僅依賴這一部分依賴,而不是整個 starter 包。這個也可以不按照他的推薦,直接手動選擇保留。

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 是一個編譯時自動生成 Getter/Setter 和構造器的工具。

Nebula Lint 依舊會檢測無用的依賴,日志如下:

> 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

處理方式(修改版本號):

gradleLint.ignore('unused-dependency') {
		compileOnly group: 'org.projectlombok', name: 'lombok', version:'1.16.20'
	}

相關 Github issue: Nebula Lint does not handle certain @Retention(RetentionPolicy.SOURCE) annotations correctly #170

總結

使用該插件可以一定程度上幫助我們刪除無用依賴,但是也可能會多刪除有用的依賴。

需要在使用插件自動修復后手動檢測項目,驗證是否會出現(xiàn)問題,避免導致上線發(fā)布錯誤的負優(yōu)化。

到此這篇關于IDEA 下 Gradle 刪除多余無用依賴的文章就介紹到這了,更多相關idea Gradle 刪除依賴內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java實現(xiàn)簡單堆棧代碼

    Java實現(xiàn)簡單堆棧代碼

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單堆棧代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • mybatis中大批量數(shù)據(jù)插入解析

    mybatis中大批量數(shù)據(jù)插入解析

    這篇文章主要介紹了mybatis中大批量數(shù)據(jù)插入解析,使用Mybatis框架批量插入的3種方法,分別是多次調用insert方法、foreach標簽、batch模式,本文來詳細說明一下,需要的朋友可以參考下
    2024-01-01
  • 關于使用POI向word中添加圖片的問題

    關于使用POI向word中添加圖片的問題

    這篇文章主要介紹了關于使用POI向word中添加圖片的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java遍歷Map對象的四種方式

    Java遍歷Map對象的四種方式

    本文給大家介紹java遍歷map對象的四種方式,對java中遍歷map感興趣的朋友可以一起了解了解
    2015-10-10
  • java9版本特性資源自動關閉的語法增強

    java9版本特性資源自動關閉的語法增強

    這篇文章主要為大家介紹了java9版本特性資源自動關閉的語法增強的詳細使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2022-03-03
  • ocp開閉原則_動力節(jié)點Java學院整理

    ocp開閉原則_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了ocp開閉原則的相關資料,ocp開閉原則指導我們如何建立一個穩(wěn)定的、靈活的系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Java中斷一個線程操作示例

    Java中斷一個線程操作示例

    這篇文章主要介紹了Java中斷一個線程操作,結合實例形式分析了java中斷線程相關的interrupt()、isInterrupted()及interrupted()函數(shù)使用技巧,需要的朋友可以參考下
    2019-10-10
  • Java集合與數(shù)組區(qū)別簡介及相互轉換實例

    Java集合與數(shù)組區(qū)別簡介及相互轉換實例

    這篇文章主要介紹了Java集合與數(shù)組區(qū)別簡介及相互轉換實例,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • 淺談java 中文件的讀取File、以及相對路徑的問題

    淺談java 中文件的讀取File、以及相對路徑的問題

    今天小編就為大家分享一篇淺談java 中文件的讀取File、以及相對路徑的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Spring Boot 指定外部啟動配置文件詳解

    Spring Boot 指定外部啟動配置文件詳解

    在springboot項目中,也可以使用yml類型的配置文件代替properties文件。接下來通過本文給大家分享Springboot配置文件的使用,感興趣的朋友一起看看吧
    2021-09-09

最新評論