🤗 ApiHug × {Postman|Swagger|Api...} = 快↑ 准√ 省↓
- GitHub - apihug/apihug.com: All abou the Apihug
- apihug.com: 有爱,有温度,有质量,有信任
- 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
包含如下:
- overridden-dependency-version
- unused-dependency
- unused-exclude-by-dep
- 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