|
3 | 3 | * Use of this source code is governed by the MIT license that can be found in the LICENSE file. |
4 | 4 | */ |
5 | 5 |
|
| 6 | +@file:Suppress("UnstableApiUsage") |
6 | 7 |
|
7 | | -// okhttp3 added for publishing to the Sonatype Central Repository: |
8 | 8 | import okhttp3.MultipartBody |
9 | 9 | import okhttp3.OkHttpClient |
10 | 10 | import okhttp3.Request |
11 | 11 | import okhttp3.RequestBody.Companion.asRequestBody |
12 | | -import java.util.* |
| 12 | +import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension |
| 13 | +import org.jetbrains.kotlin.gradle.dsl.KotlinVersion |
| 14 | +import org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin |
| 15 | +import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask |
| 16 | +import java.util.Base64 |
| 17 | +import java.util.Properties |
13 | 18 |
|
14 | | -buildscript { |
15 | | - dependencies { |
16 | | - classpath("com.squareup.okhttp3:okhttp:4.12.0") |
17 | | - } |
18 | | -} |
| 19 | +buildscript { dependencies { classpath(libs.okhttp) } } |
19 | 20 |
|
20 | 21 | plugins { |
21 | | - // this is necessary to avoid the plugins to be loaded multiple times |
22 | | - // in each subproject's classloader |
23 | | - kotlin("multiplatform").apply(false) |
24 | | - kotlin("plugin.compose").apply(false) |
25 | | - kotlin("jvm").apply(false) |
26 | | - id("org.jetbrains.compose").apply(false) |
27 | | - |
28 | | - kotlin("android").apply(false) |
29 | | - id("com.android.application").apply(false) |
30 | | - id("com.android.library").apply(false) |
31 | | - |
32 | | - id("io.codearte.nexus-staging").apply(false) |
33 | | - id("io.github.gradle-nexus.publish-plugin") |
34 | | -} |
35 | | - |
36 | | -val localProps = Properties() |
37 | | -if (project.file("local.properties").exists()) { |
38 | | - localProps.load(project.file("local.properties").inputStream()) |
39 | | -} else { |
40 | | - error("Couldn't read local.properties") |
| 22 | + alias(libs.plugins.kotlin.multiplatform) apply false |
| 23 | + alias(libs.plugins.kotlin.compose.compiler) apply false |
| 24 | + alias(libs.plugins.kotlin.jvm) apply false |
| 25 | + alias(libs.plugins.kotlin.android) apply false |
| 26 | + alias(libs.plugins.android.application) apply false |
| 27 | + alias(libs.plugins.android.library) apply false |
| 28 | + alias(libs.plugins.kotlin.multiplatform.android.library) apply false |
| 29 | + alias(libs.plugins.nexus.staging) apply false |
| 30 | + alias(libs.plugins.nexus.publish) |
41 | 31 | } |
42 | 32 |
|
43 | | -allprojects { |
44 | | - group = "org.jetbrains.lets-plot" |
45 | | - version = "3.0.3-SNAPSHOT" |
46 | | -// version = "0.0.0-SNAPSHOT" // for local publishing only |
| 33 | +// ============================= |
| 34 | +// Properties & Config |
| 35 | +// ============================= |
47 | 36 |
|
48 | | - tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all { |
49 | | - kotlinOptions { |
50 | | - jvmTarget = "11" |
51 | | - } |
| 37 | +val localProps = |
| 38 | + Properties().apply { |
| 39 | + val localPropertiesFile = rootProject.file("local.properties") |
| 40 | + if (localPropertiesFile.exists()) { |
| 41 | + load(localPropertiesFile.inputStream()) |
| 42 | + } |
52 | 43 | } |
53 | 44 |
|
54 | | - tasks.withType<JavaCompile>().all { |
55 | | - sourceCompatibility = "11" |
56 | | - targetCompatibility = "11" |
57 | | - } |
58 | | -} |
| 45 | +val javaVersion: String = libs.versions.java.get() |
| 46 | +val javaLanguageVersion: JavaLanguageVersion = JavaLanguageVersion.of(javaVersion) |
59 | 47 |
|
60 | | -// define the Maven Repository URL. Currently set to a local path for uploading |
61 | | -// artifacts to the Sonatype Central Repository. |
62 | | -val mavenReleasePublishUrl by extra { layout.buildDirectory.dir("maven/artifacts").get().toString() } |
63 | | -// define Maven Snapshot repository URL. |
64 | | -val mavenSnapshotPublishUrl by extra { "https://central.sonatype.com/repository/maven-snapshots/" } |
| 48 | +// ============================= |
| 49 | +// Maven Publishing Config |
| 50 | +// ============================= |
65 | 51 |
|
66 | | -// define Sonatype Central Repository settings: |
| 52 | +val mavenReleasePublishUrl by extra { |
| 53 | + layout.buildDirectory.dir("maven/artifacts").get().toString() |
| 54 | +} |
| 55 | +val mavenSnapshotPublishUrl by extra { "https://central.sonatype.com/repository/maven-snapshots/" } |
67 | 56 | val sonatypeUsername by extra { localProps["sonatype.username"] ?: "" } |
68 | 57 | val sonatypePassword by extra { localProps["sonatype.password"] ?: "" } |
69 | 58 |
|
70 | | -val packageMavenArtifacts by tasks.registering(Zip::class) { |
| 59 | +// ============================= |
| 60 | +// All Projects Config |
| 61 | +// ============================= |
71 | 62 |
|
72 | | - from(mavenReleasePublishUrl) |
73 | | - archiveFileName.set("${project.name}-artifacts.zip") |
74 | | - destinationDirectory.set(layout.buildDirectory) |
| 63 | +allprojects { |
| 64 | + group = "org.jetbrains.lets-plot" |
| 65 | + version = "3.0.3-SNAPSHOT" |
75 | 66 | } |
76 | | -val uploadMavenArtifacts by tasks.registering { |
77 | | - dependsOn(packageMavenArtifacts) |
78 | | - |
79 | | - doLast { |
80 | | - val uriBase = "https://central.sonatype.com/api/v1/publisher/upload" |
81 | | - val publishingType = "USER_MANAGED" |
82 | | - val deploymentName = "${project.name}-$version" |
83 | | - val uri = "$uriBase?name=$deploymentName&publishingType=$publishingType" |
84 | | - |
85 | | - val userName = sonatypeUsername as String |
86 | | - val password = sonatypePassword as String |
87 | | - val base64Auth = Base64.getEncoder().encode("$userName:$password".toByteArray()).toString(Charsets.UTF_8) |
88 | | - val bundleFile = packageMavenArtifacts.get().archiveFile.get().asFile |
89 | 67 |
|
90 | | - println("Sending request to $uri...") |
91 | | - |
92 | | - val client = OkHttpClient() |
93 | | - val request = Request.Builder() |
94 | | - .url(uri) |
95 | | - .header("Authorization", "Bearer $base64Auth") |
96 | | - .post( |
97 | | - MultipartBody.Builder() |
98 | | - .setType(MultipartBody.FORM) |
99 | | - .addFormDataPart("bundle", bundleFile.name, bundleFile.asRequestBody()) |
100 | | - .build() |
101 | | - ) |
102 | | - .build() |
103 | | - |
104 | | - client.newCall(request).execute().use { response -> |
105 | | - val statusCode = response.code |
106 | | - println("Upload status code: $statusCode") |
107 | | - println("Upload result: ${response.body!!.string()}") |
108 | | - if (statusCode != 201) { |
109 | | - error("Upload error to Central repository. Status code $statusCode.") |
110 | | - } |
111 | | - } |
| 68 | +// ============================= |
| 69 | +// Subprojects Config |
| 70 | +// ============================= |
| 71 | + |
| 72 | +subprojects { |
| 73 | + // Kotlin Configuration |
| 74 | + plugins.withType<KotlinBasePlugin> { |
| 75 | + extensions.configure<KotlinProjectExtension> { |
| 76 | + jvmToolchain { languageVersion.set(javaLanguageVersion) } |
112 | 77 | } |
| 78 | + |
| 79 | + tasks.withType<KotlinCompilationTask<*>>().configureEach { |
| 80 | + compilerOptions { |
| 81 | + apiVersion.set(KotlinVersion.KOTLIN_2_2) |
| 82 | + languageVersion.set(KotlinVersion.KOTLIN_2_2) |
| 83 | + allWarningsAsErrors.set(false) |
| 84 | + optIn.addAll("kotlin.RequiresOptIn", "kotlin.ExperimentalStdlibApi") |
| 85 | + freeCompilerArgs.addAll("-Xjsr305=strict") |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Java Configuration |
| 91 | + plugins.withType<JavaPlugin> { |
| 92 | + tasks.withType<JavaCompile>().configureEach { |
| 93 | + sourceCompatibility = JavaVersion.toVersion(javaVersion).majorVersion |
| 94 | + targetCompatibility = JavaVersion.toVersion(javaVersion).majorVersion |
| 95 | + |
| 96 | + options.apply { |
| 97 | + encoding = Charsets.UTF_8.name() |
| 98 | + isFork = true |
| 99 | + isIncremental = true |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Javadoc JAR for publishing |
| 105 | + val jarJavaDocs by |
| 106 | + tasks.registering(Jar::class) { |
| 107 | + archiveClassifier.set("javadoc") |
| 108 | + group = "publishing" |
| 109 | + from(rootProject.file("README.md")) |
| 110 | + } |
| 111 | + |
| 112 | + // Workaround for signing issue: https://github.com/gradle/gradle/issues/26091 |
| 113 | + tasks.withType<AbstractPublishToMaven>().configureEach { mustRunAfter(tasks.withType<Sign>()) } |
113 | 114 | } |
114 | 115 |
|
| 116 | +// ============================= |
| 117 | +// Publishing Tasks |
| 118 | +// ============================= |
| 119 | + |
| 120 | +val packageMavenArtifacts by |
| 121 | + tasks.registering(Zip::class) { |
| 122 | + group = "publishing" |
| 123 | + description = "Packages Maven artifacts for upload to Central Repository" |
| 124 | + from(mavenReleasePublishUrl) |
| 125 | + archiveFileName.set("${rootProject.name}-artifacts.zip") |
| 126 | + destinationDirectory.set(layout.buildDirectory) |
| 127 | + } |
115 | 128 |
|
116 | | -subprojects { |
117 | | - repositories { |
118 | | - mavenCentral() |
119 | | - google() |
120 | | - maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") |
121 | | - |
122 | | - // Repositories where other projects publish their artifacts locally to. |
123 | | - localProps["maven.repo.local"]?.let { |
124 | | - (it as String).split(",").forEach { repo -> |
125 | | - mavenLocal { |
126 | | - url = uri(repo) |
127 | | - } |
128 | | - } |
| 129 | +val uploadMavenArtifacts by |
| 130 | + tasks.registering { |
| 131 | + group = "publishing" |
| 132 | + description = "Uploads Maven artifacts to Sonatype Central Repository" |
| 133 | + dependsOn(packageMavenArtifacts) |
| 134 | + |
| 135 | + doLast { |
| 136 | + val uploadUrl = buildString { |
| 137 | + append("https://central.sonatype.com/api/v1/publisher/upload") |
| 138 | + append("?name=${rootProject.name}-$version") |
| 139 | + append("&publishingType=USER_MANAGED") |
129 | 140 | } |
130 | 141 |
|
131 | | - // SNAPSHOTS |
132 | | - maven(url = mavenSnapshotPublishUrl) |
| 142 | + val credentials = "$sonatypeUsername:$sonatypePassword" |
| 143 | + val base64Auth = Base64.getEncoder().encodeToString(credentials.toByteArray()) |
| 144 | + val bundleFile = packageMavenArtifacts.get().archiveFile.get().asFile |
133 | 145 |
|
134 | | - mavenLocal() |
135 | | - } |
| 146 | + logger.lifecycle("Uploading to: $uploadUrl") |
136 | 147 |
|
137 | | - val jarJavaDocs by tasks.creating(Jar::class) { |
138 | | - archiveClassifier.set("javadoc") |
139 | | - group = "lets plot" |
140 | | - from("$rootDir/README.md") |
141 | | - } |
| 148 | + val request = |
| 149 | + Request.Builder() |
| 150 | + .url(uploadUrl) |
| 151 | + .header("Authorization", "Bearer $base64Auth") |
| 152 | + .post( |
| 153 | + MultipartBody.Builder() |
| 154 | + .setType(MultipartBody.FORM) |
| 155 | + .addFormDataPart("bundle", bundleFile.name, bundleFile.asRequestBody()) |
| 156 | + .build() |
| 157 | + ) |
| 158 | + .build() |
142 | 159 |
|
143 | | - // ------------------------------------------ |
144 | | - // Workaround for the error when signing published artifacts. |
145 | | - // It seems to appear after switching to Gradle 8.3 |
146 | | - // For details see: https://github.com/gradle/gradle/issues/26091 : |
147 | | - // Publishing a KMP project with signing fails with "Task ... uses this output of task ... without declaring an explicit or implicit dependency" |
148 | | - // https://github.com/gradle/gradle/issues/26091 |
149 | | - tasks.withType<AbstractPublishToMaven>().configureEach { |
150 | | - val signingTasks = tasks.withType<Sign>() |
151 | | - mustRunAfter(signingTasks) |
| 160 | + OkHttpClient().newCall(request).execute().use { response -> |
| 161 | + val statusCode = response.code |
| 162 | + val responseBody = response.body?.string() ?: "" |
| 163 | + |
| 164 | + logger.lifecycle("Upload status: $statusCode") |
| 165 | + logger.lifecycle("Response: $responseBody") |
| 166 | + |
| 167 | + check(statusCode == 201) { "Upload failed with status $statusCode: $responseBody" } |
| 168 | + } |
| 169 | + } |
152 | 170 | } |
153 | | -} |
|
0 commit comments