运行

  1. 配置
1
2
3
  apply plugin: 'application'

  mainClassName = 'hello.HelloWorld'
  1. gradlew run

打包

  • 配置

    1
    2
    3
    4
    
      jar {
    baseName = 'gs-gradle'
    version =  '0.1.0'
      }
  • gradlw build

依赖

1
2
3
4
  dependencies {
compile "joda-time:joda-time:2.2"
testCompile "junit:junit:4.12"
  }

版本

1
2
  sourceCompatibility = 1.8
  targetCompatibility = 1.8

命令

gradlew tasks

gradlew build

  • 配置

    1
    
      apply plugin: 'java'

gradlew run

gradle wrapper

添加 gradlew 文件

1
   gradle wrapper --gradle-version 2.13

buildscrip 代码块

https://stackoverflow.com/questions/13923766/gradle-buildscript-dependencies

  • 解说

    • buildscript 内部的代码,只应用于 build script 本身(即: build.gradle)

      • 也就是,相对于整个项目无效
    • 而 root 代码部份,应用于整个项目
  • buildscrip 代码内容

    1
    2
    3
    4
    5
    
      buildScript {
    repositories {
       mavenCentral();
    }
      }
  • root 代码内容

    1
    2
    3
    
      repositories {
    mavenCentral();
      }

ext 代码块

https://stackoverflow.com/questions/48781976/ext-and-project-ext-have-same-scope-in-gradle

https://stackoverflow.com/questions/21696534/ext-and-code-blocks-meaning-in-the-gradle-file ext 等价于 project.ext

  • 用法

    • ext {…}
  • 作用

    • 用来添加 extra user-defined properties 额外的用户自定义属性

      1
      2
      3
      
      ext {
      compileSdkVersion = 25
      }
      • 解释
  • 这里相当于,定义了 project.compileSdkVersion = 25

dependencies 之 classpath

https://stackoverflow.com/a/34295609

  • classpath 用来指定 build.gradle 本身的依赖

    • 多用于 buildSrc {…} block 代码块
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  buildscript {
     ext {
 springBootVersion = '1.5.8.RELEASE'
     }
     repositories {
 mavenCentral()
     }
     dependencies {
 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
     }
  }

配置文件

https://www.baeldung.com/gradle-build-settings-properties

  • 项目目录

      build.gradle
      gradle    
    wrapper
       gradle-wrapper.jar
       gradle-wrapper.properties
      gradlew
      gradlew.bat
      settings.gradle
      src
    main
       java  
    App.java
    test      
       java
    AppTest.java
    

build.gradle

  • 每个项目一个

    • 有子项目的,每个子项目也有一个

settings.gradle

  • 整个项目只有一个

    • 即使是 multi-project build, 也是
    • 即,有子项目,只有根项目拥有

      1
      2
      
      rootProject.name = 'water'
      include 'bluewhale', 'krill', 'tropicalFish'
      • include —> include(String… projectPaths)
  • 用于多项目的配置

    • 对于单项目只是可选配置

gradle.properties

  • 非必选配置文件
  • 位置

    • 项目根目录
    • GRADLE_USER_HOME

      • 即 gradle 下在依赖的存放位置,如 maven local repository
    • 使用 -Dgradle.user.home="/some/place" 指定的目录

      1
      2
      3
      4
      
      org.gradle.caching=(true,false)
      org.gradle.daemon=(true,false)
      org.gradle.parallel=(true,false)
      org.gradle.logging.level=(quiet,warn,lifecycle,info,debug)
  • 作用

    • 控制 gradle 本身的特性

gradle 加载配置文件的整体流程

  • gradle 启动过程

      It launches as a new JVM process
      It parses the gradle.properties file and configures Gradle accordingly
      Next, it creates a Settings instance for the build
      Then, it evaluates the settings.gradle file against the Settings object
      It creates a hierarchy of Projects, based on the configured Settings object
      Finally, it executes each build.gradle file against its project
    
  • 加载顺序

    1. gradle.properties
    2. settings.gradle
    3. build.properties

多模块项目

https://docs.gradle.org/current/userguide/multi_project_builds.html

包含子项目

  • settings.gradle

    1
    
      include 'api', 'shared', 'services:personService'
    • 多级子项目,格式

      • "upper_project:lower_project:…"
      • 以 “:” 冒号分割

父项目配置

allprojects 代码块

1
2
3
4
5
  allprojects {
repositories {
   jcenter() 
}
  }

subprojects 代码块

project(":your_sub_project") 代码块

运行任务 task

1
2
3
4
5
6
7
8
  > gradle -q :hello :krill:hello hello
  I'm water
  I'm krill
  - I depend on water
  - The weight of my species in summer is twice as heavy as all human beings.
  - I love to spend time in the arctic waters.
  I'm tropicalFish
  - I depend on water

任务,不加所属子项目

  • 运行使用项目的同名任务

    • 梯级匹配
  • 先匹配根项目
  • 再匹配子项目

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    > gradle distanceToIceberg
    
    > Task :bluewhale:distanceToIceberg
    20 nautical miles
    
    > Task :krill:distanceToIceberg
    5 nautical miles
    
    BUILD SUCCESSFUL in 0s
    2 actionable tasks: 2 executed

任务,给定所属项目

  • 格式

    • gradle :your_sub_project:your_task
1
2
3
4
5
6
7
8
  # 根项目的 hello task
  gradle -q :hello   

  # krill 项目的 hello task
  gradle -q :krill:hello

  # 梯级匹配全部
  gradle -q hello

同名任务执行顺序,配置文件

  • 例如执行任务

    • gradle -q action
  • 执行顺序,默认情况

    • 按路径 + 任务名,字母表排序
  • :comsumer:action
  • :producer:action

子项目之间的依赖

1
2
3
4
5
  project(':services:personService') {
dependencies {
   implementation project(':shared'), project(':api')
}
  }
  • 说明

    • 子项目 :services:personService,依赖子项目 :shared 和 :api

gradle 变量

1
2
3
4
5
  task action {
doLast {
   println("Consuming message: ${rootProject.producerMessage}")
}
  }
  • ${rootProject.producerMessage}

    • rootProject: 根项目配置
  • project: 当前项目配置

gradlew test 命令行

测试 特定函数

1
2
3
  .\gradlew test --tests "cn.matgene.TestIO.myTestFunction"

  .\gradlew test --tests "cn.matgene.TestClass"

gradle test 显式 单元测试本身的输出

https://stackoverflow.com/a/41107537

1
2
3
4
5
6
  test {
testLogging {
   outputs.upToDateWhen {false}
   showStandardStreams = true
}
  }