Java 如何在 Gradle 中查找/删除未使用的依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19379517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:36:36  来源:igfitidea点击:

How to find/remove unused dependencies in Gradle

javamavengradledependency-management

提问by Alexander Bezrodniy

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

我想在我的项目中找到未使用的依赖项。Gradle 中是否有此功能,例如在 Maven 中?

回答by EFernandes

Editor's Note: This answer is out of date. Please see the top answer.

编者注:此答案已过时。请参阅顶部答案

You can try the com.github.nullstress.dependency-analysisGradle plugin

你可以试试com.github.nullstress.dependency-analysisGradle 插件

Build script snippet for use in all Gradle versions:

构建用于所有 Gradle 版本的脚本片段:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath "com.github.nullstress:DependencyAnalysisPlugin:1.0.3"
  }
}

apply plugin: "com.github.nullstress.dependency-analysis"

Build script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:

为 Gradle 2.1 中引入的新的孵化插件机制构建脚本片段:

plugins {
  id "com.github.nullstress.dependency-analysis" version "1.0.3"
}

Also, there is a thread (Is there a Gradle equivalent of "mvn dependency:analyze"?) in the Gradle forum about this.

此外,在 Gradle 论坛中有一个线程(是否有一个相当于“mvn dependency:analyze”的 Gradle )。

回答by jstricker

I've had a lot of luck using the Gradle Dependency Analysis Plugin. To get started with it, add the following two things to your Gradle build script.

我在使用Gradle Dependency Analysis Plugin 时很幸运。要开始使用它,请将以下两件事添加到您的 Gradle 构建脚本中。

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "com.github.nullstress:DependencyAnalysisPlugin:1.0.3"
    }
}

and

apply plugin: "dependencyAnalysis"

Once those are in place, run gradle analyze. If there are unused dependencies, you'll get a build failure that shows output similar to the text below, plus a list of the unused dependencies (both declared and transitive). The build failure is really handy if you want to enforce that there should be no unused dependencies via a CI build.

一旦这些就位,运行gradle analyze。如果存在未使用的依赖项,您将收到一个构建失败,显示类似于以下文本的输出,以及未使用的依赖项列表(声明的和可传递的)。如果您想通过 CI 构建强制不存在未使用的依赖项,构建失败非常方便。

:foo:analyze FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':foo:analyze'.
> The project has unused declared artifacts

回答by Subhash Chandran

The project mentioned in the earlier answers seem to be dead. I use gradle-dependency-analyze. Setup is simple:

前面答案中提到的项目似乎已经死了。我使用gradle-dependency-analyze。设置很简单:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'ca.cutterslade.gradle:gradle-dependency-analyze:1.0.3'
  }
}

apply plugin: 'ca.cutterslade.analyze'

Then do:

然后做:

$ gradle analyzeDependencies

回答by Brandon McKenzie

The projects on most of the historical answers are dead, but gradle-dependency-analyzeappears to be alive as of 2016-05-30.

大多数历史答案的项目都已死,但截至 2016 年 5 月 30日,gradle-dependency-analyze似乎还活着。

回答by SkyWalker

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

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

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

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



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

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

Gradle Lint Plugin: Full Documentation

Gradle Lint 插件:完整文档

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.

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

This plugin has various rules. Unused Dependency Ruleis 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.
  1. 删除未使用的依赖项。
  2. 将代码直接使用的传递依赖项提升为显式一阶依赖项。
  3. 将依赖项重新定位到“正确”配置。

To apply the rule, add:

要应用规则,请添加:

gradleLint.rules += 'unused-dependency'

Details of Unused Dependency Ruleis given in the last part.

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

To apply the Gradle lint plugin:

要应用 Gradle lint 插件:

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:

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

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.

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

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

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

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




Details of Unused Dependency Ruleis given in this part

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

To apply the rule, add:

要应用规则,请添加:

gradleLint.rules += 'unused-dependency'

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

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

Specifically, the rule makes the following adjustments to dependencies:

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

1. Removes unused dependencies

1. 移除未使用的依赖

  • Family-style jars like com.amazonaws:aws-java-sdk are removed, as they don't contain any code
  • 像 com.amazonaws:aws-java-sdk 这样的家庭风格的 jars 被删除,因为它们不包含任何代码

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

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

  • 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
  • 这具有将系列样式 JAR 文件(例如 com.amazonaws:aws-java-sdk)分解为您实际使用的部分并将其添加为一阶依赖项的副作用

3. Relocates dependencies to the 'correct' configuration

3. 将依赖重定位到“正确”的配置

  • Webjars are moved to the runtime configuration
  • JAR files that don't contain any classes andcontent 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).
  • Webjar 被移动到运行时配置
  • 不包含META-INF 之外的任何类内容的JAR 文件被移动到运行时
  • 'xerces'、'xercesImpl'、'xml-apis' 应该总是运行时范围
  • 如果没有任何可证明的编译时引用,服务提供者(包含 META-INF/服务的 JAR 文件)如 mysql-connector-java 将移至运行时
  • 依赖项被移动到可能的最高源集配置。例如,'junit' 被重新定位到 testCompile,除非在主源集中(很少见)对它有显式依赖。




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
  1. 找到未使用的依赖项、声明和传递的 Gradle 插件是com.github.nullstress.dependency-analysis

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

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

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

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

回答by sschrass

I just learned about this one: https://plugins.gradle.org/plugin/com.autonomousapps.dependency-analysis

我刚刚了解了这个:https: //plugins.gradle.org/plugin/com.autonomousapps.dependency-analysis

Github

GitHub

From the looks it is under active development, but I haven't tested it yet.

从外观上看,它正在积极开发中,但我还没有对其进行测试。

Edit: Actually its pretty awesome, it provides lots of advises (e.g. whether to use api vs implementation)

编辑:实际上它非常棒,它提供了很多建议(例如是否使用 api 与实现)