From afd04d5ad301b7850625b106c0bdf7f4e18ed16b Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Wed, 30 Aug 2023 21:53:01 +0200 Subject: [PATCH] =?UTF-8?q?34=20=E2=80=93=20Properties=20and=20Providers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yaml | 3 +- 34_Properties_and_Providers/README.MD | 21 +++++++++++ .../java-plugins/build.gradle.kts | 3 ++ .../kotlin/my-java-application.gradle.kts | 30 ++++++++++++++++ .../org/example/ApplicationReportTask.kt | 36 +++++++++++++++++++ .../src/main/kotlin/org/example/MyEnum.kt | 4 +++ .../my-build-logic/settings.gradle.kts | 5 +++ .../my-project/app/build.gradle.kts | 8 +++++ .../src/main/java/org/example/app/App.java | 9 +++++ .../main/java/org/example/app/PrintUtil.java | 7 ++++ .../my-project/app/version.txt | 1 + .../my-project/settings.gradle.kts | 12 +++++++ README.MD | 4 +++ 13 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 34_Properties_and_Providers/README.MD create mode 100644 34_Properties_and_Providers/my-build-logic/java-plugins/build.gradle.kts create mode 100644 34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts create mode 100644 34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/org/example/ApplicationReportTask.kt create mode 100644 34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/org/example/MyEnum.kt create mode 100644 34_Properties_and_Providers/my-build-logic/settings.gradle.kts create mode 100644 34_Properties_and_Providers/my-project/app/build.gradle.kts create mode 100644 34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/App.java create mode 100644 34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/PrintUtil.java create mode 100644 34_Properties_and_Providers/my-project/app/version.txt create mode 100644 34_Properties_and_Providers/my-project/settings.gradle.kts diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 40e8a53..3eaf438 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -51,4 +51,5 @@ jobs: - run: "gradle run --warning-mode=fail -p 30_Security_Vulnerabilities/my-project" - run: "gradle run --warning-mode=fail -p 31_The_Module_Path/my-project" - run: "gradle build --warning-mode=fail -p 32_Artifact_Transforms/my-project" - - run: "gradle build --warning-mode=fail -p 33_Classpath_and_Module_Path_in_Testing/my-project" \ No newline at end of file + - run: "gradle build --warning-mode=fail -p 33_Classpath_and_Module_Path_in_Testing/my-project" + - run: "gradle build --warning-mode=fail -p 34_Properties_and_Providers/my-project" \ No newline at end of file diff --git a/34_Properties_and_Providers/README.MD b/34_Properties_and_Providers/README.MD new file mode 100644 index 0000000..742db7f --- /dev/null +++ b/34_Properties_and_Providers/README.MD @@ -0,0 +1,21 @@ +# Understanding Gradle #34 – Properties and Providers + +**👇 click thumbnail to watch video** + +[](https://www.youtube.com/watch?v=n8Tgr4aLB18&list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE) + +What are the Property and Provider concepts in Gradle and why were they introduced? + +## Explore this sample + +This sample is best explored in [IntelliJ IDEA](https://www.jetbrains.com/idea/download). +Open **`34_Properties_and_Providers/my-project`** in IDEA and confirm with _Trust Project_. + +## Further readings + +* [Lazy Configuration](https://docs.gradle.org/current/userguide/lazy_configuration.html) +* [Task Configuration using Properties](https://docs.gradle.org/current/userguide/custom_gradle_types.html#configuration_using_properties) + +## Need Gradle support? + +Contact me, if you need help with Gradle: [onepiece.Software](https://onepiece.software). diff --git a/34_Properties_and_Providers/my-build-logic/java-plugins/build.gradle.kts b/34_Properties_and_Providers/my-build-logic/java-plugins/build.gradle.kts new file mode 100644 index 0000000..bc0172f --- /dev/null +++ b/34_Properties_and_Providers/my-build-logic/java-plugins/build.gradle.kts @@ -0,0 +1,3 @@ +plugins { + `kotlin-dsl` +} diff --git a/34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts b/34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts new file mode 100644 index 0000000..74d7620 --- /dev/null +++ b/34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts @@ -0,0 +1,30 @@ +import org.example.ApplicationReportTask + +plugins { + id("application") +} + +tasks.named("assemble") // access existing task +tasks.register("checkAll") // register new task +tasks.withType().configureEach { } // configure all of a certain type + +layout.projectDirectory +layout.buildDirectory + +providers.environmentVariable("CI").orElse("false").get().toBoolean() +providers.gradleProperty("foo") // pass to build via -Pfoo=bar +providers.systemProperty("file.encoding") + +version = providers.fileContents(layout.projectDirectory.file("version.txt")) + .asText.get() + +val fileCollection: FileCollection = configurations.runtimeClasspath.get() +fileCollection.elements.map { it.first().asFile } + + +val main = application.mainClass.map { "$it-report.txt" } + +tasks.register("applicationReport") { + classesDir.set(tasks.compileJava.flatMap { it.destinationDirectory }) + reportFile.set(layout.buildDirectory.file(main)) +} diff --git a/34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/org/example/ApplicationReportTask.kt b/34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/org/example/ApplicationReportTask.kt new file mode 100644 index 0000000..e275e70 --- /dev/null +++ b/34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/org/example/ApplicationReportTask.kt @@ -0,0 +1,36 @@ +package org.example + +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.tasks.InputDirectory +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction + +abstract class ApplicationReportTask : DefaultTask() { + + //abstract val p1: Property + //abstract val p2: Property + //abstract val p3: Property + + //abstract val p4: SetProperty + //abstract val p5: ListProperty + //abstract val p6: MapProperty + + @get:InputDirectory + abstract val classesDir: DirectoryProperty + + @get:OutputFile + abstract val reportFile: RegularFileProperty + + @TaskAction + fun report() { + reportFile.get().asFile.writeText( + """ + Application report: + =================== + Class File Count: ${classesDir.get().asFileTree.count()} + """.trimIndent()) + } + +} diff --git a/34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/org/example/MyEnum.kt b/34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/org/example/MyEnum.kt new file mode 100644 index 0000000..9262371 --- /dev/null +++ b/34_Properties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/org/example/MyEnum.kt @@ -0,0 +1,4 @@ +package org.example + +enum class MyEnum { +} \ No newline at end of file diff --git a/34_Properties_and_Providers/my-build-logic/settings.gradle.kts b/34_Properties_and_Providers/my-build-logic/settings.gradle.kts new file mode 100644 index 0000000..90e7393 --- /dev/null +++ b/34_Properties_and_Providers/my-build-logic/settings.gradle.kts @@ -0,0 +1,5 @@ +dependencyResolutionManagement { + repositories.gradlePluginPortal() +} + +include("java-plugins") \ No newline at end of file diff --git a/34_Properties_and_Providers/my-project/app/build.gradle.kts b/34_Properties_and_Providers/my-project/app/build.gradle.kts new file mode 100644 index 0000000..b66f370 --- /dev/null +++ b/34_Properties_and_Providers/my-project/app/build.gradle.kts @@ -0,0 +1,8 @@ +plugins { + id("my-java-application") +} + +application { + // mainClassName = "org.example.app.App" + mainClass.set("org.example.app.App") +} \ No newline at end of file diff --git a/34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/App.java b/34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/App.java new file mode 100644 index 0000000..72813dd --- /dev/null +++ b/34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/App.java @@ -0,0 +1,9 @@ +package org.example.app; + +public class App { + + public static void main(String[] args) { + PrintUtil.print(); + } +} + diff --git a/34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/PrintUtil.java b/34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/PrintUtil.java new file mode 100644 index 0000000..33a1e92 --- /dev/null +++ b/34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/PrintUtil.java @@ -0,0 +1,7 @@ +package org.example.app; + +public class PrintUtil { + public static void print() { + System.out.println("\uD83E\uDDA5"); + } +} diff --git a/34_Properties_and_Providers/my-project/app/version.txt b/34_Properties_and_Providers/my-project/app/version.txt new file mode 100644 index 0000000..eda3ab3 --- /dev/null +++ b/34_Properties_and_Providers/my-project/app/version.txt @@ -0,0 +1 @@ +1.34 \ No newline at end of file diff --git a/34_Properties_and_Providers/my-project/settings.gradle.kts b/34_Properties_and_Providers/my-project/settings.gradle.kts new file mode 100644 index 0000000..c6cf00c --- /dev/null +++ b/34_Properties_and_Providers/my-project/settings.gradle.kts @@ -0,0 +1,12 @@ +// Locations of Gradle plugins +pluginManagement { + repositories.gradlePluginPortal() + includeBuild("../my-build-logic") +} + +// Location of other components +dependencyResolutionManagement { + repositories.mavenCentral() +} + +include("app") diff --git a/README.MD b/README.MD index f8554a3..f6371da 100644 --- a/README.MD +++ b/README.MD @@ -60,6 +60,10 @@ This gives you a general understanding of how things work in Gradle and enables 32. [Artifact Transforms](32_Artifact_Transforms) 33. [Classpath and Module Path in Testing](33_Classpath_and_Module_Path_in_Testing) +## More Fundamentals (Entries 34 - ??) + +34. [Properties and Providers](34_Properties_and_Providers) + ## Need Gradle support? Contact me, if you need help with Gradle: [onepiece.Software](http://onepiece.software).