如何在gradle中找到\删除未使用的依赖关系 [英] How to find\remove unused dependencies in gradle

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

问题描述

我想在我的项目中找到未使用的依赖项。在gradle中有没有像maven这样的功能?

解决方案

UPDATE:28-06-2016:Android support to unused-依赖关系




2017年6月中,他们发布了 4.0.0版本
并重命名为root项目名称gradle-lint-plugin
nebula-lint -plugin。他们还将 Android支持添加到
unused-dependency







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




Gradle Lint插件是一种可插拔和可配置的linter
工具识别和报告
Gradle脚本和相关文件中滥用或贬低的模式。

插件有各种规则。 未使用的依赖规则就是其中之一。它具有3个特定的特性。
$ b $ ol

  • 删除未使用的依赖关系。
  • 促进直接使用的传递依赖关系通过您的代码
    显式的第一顺序依赖关系。
  • 将依赖关系重新定位到正确配置。

  • 要应用规则,请添加:

      gradleLint.rules + ='unused-dependency'

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

    lint插件:

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

    或者:

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

    apply plugin:'nebula.lint'
    code>

    定义您想要抵制的规则:

      gradleLint.rules = ['all-dependency'] //在这里添加尽可能多的规则,因为您希望

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

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

      allprojects {
    apply plugin:'nebula.lint'
    gradleLint.rules = ['all-dependency'] //在这里添加尽可能多的规则,如您所愿
    }









    未使用的依赖规则在这部分给出



    要应用规则,请添加:

      gradleLint.rules + ='unused-dependency'

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

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

    1)删除未使用依赖关系




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


      $ p $ 2 2)促进直接由代码使用的传递依赖性exp合法的第一次订单依赖关系




      • 这有助于打破像b $ b com.amazonaws这样的家庭式瓶装的副作用: aws-java-sdk放入你实际使用
        的部分,并将它们添加为一阶依赖关系。


        3)将依赖关系重新定位到正确配置




        • Webjars被移动到运行时配置

        • 不包含META-INF之外的类和内容的jar包被移动到运行时
          xerces'xercesImpl'xml-apis应该总是运行时有作用域
        • 如果没有可证明的
          编译,服务提供者(包含META-INF /服务的jar)就像
          mysql-connector-java一样被移动到运行时

        • 依赖关系将移至最高源集配置
          。例如,'junit'被重定位到testCompile,除非
          在主源集(罕见)中存在对它的显式依赖关系。








        UPDATE:上一个插件



        我想分享以前的插件


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

        最新版本1.0.3是2014年12月23日创建的。之后,没有更新。


        注意:我们的许多工程师对这个插件,因为它们的
        只更新了版本号。



        I wanted to find unused dependencies in my project. Is there any 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 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 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 3 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 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 contain no 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 jars 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
        • Jars that contain no classes AND content outside of META-INF are moved to runtime
        • 'xerces', 'xercesImpl', 'xml-apis' should always be runtime scoped
        • Service providers (jars containing META-INF/services) like mysql-connector-java are moved to runtime if there is no 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. Gradle plugin that finds unused dependencies, declared and transitive is com.github.nullstress.dependency-analysis

        But it's latest version 1.0.3 is created 23 December 2014. After that there is no update.

        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天全站免登陆