如何在 Gradle 中查找/删除未使用的依赖项 [英] How to find/remove unused dependencies in Gradle

查看:46
本文介绍了如何在 Gradle 中查找/删除未使用的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中找到未使用的依赖项.Gradle 中是否有类似 Maven 的功能?

解决方案

UPDATE: 28-06-2016: Android 对未使用依赖项的支持

<块引用>

2017 年 6 月,他们发布了4.0.0 版本并将根项目名称 "gradle-lint-plugin" 重命名为nebula-lint-plugin".他们还添加了 Android 支持未使用的依赖.

<小时>

2016 年 5 月,Gradle 实现了Gradle lint 插件 用于查找和删除不需要的依赖

Gradle Lint 插件:完整文档

<块引用>

Gradle Lint 插件是一个可插拔和可配置的 linter 工具,用于识别和报告滥用或弃用的模式Gradle 脚本和相关文件.

这个插件有各种规则.未使用的依赖规则就是其中之一.它具有三个具体特征.

  1. 删除未使用的依赖项.
  2. 促进代码直接使用的传递依赖项显式一阶依赖关系.
  3. 将依赖项重新定位到正确"的配置.

要应用规则,请添加:

gradleLint.rules += 'unused-dependency'

未使用的依赖规则的详细信息是在最后一部分给出.

应用 Gradle lint 插件:

buildscript { 存储库 { jcenter() } }插件{id 'nebula.lint' 版本 '0.30.2'}

或者:

buildscript {存储库 { jcenter() }依赖{类路径 'com.netflix.nebula:gradle-lint-plugin:latest.release'}}应用插件:'nebula.lint'

定义您要针对哪些规则进行 lint:

gradleLint.rules = ['all-dependency']//在这里添加任意数量的规则

对于企业构建,我们建议在 init.gradle 脚本或通过 Gradle apply from 机制包含的 Gradle 脚本中定义 lint 规则.

对于多模块项目,我们建议在 allprojects 块中应用插件:

所有项目{应用插件:'nebula.lint'gradleLint.rules = ['all-dependency']//在这里添加任意数量的规则}

<小时><小时>

未使用的依赖规则的详细信息是本部分给出

要应用规则,请添加:

gradleLint.rules += 'unused-dependency'

该规则检查源自项目源集的已编译二进制文件用于类引用,并将这些引用与您在 dependencies 中声明的依赖项匹配a> 块.

具体来说,该规则对依赖项做了以下调整:

1.删除未使用的依赖项

  • 像 com.amazonaws:aws-java-sdk 这样的家庭风格的 jars 被删除,因为它们不包含任何代码

2.将代码直接使用的传递依赖项提升为显式一阶依赖项

  • 这有破坏家族风格 JAR 文件的副作用,比如com.amazonaws:aws-java-sdk,进入你真正的部分使用,并将它们添加为一阶依赖项

3.将依赖项重新定位到正确"的配置

  • Webjar 被移动到运行时配置
  • 不包含 META-INF 之外的任何类内容的 JAR 文件是移至运行时
  • 'xerces'、'xercesImpl'、'xml-apis' 应该总是运行时范围
  • 服务提供者(包含 META-INF/服务的 JAR 文件),如如果没有任何可证明的,mysql-connector-java 将移至运行时编译时参考
  • 依赖项被移动到最高的源集配置可能的.例如,'junit' 被重新定位到 testCompile 除非在主源集中有一个明确的依赖关系(罕见).
<小时><小时>

更新:以前的插件

为了您的信息,我想分享以前的插件

  1. 发现未使用的依赖项、声明的和可传递的依赖项的 Gradle 插件是 com.github.nullstress.dependency-analysis

但是其最新版本 1.0.3 是在 2014 年 12 月 23 日创建的.之后没有任何更新.

<块引用>

注意:我们的许多工程师都对这个插件感到困惑,因为他们只更新了版本号,没有别的.

I wanted to find unused dependencies in my project. Is there a feature for this in Gradle, like in Maven?

解决方案

UPDATE: 28-06-2016: Android support to unused-dependency

In June, 2017, they have released the 4.0.0 version and renamed the root project name "gradle-lint-plugin" to "nebula-lint-plugin". They have also added Android support to unused-dependency.


In May 2016 Gradle has implemented the Gradle lint plugin for finding and removing unwanted dependency

Gradle Lint Plugin: Full Documentation

The Gradle Lint plugin is a pluggable and configurable linter tool for identifying and reporting on patterns of misuse or deprecations in Gradle scripts and related files.

This plugin has various rules. Unused Dependency Rule is one of them. It has three specific characteristics.

  1. Removes unused dependencies.
  2. Promotes transitive dependencies that are used directly by your code to explicit first order dependencies.
  3. Relocates dependencies to the 'correct' configuration.

To apply the rule, add:

gradleLint.rules += 'unused-dependency'

Details of Unused Dependency Rule is given in the last part.

To apply the Gradle lint plugin:

buildscript { repositories { jcenter() } }
plugins {
  id 'nebula.lint' version '0.30.2'
}

Alternatively:

buildscript {
  repositories { jcenter() }
  dependencies {
    classpath 'com.netflix.nebula:gradle-lint-plugin:latest.release'
  }
}

apply plugin: 'nebula.lint'

Define which rules you would like to lint against:

gradleLint.rules = ['all-dependency'] // Add as many rules here as you'd like

For an enterprise build, we recommend defining the lint rules in a init.gradle script or in a Gradle script that is included via the Gradle apply from mechanism.

For multimodule projects, we recommend applying the plugin in an allprojects block:

allprojects {
  apply plugin: 'nebula.lint'
  gradleLint.rules = ['all-dependency'] // Add as many rules here as you'd like
}



Details of Unused Dependency Rule is given in this part

To apply the rule, add:

gradleLint.rules += 'unused-dependency'

The rule inspects compiled binaries emanating from your project's source sets looking for class references and matches those references to the dependencies that you have declared in your dependencies block.

Specifically, the rule makes the following adjustments to dependencies:

1. Removes unused dependencies

  • Family-style jars like com.amazonaws:aws-java-sdk are removed, as they don't contain any code

2. Promotes transitive dependencies that are used directly by your code to explicit first order dependencies

  • This has the side effect of breaking up family style JAR files, like com.amazonaws:aws-java-sdk, into the parts that you are actually using, and adding those as first order dependencies

3. Relocates dependencies to the 'correct' configuration

  • Webjars are moved to the runtime configuration
  • JAR files that don't contain any classes and content outside of META-INF are moved to runtime
  • 'xerces', 'xercesImpl', 'xml-apis' should always be runtime scoped
  • Service providers (JAR files containing META-INF/services) like mysql-connector-java are moved to runtime if there isn't any provable compile-time reference
  • Dependencies are moved to the highest source set configuration possible. For example, 'junit' is relocated to testCompile unless there is an explicit dependency on it in the main source set (rare).


UPDATE: Previous plugins

For your kind information, I want to share about previous plugins

  1. The Gradle plugin that finds unused dependencies, declared and transitive is com.github.nullstress.dependency-analysis

But its latest version 1.0.3 is created 23 December 2014. After that there aren't any updates.

N.B: Many of our engineers are being confused about this plugin as they updated only the version number, nothing else.

这篇关于如何在 Gradle 中查找/删除未使用的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆