Gradle 实战 - 检查不用包 -ApiHug准备-工具篇-010

  🤗 ApiHug × {Postman|Swagger|Api...} = 快↑ 准√ 省↓

  1. GitHub - apihug/apihug.com: All abou the Apihug   
  2. apihug.com: 有爱,有温度,有质量,有信任
  3. ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace

ApiHug 整个工具链基于 Gradle, 使用 ApiHug 准备工作最先需要学习的就是 gradle. 工欲善其事,必先利其器

在开发过程中,引包一时爽,打包火葬场, 看到巨大无比的 jar 吧, 虽然磁盘空间很便宜,但是作为洁癖者,还是很不爽。

幸好有 gradle-lint-pluginopen in new window 可以帮你找出不需要的依赖。

#创建个项目


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

description = "all the gradle trivial example"

allprojects {
    
    apply plugin :"java"
    apply plugin :"nebula.lint"

    gradleLint {
        rules=['unused-dependency']
        reportFormat = 'text'
    }

}

这个在父目录配置, 整体 lint 检查, 然后测试模块 unused-dependencies 中配置如下依赖:


dependencies {
    // Guava is a suite of core and expanded libraries that include utility classes, 
    // Google's collections, I/O classes, and much more.
    implementation 'com.google.guava:guava:31.1-jre'

    //Apache HttpComponents Client
    implementation 'org.apache.httpcomponents:httpclient:4.5.13'

    testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
}

tasks.named('test') {
    useJUnitPlatform()
}

项目中使用如下:

private static void useGuava() {
    List<String> list = ImmutableList.of("dearxue", "is", "dear");
    System.out.println(list.stream()
        .collect(Collectors.joining(" ")));
}

private static void useHttpCore() {
    SSLContextBuilder.create();
}

触发 gradlew clean build 输出如下:


This project contains lint violations. A complete listing of the violations follows.
Because none were serious, the build's overall status was unaffected.

unused-dependency                  
one or more classes in org.apache.httpcomponents:httpcore:4.4.13 are required by your code directly (no auto-fix available)

warning   unused-dependency                  
this dependency is unused and can be removed 
unused-dependencies/build.gradle:10
implementation 'org.apache.httpcomponents:httpclient:4.5.13'

warning   unused-dependency                  
this dependency is unused and can be removed
unused-dependencies/build.gradle:12
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'

gradle-lint-plugin unused-dependency ruleopen in new window 有很多,不同情况下有不同的报告, all-dependency 包含如下:

  1. overridden-dependency-version
  2. unused-dependency
  3. unused-exclude-by-dep
  4. unused-exclude-by-conf

按照建议,可以运行 fixGradleLint 即可修复 -- 也就是将不用的依赖注拿掉。 Lint 带来的 tasks:


gradlew  tasks --all | grep Lint
autoLintGradle
criticalLintGradle
fixGradleLint
fixLintGradle
generateGradleLintReport
unused-dependencies:generateGradleLintReport

#几点问题

Transitive Dependencies(间接依赖) & 反射依赖, 无法很好探测。


import org.apache.http.ssl.SSLContextBuilder;

private static void useHttpCore() {
    SSLContextBuilder.create();
}

上面的例子已经报告 需要添加: org.apache.httpcomponents:httpcore:4.4.13 依赖,其实已经由 org.apache.httpcomponents:httpclient:4.5.13 依赖引入。

gradlew unused-dependencies:dependencies --configuration compileClasspath

Task :unused-dependencies:dependencies

------------------------------------------------------------
Project ':unused-dependencies' - Gradle Unused Dependencies example
------------------------------------------------------------

compileClasspath - Compile classpath for source set 'main'.
+--- com.google.guava:guava:31.1-jre
|    +--- com.google.guava:failureaccess:1.0.1
|    +--- com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava
|    +--- com.google.code.findbugs:jsr305:3.0.2
|    +--- org.checkerframework:checker-qual:3.12.0
|    +--- com.google.errorprone:error_prone_annotations:2.11.0
|    \--- com.google.j2objc:j2objc-annotations:1.3
\--- org.apache.httpcomponents:httpclient:4.5.13
     +--- org.apache.httpcomponents:httpcore:4.4.13
     +--- commons-logging:commons-logging:1.2
     \--- commons-codec:commons-codec:1.11

同时反射的内容也无法识别:


private static void useHttpClientWithReflection() {
    try {
        Class<?> httpBuilder = Class.forName("org.apache.http.impl.client.HttpClientBuilder");
        Method create = httpBuilder.getMethod("create", null);
        create.invoke(httpBuilder, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

其他冲突比如 annotation processor: Lombok 编译时自动生成 Getter/Setter 和构造器的工具, 也会导致 unused 报告。

剔除检查规则比如:

gradleLint.ignore('unused-dependency') {
    compileOnly group: 'org.projectlombok', name: 'lombok', version:'1.16.20'
}

#报告

gradlew generateGradleLintReport 命令, 导出内容我们上面看到差不多。

例子项目在: unused-dependencies Example

我们

api-hug-contact

相关推荐

  1. Gradle设置引用的JAR编译到APK中

    2024-04-13 09:14:04       30 阅读
  2. gradle中如何使用插件将依赖打进jar

    2024-04-13 09:14:04       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 09:14:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 09:14:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 09:14:04       82 阅读
  4. Python语言-面向对象

    2024-04-13 09:14:04       91 阅读

热门阅读

  1. Scala详解(2)

    2024-04-13 09:14:04       75 阅读
  2. 数据库DMP格式备份文件

    2024-04-13 09:14:04       122 阅读
  3. 3d max快捷键命令大全

    2024-04-13 09:14:04       41 阅读
  4. 用自动LOD简化3D网格【Babylon.js】

    2024-04-13 09:14:04       42 阅读
  5. Python如何使用不同的库来导出PDF和XLSX

    2024-04-13 09:14:04       42 阅读
  6. C语言--第二章之位运算符

    2024-04-13 09:14:04       34 阅读
  7. 熟练使用Nacos、GateWay、OpenFeign、Sentinel常用组件

    2024-04-13 09:14:04       44 阅读
  8. 微服务下使用sentinel进行服务熔断

    2024-04-13 09:14:04       41 阅读
  9. C++题目讲解目录

    2024-04-13 09:14:04       38 阅读
  10. QThread线程退出

    2024-04-13 09:14:04       46 阅读
  11. 图像处理-采样方法概述

    2024-04-13 09:14:04       31 阅读