IDEA 下 Gradle 删除多余无用依赖的处理方法

 更新时间:2022年03月31日 12:00:45   作者:assember  
这篇文章主要介绍了IDEA下Gradle删除多余无用依赖,使用该插件可以一定程度上帮助我们删除无用依赖,但是也可能会多删除有用的依赖,需要在使用插件自动修复后手动检测项目,验证是否会出现问题,避免导致上线发布错误的负优化

简介

项目中经过很久开发,会有很多当初引入后来又不再使用的依赖,靠肉眼很难分辨删除。

这时候,我们可以使用分析无用依赖插件进行处理:gradle-lint-plugin

如何使用

注意: 他可能存在删除错误的引用依赖,需要删除后进行检查和测试

并且,这里仅支持单模块项目,如果是多模块项目请参考官方文档进行处理

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

1.引入插件

在项目的 build.gradle 中引用该插件, 最新版本号可以 点击这里查看:

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

如何你项目中本身已经有插件,则可以在后面追加,例如我的:

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 应用 该插件,并在任意位置,配置检测规则:

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

3.使用 Gradle 进行重新载入项目

IDEA 使用 Gradle 进行重新载入项目,则会出现 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.

最后,可以执行 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

总结

使用该插件可以一定程度上帮助我们删除无用依赖,但是也可能会多删除有用的依赖。

需要在使用插件自动修复后手动检测项目,验证是否会出现问题,避免导致上线发布错误的负优化。

到此这篇关于IDEA 下 Gradle 删除多余无用依赖的文章就介绍到这了,更多相关idea Gradle 删除依赖内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java实现简单堆栈代码

    Java实现简单堆栈代码

    这篇文章主要为大家详细介绍了Java实现简单堆栈代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • mybatis中大批量数据插入解析

    mybatis中大批量数据插入解析

    这篇文章主要介绍了mybatis中大批量数据插入解析,使用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开闭原则_动力节点Java学院整理

    ocp开闭原则_动力节点Java学院整理

    这篇文章主要为大家详细介绍了ocp开闭原则的相关资料,ocp开闭原则指导我们如何建立一个稳定的、灵活的系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Java中断一个线程操作示例

    Java中断一个线程操作示例

    这篇文章主要介绍了Java中断一个线程操作,结合实例形式分析了java中断线程相关的interrupt()、isInterrupted()及interrupted()函数使用技巧,需要的朋友可以参考下
    2019-10-10
  • Java集合与数组区别简介及相互转换实例

    Java集合与数组区别简介及相互转换实例

    这篇文章主要介绍了Java集合与数组区别简介及相互转换实例,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • 浅谈java 中文件的读取File、以及相对路径的问题

    浅谈java 中文件的读取File、以及相对路径的问题

    今天小编就为大家分享一篇浅谈java 中文件的读取File、以及相对路径的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Spring Boot 指定外部启动配置文件详解

    Spring Boot 指定外部启动配置文件详解

    在springboot项目中,也可以使用yml类型的配置文件代替properties文件。接下来通过本文给大家分享Springboot配置文件的使用,感兴趣的朋友一起看看吧
    2021-09-09

最新评论