Skip to content

Commit 4d51bae

Browse files
committed
Introduce Java library Stream plugin
1 parent 13235a4 commit 4d51bae

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

plugin/build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ gradlePlugin {
4848
description = "Convention plugin for Stream Android application modules"
4949
tags = listOf("android", "application", "convention", "stream", "kotlin")
5050
}
51+
create("javaLibrary") {
52+
id = "io.getstream.java.library"
53+
implementationClass = "io.getstream.android.JavaLibraryConventionPlugin"
54+
displayName = "Stream Java Library Convention Plugin"
55+
description = "Convention plugin for Stream Java/Kotlin JVM library modules"
56+
tags = listOf("java", "library", "convention", "stream", "kotlin")
57+
}
5158
}
5259
}
5360

plugin/src/main/kotlin/io/getstream/android/StreamConventionPlugins.kt

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@ import com.android.build.api.dsl.LibraryExtension
2121
import org.gradle.api.JavaVersion
2222
import org.gradle.api.Plugin
2323
import org.gradle.api.Project
24+
import org.gradle.api.plugins.AppliedPlugin
2425
import org.gradle.api.tasks.compile.JavaCompile
26+
import org.gradle.api.tasks.testing.Test
2527
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
26-
import org.gradle.kotlin.dsl.configure
28+
import org.gradle.kotlin.dsl.getByType
2729
import org.gradle.kotlin.dsl.withType
2830
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
29-
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
31+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3032

3133
class AndroidApplicationConventionPlugin : Plugin<Project> {
3234
override fun apply(target: Project) {
3335
with(target) {
3436
pluginManager.apply("com.android.application")
3537

3638
configureAndroid<ApplicationExtension>()
39+
configureKotlin()
3740
}
3841
}
3942
}
@@ -44,6 +47,19 @@ class AndroidLibraryConventionPlugin : Plugin<Project> {
4447
pluginManager.apply("com.android.library")
4548

4649
configureAndroid<LibraryExtension>()
50+
configureKotlin()
51+
}
52+
}
53+
}
54+
55+
class JavaLibraryConventionPlugin : Plugin<Project> {
56+
override fun apply(target: Project) {
57+
with(target) {
58+
pluginManager.apply("java-library")
59+
pluginManager.apply("org.jetbrains.kotlin.jvm")
60+
61+
configureJava()
62+
configureKotlin()
4763
}
4864
}
4965
}
@@ -52,7 +68,7 @@ private val javaVersion = JavaVersion.VERSION_11
5268
private val jvmTargetVersion = JvmTarget.JVM_11
5369

5470
private inline fun <reified Ext : CommonExtension<*, *, *, *, *, *>> Project.configureAndroid() {
55-
val commonExtension = extensions.getByType(Ext::class.java)
71+
val commonExtension = extensions.getByType<Ext>()
5672

5773
commonExtension.apply {
5874
compileOptions {
@@ -64,15 +80,7 @@ private inline fun <reified Ext : CommonExtension<*, *, *, *, *, *>> Project.con
6480
unitTests {
6581
isIncludeAndroidResources = true
6682
isReturnDefaultValues = true
67-
all {
68-
it.testLogging {
69-
events("failed")
70-
showExceptions = true
71-
showCauses = true
72-
showStackTraces = true
73-
exceptionFormat = TestExceptionFormat.FULL
74-
}
75-
}
83+
all(Test::configureTestLogging)
7684
}
7785
}
7886
}
@@ -81,11 +89,34 @@ private inline fun <reified Ext : CommonExtension<*, *, *, *, *, *>> Project.con
8189
sourceCompatibility = javaVersion.toString()
8290
targetCompatibility = javaVersion.toString()
8391
}
92+
}
8493

85-
// Configure the Kotlin plugin if it is applied
86-
pluginManager.withPlugin("org.jetbrains.kotlin.android") {
87-
extensions.configure<KotlinAndroidProjectExtension> {
88-
compilerOptions { jvmTarget.set(jvmTargetVersion) }
89-
}
94+
private fun Project.configureJava() {
95+
tasks.withType<JavaCompile>().configureEach {
96+
sourceCompatibility = javaVersion.toString()
97+
targetCompatibility = javaVersion.toString()
9098
}
99+
100+
tasks.withType<Test>().configureEach(Test::configureTestLogging)
101+
}
102+
103+
private fun Test.configureTestLogging() = testLogging {
104+
events("failed")
105+
showExceptions = true
106+
showCauses = true
107+
showStackTraces = true
108+
exceptionFormat = TestExceptionFormat.FULL
109+
}
110+
111+
private fun Project.configureKotlin() {
112+
val configure =
113+
fun(_: AppliedPlugin) {
114+
tasks.withType<KotlinCompile>().configureEach {
115+
compilerOptions { jvmTarget.set(jvmTargetVersion) }
116+
}
117+
}
118+
119+
// Configure the Kotlin plugin that is applied, if any
120+
pluginManager.withPlugin("org.jetbrains.kotlin.android", configure)
121+
pluginManager.withPlugin("org.jetbrains.kotlin.jvm", configure)
91122
}

0 commit comments

Comments
 (0)