aboutsummaryrefslogtreecommitdiff
path: root/build.gradle.kts
blob: 421de38bff98691a7b4b33a451d2cc7887b8765a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2019 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import com.diffplug.gradle.spotless.SpotlessExtension
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
  alias(libs.plugins.kotlin.jvm) apply false
  alias(libs.plugins.ksp) apply false
  alias(libs.plugins.dokka) apply false
  alias(libs.plugins.spotless) apply false
  alias(libs.plugins.mavenPublish) apply false
  alias(libs.plugins.kotlinBinaryCompatibilityValidator)
}

allprojects {
  group = property("GROUP") as String
  version = property("VERSION_NAME") as String

  repositories {
    mavenCentral()
  }
}

subprojects {
  tasks.withType<KotlinCompile> {
    kotlinOptions {
      freeCompilerArgs += listOf("-opt-in=kotlin.RequiresOptIn")
    }
  }
  // Ensure "org.gradle.jvm.version" is set to "8" in Gradle metadata.
  tasks.withType<JavaCompile> {
    sourceCompatibility = JavaVersion.VERSION_1_8.toString()
    targetCompatibility = JavaVersion.VERSION_1_8.toString()
  }

  apply(plugin = "org.jetbrains.kotlin.jvm")
  if ("test" !in name && buildFile.exists()) {
    apply(plugin = "org.jetbrains.dokka")
    apply(plugin = "com.vanniktech.maven.publish")
    configure<KotlinProjectExtension> {
      explicitApi()
    }
    afterEvaluate {
      tasks.named<DokkaTask>("dokkaHtml") {
        val projectFolder = project.path.trim(':').replace(':', '-')
        outputDirectory.set(rootProject.rootDir.resolve("docs/1.x/$projectFolder"))
        dokkaSourceSets.configureEach {
          skipDeprecated.set(true)
        }
      }
    }
  }

  apply(plugin = "com.diffplug.spotless")
  configure<SpotlessExtension> {
    kotlin {
      target("**/*.kt")
      ktlint(libs.versions.ktlint.get()).editorConfigOverride(
        mapOf("ktlint_standard_filename" to "disabled"),
      )
      trimTrailingWhitespace()
      endWithNewline()

      licenseHeader(
        """
        |/*
        | * Copyright (C) ${'$'}YEAR Square, Inc.
        | *
        | * Licensed under the Apache License, Version 2.0 (the "License");
        | * you may not use this file except in compliance with the License.
        | * You may obtain a copy of the License at
        | *
        | * https://www.apache.org/licenses/LICENSE-2.0
        | *
        | * Unless required by applicable law or agreed to in writing, software
        | * distributed under the License is distributed on an "AS IS" BASIS,
        | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        | * See the License for the specific language governing permissions and
        | * limitations under the License.
        | */
        """.trimMargin()
      )
    }
  }

  // Copied from https://github.com/square/retrofit/blob/master/retrofit/build.gradle#L28.
  // Create a test task for each supported JDK.
  for (majorVersion in 8..18) {
    // Adoptium JDK 9 cannot extract on Linux or Mac OS.
    if (majorVersion == 9) continue
    // Started causing build failures in late 2022, e.g.:
    // https://github.com/square/kotlinpoet/actions/runs/3816320722/jobs/6531532305.
    if (majorVersion == 10) continue

    val jdkTest = tasks.register<Test>("testJdk$majorVersion") {
      val javaToolchains = project.extensions.getByType(JavaToolchainService::class)
      javaLauncher.set(javaToolchains.launcherFor {
        languageVersion.set(JavaLanguageVersion.of(majorVersion))
      })

      description = "Runs the test suite on JDK $majorVersion"
      group = LifecycleBasePlugin.VERIFICATION_GROUP

      // Copy inputs from normal Test task.
      val testTask = tasks.getByName<Test>("test")
      classpath = testTask.classpath
      testClassesDirs = testTask.testClassesDirs
    }
    tasks.named("check").configure {
      dependsOn(jdkTest)
    }
  }
}

apiValidation {
  nonPublicMarkers += "com.squareup.kotlinpoet.ExperimentalKotlinPoetApi"
  ignoredProjects += listOf(
    "interop", // Empty middle package
    "test-processor" // Test only
  )
}