Gradle

Gradle

Posted by nomadli on March 30, 2018

基本信息

  • 选Kotlin做配置语言,Groovy相对较弱
  • 缓存~/.gradle
  • 测试目录 项目目录/build/reports/tests/test/index.html
  • 库输出目录 项目目录/build/libs
  • 构建默认task顺序:compileJava processResources classes jar assemble compileTestJava processTestResources testClasses test check build
  • gradle 配置 项目目录/.gradle/gradle.properties
    javaHome=/Library/Java/JavaVirtualMachines/jdkxxx.jdk/Contents/Home
    targetJavaVersion=1.8
    
  • 项目配置 build.gradle.kts ```kotlin //java 项目需要java插件 plugins { java-library //java 项目必须, compileJava task 将编译 项目目录/src/main/java下面的所有文件 //compileTestJava task 将编译 项目目录/src/test/java //test task run from 项目目录/src/test/java //jar task pack class文件 和 项目目录/src/main/resources -> -.jar //javadoc from class 文件 `java-application` `cpp-application` `cpp-library` `maven-publish` //maven 仓库操作 }

//配置好环境一般不需要环境检查 require(hasProperty(“javaHome”)) { “Set ‘javaHome’ in project/.gradle/gradle.properties” } require(hasProperty(“targetJavaVersion”)) { “Set ‘targetJavaVersion’ in project/.gradle/gradle.properties” }

val javaHome: String by project val javaExecutablesPath = File(javaHome, “bin”) fun javaExecutable(execName: String): String { val executable = File(javaExecutablesPath, execName) require(executable.exists()) { “There is no ${execName} executable in ${javaExecutablesPath}” } return executable.toString() }

tasks.withType().configureEach { options.apply { isFork = true forkOptions.javaHome = file(javaHome) } } tasks.withType().configureEach { executable = javaExecutable("javadoc") } tasks.withType().configureEach { executable = javaExecutable("java") } tasks.withType().configureEach { executable = javaExecutable("java") } tasks.create("mycopy") {//创建一个Copy类型的名字为mycopy的任务 description = "Copies sources to the dest directory" group = "Custom"

from("src")
into("dest") }

tasks.create("zip") { description = "Archives sources in a zip file" group = "Archive"

from("src")
archiveFileName.set("basic-demo-1.0.zip") }

//设置编译及运行需要的版本 val targetJavaVersion: String by project java { //java plugins 中含有java-library才需要这个配置 sourceCompatibility = JavaVersion.VERSION_1_8 //设置java语言版本 //sourceCompatibility = JavaVersion.toVersion(targetJavaVersion) targetCompatibility = JavaVersion.VERSION_1_8 //设置jvm运行时最低版本 withJavadocJar() //生成包含doc的jar包 withSourcesJar() //生成包含源码的jar包 }

version = “1.2.1” //当前项目版本号

repositories { //指定使用的包管理服务及地址 mavenCentral() //maven公共仓库 jcenter() //bintray Artifactory存储库 }

dependencies { //依赖包设置 config(“::") //如implementation("org.hibernate:hibernate-core:3.6.7.Final") }

tasks.compileJava { //编译源码task options.isIncremental = true //增量编译 options.isFork = true //使用单独的jvm进程编译 options.isFailOnError = false //编译失败整个构建不要失败 }

tasks { //指定jar task manifest 内容 jar { manifest { attributes( mapOf(“Implementation-Title” to project.name, “Implementation-Version” to project.version) ) } } }

publishing { //将包发布 publications { create("myLibrary") { from(components["java"]) } }

repositories {
    maven {
        name = "myRepo"
        url = uri("file://${buildDir}/repo")
    }
} } ``` - 修改默认源码目录 ```kotlin sourceSets {
main {
    java {
        setSrcDirs(listOf("src"))   //将默认 项目目录项目目录/src/main/java 替换为 项目目录/src
    }
}

test {
    java {
        setSrcDirs(listOf("test")) //将默认 项目目录/src/test/java 替换为 项目目录/test
    }
} } ``` - 添加其它源码目录 ```kotlin sourceSets {
main {
    java {
        srcDir("thirdParty/src/main/java")
    }
} } ``` - dependencies config
- api 类似compile可能是公共缓存
- compileOnly 仅用于编译阶段,不用于运行时
- implementation取代compile 用于编译和运行时 compile会自动依赖传导导致版本冲突等,implementation需手动依赖所有 
- runtimeOnly取代runtime 仅在运行时使用,不用于编译
- testCompileOnly testImplementation testRuntimeOnly
- provided 只参与编译不参与打包, libC->(libA,libB) libB->libA 则libB使用provided libA
  • settings.gradle.kts
    • rootProject.name = “xx” //项目名称
    • include(“xxx”, “xx”) //包含子项目目录

基本使用

mkdir test
cd test
gradle init
select basic or application or library
select java
select Kotlin
select Junit
input project name
input com.nomadli.xxx
vim ./gradlew | ./settings.gradle.kts
ls -Rlsha
build.gradle.kts
gradle
    wrapper
        gradle-wrapper.jar
        gradle-wrapper.properties
gradlew | gradlew.bat
settings.gradle.kts
src
    main
        java
            com
                nomadli
                    ...
    test
        java
            com
                nomadli
                    ...
sh +x ./gradlew build #compileJava->processResources->classes->jar->assemble->compileTestJava->processTestResources->testClasses->test->check->build
sh +x ./gradlew jar #compileJava->processResources->classes->jar
sh +x ./gradlew javadoc #compileJava->processResources->javadoc out->build/docs/javadoc/index.html

语法

  • 打印顺序
    val cl = Action<Task> { println("I'm ${this.project.name}") }
    tasks.register("hello") { doLast(cl) }
    project(":bluewhale") {
      tasks.register("hello") { doLast(cl) }
    }
    
  • 打印顺序
    allprojects {
      tasks.register("hello") {
          doLast {
              println("I'm ${this.project.name}")
          }
      }
    }
    subprojects {
      tasks.named("hello") {
          doLast {
              println("- I depend on water")
          }
      }
    }
    project(":bluewhale").tasks.named("hello") {
      doLast {
          println("- I'm the largest animal that has ever lived on this planet.")
      }
    }