forked from apollographql/apollo-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.gradle.kts
332 lines (285 loc) · 10 KB
/
build.gradle.kts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import okhttp3.Credentials.basic
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
project.apply {
from(rootProject.file("gradle/dependencies.gradle"))
}
}
ApiCompatibility.configure(rootProject)
subprojects {
apply {
from(rootProject.file("gradle/dependencies.gradle"))
}
plugins.withType(com.android.build.gradle.BasePlugin::class.java) {
(project.extensions.getByName("android") as com.android.build.gradle.BaseExtension).compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
plugins.withType(org.gradle.api.plugins.JavaPlugin::class.java) {
extensions.configure(JavaPluginExtension::class.java) {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
// Gradle forces 1.3.72 for the time being so compile against 1.3 stdlib for the time being
// See https://issuetracker.google.com/issues/166582569
apiVersion = "1.3"
jvmTarget = JavaVersion.VERSION_1_8.toString()
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
}
}
(project.extensions.findByName("kotlin")
as? org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension)?.run {
sourceSets.all {
languageSettings.apply {
apiVersion = "1.3"
this.useExperimentalAnnotation("kotlin.RequiresOptIn")
}
}
}
tasks.withType<Test> {
testLogging {
exceptionFormat = TestExceptionFormat.FULL
}
}
this.apply(plugin = "maven-publish")
this.apply(plugin = "signing")
repositories {
google()
mavenCentral()
jcenter {
content {
includeGroup("org.jetbrains.trove4j")
}
}
}
group = property("GROUP")!!
version = property("VERSION_NAME")!!
apply(plugin = "checkstyle")
extensions.findByType(CheckstyleExtension::class.java)!!.apply {
configFile = rootProject.file("checkstyle.xml")
configProperties = mapOf(
"checkstyle.cache.file" to rootProject.file("build/checkstyle.cache")
)
}
tasks.register("checkstyle", Checkstyle::class.java) {
source("src/main/java")
include("**/*.java")
classpath = files()
}
afterEvaluate {
tasks.findByName("check")?.dependsOn("checkstyle")
}
tasks.withType<Test>().configureEach {
systemProperty("updateTestFixtures", System.getProperty("updateTestFixtures"))
}
afterEvaluate {
configurePublishing()
}
}
fun Project.configurePublishing() {
val android = extensions.findByType(com.android.build.gradle.BaseExtension::class.java)
/**
* Javadoc
*/
var javadocTask = tasks.findByName("javadoc") as Javadoc?
var javadocJarTaskProvider: TaskProvider<org.gradle.jvm.tasks.Jar>? = null
if (javadocTask == null && android != null) {
// create the Android javadoc if needed
javadocTask = tasks.create("javadoc", Javadoc::class.java) {
source = android.sourceSets.getByName("main").java.getSourceFiles()
classpath += project.files(android.bootClasspath.joinToString(File.pathSeparator))
(android as? com.android.build.gradle.LibraryExtension)?.libraryVariants?.configureEach {
if (name != "release") {
return@configureEach
}
classpath += getCompileClasspath(null)
}
}
}
javadocJarTaskProvider = tasks.register("javadocJar", org.gradle.jvm.tasks.Jar::class.java) {
archiveClassifier.set("javadoc")
if (javadocTask != null) {
dependsOn(javadocTask)
from(javadocTask.destinationDir)
}
}
val javaPluginConvention = project.convention.findPlugin(JavaPluginConvention::class.java)
val sourcesJarTaskProvider = tasks.register("apolloSourcesJar", org.gradle.jvm.tasks.Jar::class.java) {
archiveClassifier.set("sources")
when {
javaPluginConvention != null && android == null -> {
from(javaPluginConvention.sourceSets.get("main").allSource)
}
android != null -> {
from(android.sourceSets.getByName("main").java.getSourceFiles())
}
}
}
tasks.withType(Javadoc::class.java) {
// TODO: fix the javadoc warnings
(options as StandardJavadocDocletOptions).addStringOption("Xdoclint:none", "-quiet")
}
tasks.withType(Jar::class.java) {
manifest {
attributes["Built-By"] = findProperty("POM_DEVELOPER_ID") as String?
attributes["Build-Jdk"] = "${System.getProperty("java.version")} (${System.getProperty("java.vendor")} ${System.getProperty("java.vm.version")})"
attributes["Created-By"] = "Gradle ${gradle.gradleVersion}"
attributes["Implementation-Title"] = findProperty("POM_NAME") as String?
attributes["Implementation-Version"] = findProperty("VERSION_NAME") as String?
}
}
configure<PublishingExtension> {
publications {
when {
plugins.hasPlugin("org.jetbrains.kotlin.multiplatform") -> {
withType<MavenPublication> {
// multiplatform doesn't add javadoc by default so add it here
artifact(javadocJarTaskProvider.get())
}
}
plugins.hasPlugin("java-gradle-plugin") -> {
// java-gradle-plugin doesn't add javadoc/sources by default so add it here
withType<MavenPublication> {
artifact(javadocJarTaskProvider.get())
artifact(sourcesJarTaskProvider.get())
}
}
else -> {
create<MavenPublication>("default") {
val javaComponent = components.findByName("java")
if (javaComponent != null) {
from(javaComponent)
} else if (android != null) {
afterEvaluate {
from(components.findByName("release"))
}
}
artifact(javadocJarTaskProvider.get())
artifact(sourcesJarTaskProvider.get())
pom {
artifactId = findProperty("POM_ARTIFACT_ID") as String?
}
}
}
}
withType<MavenPublication> {
setDefaultPomFields(this)
}
}
repositories {
maven {
name = "pluginTest"
url = uri("file://${rootProject.buildDir}/localMaven")
}
maven {
name = "ossSnapshots"
url = uri("https://oss.sonatype.org/content/repositories/snapshots/")
credentials {
username = System.getenv("SONATYPE_NEXUS_USERNAME")
password = System.getenv("SONATYPE_NEXUS_PASSWORD")
}
}
maven {
name = "ossStaging"
url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials {
username = System.getenv("SONATYPE_NEXUS_USERNAME")
password = System.getenv("SONATYPE_NEXUS_PASSWORD")
}
}
}
}
configure<SigningExtension> {
// GPG_PRIVATE_KEY should contain the armoured private key that starts with -----BEGIN PGP PRIVATE KEY BLOCK-----
// It can be obtained with gpg --armour --export-secret-keys KEY_ID
useInMemoryPgpKeys(System.getenv("GPG_PRIVATE_KEY"), System.getenv("GPG_PRIVATE_KEY_PASSWORD"))
val publicationsContainer = (extensions.get("publishing") as PublishingExtension).publications
sign(publicationsContainer)
}
tasks.withType<Sign> {
isEnabled = !System.getenv("GPG_PRIVATE_KEY").isNullOrBlank()
}
}
/**
* Set fields which are common to all project, either KMP or non-KMP
*/
fun Project.setDefaultPomFields(mavenPublication: MavenPublication) {
mavenPublication.groupId = findProperty("GROUP") as String?
mavenPublication.version = findProperty("VERSION_NAME") as String?
mavenPublication.pom {
name.set(findProperty("POM_NAME") as String?)
(findProperty("POM_PACKAGING") as String?)?.let {
// Do not overwrite packaging if set by the multiplatform plugin
packaging = it
}
description.set(findProperty("POM_DESCRIPTION") as String?)
url.set(findProperty("POM_URL") as String?)
scm {
url.set(findProperty("POM_SCM_URL") as String?)
connection.set(findProperty("POM_SCM_CONNECTION") as String?)
developerConnection.set(findProperty("POM_SCM_DEV_CONNECTION") as String?)
}
licenses {
license {
name.set(findProperty("POM_LICENCE_NAME") as String?)
url.set(findProperty("POM_LICENCE_URL") as String?)
}
}
developers {
developer {
id.set(findProperty("POM_DEVELOPER_ID") as String?)
name.set(findProperty("POM_DEVELOPER_NAME") as String?)
}
}
}
}
fun subprojectTasks(name: String): List<Task> {
return subprojects.flatMap { subproject ->
subproject.tasks.matching { it.name == name }
}
}
fun isTag(): Boolean {
val ref = System.getenv("GITHUB_REF")
return ref?.startsWith("refs/tags/") == true
}
fun isMain(): Boolean {
val eventName = System.getenv("GITHUB_EVENT_NAME")
val ref = System.getenv("GITHUB_REF")
return eventName == "push" && ref == "refs/heads/main"
}
tasks.register("publishSnapshotsIfNeeded") {
if (isMain()) {
project.logger.log(LogLevel.LIFECYCLE, "Deploying snapshot to OSS Snapshots...")
dependsOn(subprojectTasks("publishAllPublicationsToOssSnapshotsRepository"))
}
}
tasks.register("publishToOssStagingIfNeeded") {
if (isTag()) {
project.logger.log(LogLevel.LIFECYCLE, "Deploying release to OSS staging...")
dependsOn(subprojectTasks("publishAllPublicationsToOssStagingRepository"))
}
}
tasks.register("publishToGradlePortalIfNeeded") {
if (isTag()) {
project.logger.log(LogLevel.LIFECYCLE, "Deploying release to Gradle Portal...")
dependsOn(":apollo-gradle-plugin:publishPlugins")
}
}
tasks.register("sonatypeCloseAndReleaseRepository") {
doLast {
com.vanniktech.maven.publish.nexus.Nexus(
username = System.getenv("SONATYPE_NEXUS_USERNAME"),
password = System.getenv("SONATYPE_NEXUS_PASSWORD"),
baseUrl = "https://oss.sonatype.org/service/local/",
groupId = "com.apollographql"
).closeAndReleaseRepository()
}
}