Kotlin Ͱॻ͘ Gradle Custom Tasks What we can do with Gradle Custom Tasks? ▸ Whatever you want! ▸ Access to Web APIs ▸ Git operations ▸ File I/O 9 shuuu-mai
Kotlin Ͱॻ͘ Gradle Custom Tasks What we can do with Gradle Custom Tasks? ▸ Whatever you want! ▸ Access to Web APIs ▸ Git operations ▸ File I/O 10 shuuu-mai = Automation!
Kotlin Ͱॻ͘ Gradle Custom Tasks Case Study: What we do with Gradle Custom Tasks 1. Keep the master up-to-date with the latest release branch ‣ Daily feature development: master ‣ Bug-fixes: release/* ‣ release/* may conflict with master… shuuu-mai 11 master release/*
Kotlin Ͱॻ͘ Gradle Custom Tasks Case Study: What we do with Gradle Custom Tasks 1. Keep the master up-to-date with the latest release branch ‣ Daily feature development: master ‣ Bug-fixes: release/* ‣ release/* may conflict with master… Resolve it as early as possible! shuuu-mai 12 master release/*
Kotlin Ͱॻ͘ Gradle Custom Tasks Declare a new Gradle Task ▸ In the build script: one-off task for a single build script ▸ In buildSrc ▸ Standalone project 14 shuuu-mai
Kotlin Ͱॻ͘ Gradle Custom Tasks Declare a new Gradle Task ▸ In the build script: one-off task for a single build script ▸ In buildSrc: Commonly-used task among multiple build scripts ▸ Standalone project 15 shuuu-mai
Kotlin Ͱॻ͘ Gradle Custom Tasks Declare a new Gradle Task ▸ In the build script: one-off task for a single build script ▸ In buildSrc: Commonly-used task among multiple build scripts ▸ Standalone project: Generates sharable jar artifact of Gradle Tasks 16 shuuu-mai
Kotlin Ͱॻ͘ Gradle Custom Tasks Gradle Custom Tasks: Getting Started ▸ Gradle Custom Task class ▸ Must be open class ▸ Extends DefaultTask ▸ Task function is annotated with @TaskAction 19 shuuu-mai
Kotlin Ͱॻ͘ Gradle Custom Tasks Gradle Custom Tasks: Options ▸ Gradle Custom Task class ▸ lateinit variable with @Input ▸ Get a CLI option from properties field in the project root build.gradle.kts 28 shuuu-mai
Kotlin Ͱॻ͘ Gradle Custom Tasks Case Study: Daily release branch merge into master 1. Workflow ‣ Pull all the updates ‣ Checkout a wokring branch for merging from a release branch ‣ Create a merge commit, push and open a pull request shuuu-mai 31
Kotlin Ͱॻ͘ Gradle Custom Tasks Case Study: Daily release branch merge into master 2. What we need ‣ GitHub API token ‣ A library to execute Git operations and GitHub API calls ‣ e.g. JGit for Git operations ‣ e.g. github-api for GitHub API calls ‣ e.g. jsemver for comparing semantic version shuuu-mai 32
Kotlin Ͱॻ͘ Gradle Custom Tasks Gradle Custom Tasks: Create a working branch open class DailyMergeTask : DefaultTask() { @Input lateinit var token: String @TaskAction fun merge() { // … // equivalent: $ git checkout -b ci/$version_to_master val version = releaseBranch.name.replace("refs/heads/release/", "") val workingBranch = "ci/$version_to_master" // Ext method to create a new branch (delete existing one if any) and checkout git.createAndCheckout(workingBranch) } } shuuu-mai
Kotlin Ͱॻ͘ Gradle Custom Tasks Gradle Custom Tasks: Check if any conflicts open class DailyMergeTask : DefaultTask() { @Input lateinit var token: String @TaskAction fun merge() { // … val status = mergeResult.mergeStatus if (!status.isSuccessful && status == CONFLICTING) { throw IllegalStateException(mergeResult.message) } } } shuuu-mai
Kotlin Ͱॻ͘ Gradle Custom Tasks Wrap up: General use of Gradle Tasks ▸ Automate the stuff you are doing in daily basis with Gradle Custom Tasks. ▸ A Gradle Custom Task can handle File and/or Network I/O. ▸ No need to consider some kind of AsyncTask. ▸ And… you can write unit tests on Gradle Custom Tasks. :) 43 shuuu-mai