Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to remove redundant useless Dependencies by Gradle in IDEA

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to delete redundant useless dependencies in Gradle in IDEA. I hope you will get something after reading this article. Let's discuss it together.

Brief introduction

After a long time of development in the project, there will be a lot of dependencies that were introduced and then no longer used, and it is difficult for the naked eye to distinguish and delete them.

At this point, we can use the analysis useless dependency plug-in to deal with it: gradle-lint-plugin

How to use

Note: he may have a reference dependency to delete errors, which needs to be checked and tested after deletion.

Moreover, only single module projects are supported here. If it is a multi-module project, please refer to the official documentation to deal with it.

Official document address: https://github.com/nebula-plugins/gradle-lint-plugin/wiki

1. Introduce plug-in

Refer to the plug-in in the build.gradle of the project. The latest version number can be viewed here:

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

If you already have a plug-in in your project, you can add it later, such as mine:

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. Application plug-in

Apply the plug-in in build.gradle and configure the detection rules anywhere:

Apply plugin: "nebula.lint" gradleLint.rules= ['unused-dependency'] 3. Use Gradle to reload the project

When IDEA uses Gradle to reload the project, the Lint menu appears, as shown in the following figure:

4. Generate a report

Click lint-> generateGradleLintReport to generate the report.

The report retains only different types of ellipsis results, and you can see the following four types of report results:

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)

Where this dependency is unused and can be removed represents a dependency that can be deleted.

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. Delete useless dependencies

We can see the last sentence of the report.

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

Finally, you can perform fixGradleLint to automatically delete useless dependencies.

The fix report is as follows, and we can see that in addition to the useless dependencies, some other dependencies have also been removed because he thinks we can directly introduce their corresponding dependencies instead of the entire dependency package.

For example, in compile group: 'com.baomidou', name:' mybatis-plus-boot-starter', version: '3.3.1' we use only part of the dependency of the entire starter package, and we can rely on this part of the dependency instead of the entire starter package. You can also manually choose to keep this without following his recommendation.

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

Special case Lombok

Lombok is a tool that automatically generates Getter/Setter and constructors at compile time.

Nebula Lint still detects useless dependencies. The log is as follows:

> 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

Handling method (modify version number):

GradleLint.ignore ('unused-dependency') {compileOnly group:' org.projectlombok', name: 'lombok', version:'1.16.20'} after reading this article, I believe you have some understanding of "how to remove redundant and useless dependencies in IDEA". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 285

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report