Gradle 依赖指南

303 阅读2分钟

基础依赖

  1. org.springframework.boot:spring-boot-starter-parent:${version}
  • 引入此依赖后,其他依赖就不需要再指定依赖版本了,会自动引入最合适的依赖版本
  • ${version}与springboot版本保持一致
  1. org.springframework.boot:spring-boot-starter-web
  2. org.springframework.boot:spring-boot-starter-test
  3. org.projectlombok:lombok(可选,推荐)
  • 提供@Get @Set 等功能
  1. org.springframework.boot:spring-boot-devtools
  • 主要是支持热部署
  • 只引入此依赖无法生效,需要勾选preferences->Build, Execution, Deployment->compiler->build project automatically
  • 在springboot启动器中add run options,选择On Update action(代码变动时)/On frame deactivation(失去焦点时),选择更新classes and resources
  1. org.springframework.boot:spring-boot-configuration-processor
  • 支持自定义配置文件 => spring-configuration-metadata.json(配置spring的元数据)
  • 支持.properties、.yml文件
  • .yml比.properties先加载,因此.proerties的配置内容会覆盖.yml
  • 在项目中,尽可能的使用统一的配置文件类型

其他依赖

  1. cglib:cglib:3.3.0 Cglib动态代理
  2. com.alibaba:fastjson:1.2.58 Alibaba Fastjson
  3. com.aliyun:ecs20140526:3.1.12 Aliyun SDK
  4. com.aliyun:alibabacloud-dysmsapi20170525:2.0.23 Aliyun 短信SDK
  5. implementation 'redis.clients:jedis:4.4.3' jedis
  6. springboot继承redis
    • implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.1.4.RELEASE'
    • implementation 'org.springframework.data:spring-data-redis:2.1.4.RELEASE'

build.gradle配置说明

  1. dependencies中的各种用法
// compile: 为自己的项目模块添加依赖,该依赖会参与到编译构建过程,并且会打包到APK文件中,如果其它工程依赖本模块, 则会将依赖传递到其它工程中
// compileOnly: 依赖只会添加到编译路径中,不会打包到apk中,因此只能在编译时访问,且compileOnly修饰的依赖不会传递;只在编译过程中做一些辅助类的工作,在工程中没有使用该依赖库
// implementation: 为自己的项目模块添加依赖,该依赖会参与到编译构建过程,并且会打包到APK文件中,但该依赖不会传递给其他工程中
// api: 为自己的项目模块添加依赖,该依赖会参与到编译构建过程,并且会打包到APK文件中,但是这个依赖是可以传递的,比如模块A依赖模块B,B依赖库C,模块B在编译时能够访问到库C,但与implementation不同的是,在模块A中库C也是可以访问的
// runtimeOnly: 这个与compileOnly相反,它修饰的依赖不会添加到编译路径中,但是被打包到apk中,运行时使用。几乎不使用
// annotationProcessor: 该依赖用于设置注解处理器依赖, 在APT(Annotation Processing Tool)注解处理工具中使用该依赖