Skip to content

Commit

Permalink
34 – Properties and Providers
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Aug 30, 2023
1 parent b7e0d5f commit afd04d5
Show file tree
Hide file tree
Showing 13 changed files with 142 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 @@ -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"
- 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"
21 changes: 21 additions & 0 deletions 34_Properties_and_Providers/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Understanding Gradle #34 – Properties and Providers

**👇 click thumbnail to watch video**

[<img src="https://onepiecesoftware.github.io/img/videos/34.png" width="640">](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).
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,30 @@
import org.example.ApplicationReportTask

plugins {
id("application")
}

tasks.named("assemble") // access existing task
tasks.register("checkAll") // register new task
tasks.withType<JavaCompile>().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<ApplicationReportTask>("applicationReport") {
classesDir.set(tasks.compileJava.flatMap { it.destinationDirectory })
reportFile.set(layout.buildDirectory.file(main))
}
Original file line number Diff line number Diff line change
@@ -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<String>
//abstract val p2: Property<Int>
//abstract val p3: Property<MyEnum>

//abstract val p4: SetProperty<String>
//abstract val p5: ListProperty<String>
//abstract val p6: MapProperty<String, String>

@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())
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.example

enum class MyEnum {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencyResolutionManagement {
repositories.gradlePluginPortal()
}

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

application {
// mainClassName = "org.example.app.App"
mainClass.set("org.example.app.App")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.app;

public class App {

public static void main(String[] args) {
PrintUtil.print();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.app;

public class PrintUtil {
public static void print() {
System.out.println("\uD83E\uDDA5");
}
}
1 change: 1 addition & 0 deletions 34_Properties_and_Providers/my-project/app/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.34
12 changes: 12 additions & 0 deletions 34_Properties_and_Providers/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")
4 changes: 4 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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).

0 comments on commit afd04d5

Please sign in to comment.