-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
3 changes: 3 additions & 0 deletions
3
34_Properties_and_Providers/my-build-logic/java-plugins/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
plugins { | ||
`kotlin-dsl` | ||
} |
30 changes: 30 additions & 0 deletions
30
..._and_Providers/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
36 changes: 36 additions & 0 deletions
36
...roviders/my-build-logic/java-plugins/src/main/kotlin/org/example/ApplicationReportTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
...roperties_and_Providers/my-build-logic/java-plugins/src/main/kotlin/org/example/MyEnum.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.example | ||
|
||
enum class MyEnum { | ||
} |
5 changes: 5 additions & 0 deletions
5
34_Properties_and_Providers/my-build-logic/settings.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
dependencyResolutionManagement { | ||
repositories.gradlePluginPortal() | ||
} | ||
|
||
include("java-plugins") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
9 changes: 9 additions & 0 deletions
9
34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
7 changes: 7 additions & 0 deletions
7
34_Properties_and_Providers/my-project/app/src/main/java/org/example/app/PrintUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.34 |
12 changes: 12 additions & 0 deletions
12
34_Properties_and_Providers/my-project/settings.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters