教程

如何通俗地理解 Gradle?
官方文档为主,翻译,可能版本较低,有些可借鉴

安装

国内镜像下载
安装参考官方文档(选择installing manually):installing Gradle

升级

官方文档指引:Upgrading your build from Gradle

Gradle搭建SpringBoot

  • gradle官方文档关于搭建
  • Spring官方文档关于搭建 (较复杂)

    plugin

    Spring Boot Gradle Plugin java -jar方式运行Springboot项目

    init.gradle

    阿里云云效
    Gradle 仓库及插件依赖代理镜像加速设置

    1. allprojects {
    2. ext.ALIYUN_REPO_URL = 'https://maven.aliyun.com/nexus/content/groups/public/'
    3. ext.DEEPDRAW_REPO_URL = 'http://116.62.122.39:8088/repository/maven-releases/'
    4. ext.DEEPDRAW_REPO_ACCOUNT = 'lidebing'
    5. ext.DEEPDRAW_REPO_PWD = 'e%TF5Z&vifh$WPuN'
    6. repositories {
    7. all { ArtifactRepository repo ->
    8. if (repo instanceof MavenArtifactRepository) {
    9. def url = repo.url.toString()
    10. if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
    11. project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPO_URL."
    12. remove repo
    13. }
    14. }
    15. }
    16. maven {
    17. url ALIYUN_REPO_URL
    18. }
    19. maven {
    20. allowInsecureProtocol = true
    21. url DEEPDRAW_REPO_URL
    22. credentials {
    23. username DEEPDRAW_REPO_ACCOUNT
    24. password DEEPDRAW_REPO_PWD
    25. }
    26. }
    27. }
    28. }

    jar和bootJar

    jar和bootJar,bootJar可以java -jar执行

    gradle cache

    image.png

    gradlew

    gradle和gradlew的区别

    build.gradle

    管理依赖 dependency management

    api与implementation的区别

  • A-B-C在全部远程依赖模式下,无论是api还是implemention都起不到依赖隔离的作用

  • A-B是本地依赖,使用implemention,A访问不到C的API
    • 如果你是一个lib库的维护者,对于所有需要公开的 API 你应该使用 api 依赖配置,测试依赖或不让最终用户使用的依赖使用 implementation 依赖配置。

Java project

  • compileOnly (supersedes provided) — for dependencies that are necessary to compile your production code but shouldn’t be part of the runtime classpath
  • implementation (supersedes compile) — used for compilation and runtime (maven )
  • runtimeOnly (supersedes runtime) — only used at runtime, not for compilation
  • testCompileOnly — same as compileOnly except it’s for the tests
  • testImplementation — test equivalent of implementation
  • testRuntimeOnly — test equivalent of runtimeOnly

Java plugin

  • implementation

The dependencies required to compile the production source of the project which are not part of the API exposed by the project. For example the project uses Hibernate for its internal persistence layer implementation.

  • api

The dependencies required to compile the production source of the project which are part of the API exposed by the project. For example the project uses Guava and exposes public interfaces with Guava classes in their method signatures.

  • testImplementation

The dependencies required to compile and run the test source of the project. For example the project decided to write test code with the test framework JUnit.

War plugin

  • providedCompile

This configuration should be used for dependencies required at compilation but which are provided by the environment in which the WAR is deployed. Dependencies declared here are thus visible to the main and test compilation classpaths.

  • providedRuntime

This configuration should be used for dependencies required at runtime but which are provided by the environment in which the WAR is deployed. Dependencies declared here are only visible to the main and test runtime classpaths.

排除依赖

  1. // dubbo和io.lettuce引入的netty依赖冲突,故排除
  2. all*.exclude group: 'io.netty', module: 'netty-all'

导入本地jar包

  1. implementation fileTree('webapp/WEB-INF/lib').include('*.jar')

问题解决

无效的源发行版

多模块设置gradle location

每个模块都要设置一下
image.png