Skip to content

Commit

Permalink
35 – Task Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Nov 13, 2023
1 parent e5c20e4 commit eaf32c4
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ jobs:
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 32_Artifact_Transforms/my-project"
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 33_Classpath_and_Module_Path_in_Testing/my-project"
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 34_Properties_and_Providers/my-project"
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 35_Working_with_Files/my-project"
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 35_Working_with_Files/my-project"
- run: "gradle run -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 36_Task_Actions/my-project"
21 changes: 21 additions & 0 deletions 36_Task_Actions/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Understanding Gradle #36 – Task Actions

**👇 click thumbnail to watch video**

[<img src="https://onepiecesoftware.github.io/img/videos/36.png" width="640">](https://www.youtube.com/watch?v=7ueAnM7ADm4&list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE)

Tweak what a task is doing by adding or modifying task actions.

## Explore this sample

This sample is best explored in [IntelliJ IDEA](https://www.jetbrains.com/idea/download).
Open **`36_Task_Actions/my-project`** in IDEA and confirm with _Trust Project_.

## Further readings

* [Configuring Tasks](https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:configuring_tasks)
* ['doFirst' / 'doLast' and Configuration Cache](https://docs.gradle.org/current/userguide/common_caching_problems.html#suggestions_for_authoring_your_build)

## Need Gradle support?

Contact me, if you need help with Gradle: [onepiece.Software](https://onepiece.software).
3 changes: 3 additions & 0 deletions 36_Task_Actions/my-build-logic/java-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
`kotlin-dsl`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import org.gradle.api.DefaultTask
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction

abstract class PrintVersion : DefaultTask() {

@get:Input
abstract val version: Property<String>

@TaskAction
fun print() {
println("Version: ${version.get()}")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id("application")
}

tasks.compileJava {
actions.clear() // Remove compile action from JavaCompile task
doFirst {
println("Number of task actions: " + actions.size)
}
doLast {
this as JavaCompile
Runtime.getRuntime().exec( // Add our own 'java compile' implementation
"javac ${source.asPath} -d ${destinationDirectory.get().asFile}").waitFor()
println("Compilation finished: ${inputs.sourceFiles.asPath}")
}
}

tasks.register<PrintVersion>("printVersion") {
version = project.version as String
}

tasks.register("printVersionDynamic") {
inputs.property("version", project.version)
doLast {
// Task action / execution time code
println("Version: ${inputs.properties["version"]}")
}
}
5 changes: 5 additions & 0 deletions 36_Task_Actions/my-build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencyResolutionManagement {
repositories.gradlePluginPortal()
}

include("java-plugins")
9 changes: 9 additions & 0 deletions 36_Task_Actions/my-project/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id("my-java-application")
}

version = "1.0"

application {
mainClass = "org.example.app.App"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.example.app;

public class App {

public static void main(String[] args) {
System.out.println("Hello World \uD83D\uDE80");
}
}
1 change: 1 addition & 0 deletions 36_Task_Actions/my-project/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.configuration-cache=true
12 changes: 12 additions & 0 deletions 36_Task_Actions/my-project/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Locations of Gradle plugins
pluginManagement {
repositories.gradlePluginPortal()
includeBuild("../my-build-logic")
}

// Location of other components
dependencyResolutionManagement {
repositories.mavenCentral()
}

include("app")
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ This gives you a general understanding of how things work in Gradle and enables

34. [Properties and Providers](34_Properties_and_Providers)
35. [Working with Files](35_Working_with_Files)
36. [Task Actions](36_Task_Actions)

## Need Gradle support?

Expand Down

0 comments on commit eaf32c4

Please sign in to comment.