diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 46362c088..337110d0f 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -5,4 +5,4 @@ plugins { repositories { mavenCentral() gradlePluginPortal() -} \ No newline at end of file +} diff --git a/buildSrc/src/main/kotlin/Predef.kt b/buildSrc/src/main/kotlin/Predef.kt new file mode 100644 index 000000000..f507d0b88 --- /dev/null +++ b/buildSrc/src/main/kotlin/Predef.kt @@ -0,0 +1,22 @@ +import org.gradle.api.Project + +internal fun Project.configValue(propertyName: String, environmentVariableName: String): String? { + val property: String? = project.properties[propertyName]?.toString() + val environmentVariable: String? = System.getenv(environmentVariableName) + + val configValue = property ?: environmentVariable + + return configValue.also { + if (configValue.isNullOrBlank()) { + errorMessage( + "$propertyName Gradle property and " + + "$environmentVariableName environment variable are missing", + ) + } + } +} + +internal fun Project.errorMessage(message: String) = logger.lifecycle("$YELLOW$message$RESET") + +private const val RESET = "\u001B[0m" +private const val YELLOW = "\u001B[0;33m" diff --git a/buildSrc/src/main/kotlin/ScalaDocumentationPlugin.kt b/buildSrc/src/main/kotlin/ScalaDocumentationPlugin.kt deleted file mode 100644 index 852653249..000000000 --- a/buildSrc/src/main/kotlin/ScalaDocumentationPlugin.kt +++ /dev/null @@ -1,15 +0,0 @@ -import org.gradle.api.Plugin -import org.gradle.api.Project -import org.gradle.api.tasks.bundling.Jar - -class ScalaDocumentationPlugin : Plugin { - override fun apply(project: Project) { - with(project) { - tasks.register("scaladocJar", Jar::class.java) { - archiveClassifier.set("javadoc") - dependsOn(tasks.named("scaladoc")) - from("${project.buildDir}/docs/scaladoc") - } - } - } -} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/ScalaPublishingConventionsPlugin.kt b/buildSrc/src/main/kotlin/ScalaPublishingConventionsPlugin.kt new file mode 100644 index 000000000..332db80d0 --- /dev/null +++ b/buildSrc/src/main/kotlin/ScalaPublishingConventionsPlugin.kt @@ -0,0 +1,86 @@ +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.plugins.BasePlugin +import org.gradle.api.plugins.BasePluginExtension +import org.gradle.api.publish.PublishingExtension +import org.gradle.api.publish.maven.MavenPublication +import org.gradle.api.tasks.TaskProvider +import org.gradle.api.tasks.bundling.Jar +import org.gradle.kotlin.dsl.findByType +import org.gradle.kotlin.dsl.get +import org.gradle.kotlin.dsl.register +import org.gradle.plugins.signing.SigningExtension + +class ScalaPublishingConventionsPlugin : Plugin { + override fun apply(project: Project): Unit = project.run { + val scaladocJarTask: TaskProvider = tasks.register("scaladocJar") { + group = BasePlugin.BUILD_GROUP + tasks.findByName("scaladoc")?.let { dependsOn(it) } + ?: errorMessage("The scaladoc task was not found. The Javadoc jar file won't contain any documentation") + archiveClassifier.set("javadoc") + from("$buildDir/docs/scaladoc") + } + + val publishingExtension: PublishingExtension = + extensions.findByType() + ?: throw IllegalStateException("The Maven Publish plugin is required to publish the build artifacts") + + val signingExtension: SigningExtension = + extensions.findByType() + ?: throw IllegalStateException("The Signing plugin is required to digitally sign the built artifacts") + + val basePluginExtension: BasePluginExtension = + extensions.findByType() + ?: throw IllegalStateException("The Base plugin is required to configure the name of artifacts") + + publishingExtension.run { + publications { + register("maven") { + val scala3Suffix = "_3" + + artifactId = basePluginExtension.archivesName.get() + scala3Suffix + from(components["java"]) + artifact(scaladocJarTask) + + pom { + name.set(project.properties["pom.name"]?.toString()) + description.set(project.properties["pom.description"]?.toString()) + url.set(project.properties["pom.url"]?.toString()) + + licenses { + license { + name.set(project.properties["pom.license.name"]?.toString()) + url.set(project.properties["pom.license.url"]?.toString()) + } + } + + developers { + developer { + id.set(project.properties["pom.developer.id"].toString()) + name.set(project.properties["pom.developer.name"].toString()) + } + } + + scm { + url.set(project.properties["pom.smc.url"].toString()) + connection.set(project.properties["pom.smc.connection"].toString()) + developerConnection.set(project.properties["pom.smc.developerConnection"].toString()) + } + } + } + } + } + + signingExtension.run { + val isLocal = gradle.startParameter.taskNames.any { it.contains("publishToMavenLocal", ignoreCase = true) } + val signingKeyId: String? = configValue("signing.keyId", "SIGNING_KEY_ID") + val signingKey: String? = configValue("signing.key", "SIGNING_KEY") + val signingPassphrase: String? = configValue("signing.passphrase", "SIGNING_KEY_PASSPHRASE") + + isRequired = !isLocal + useGpgCmd() + useInMemoryPgpKeys(signingKeyId, signingKey, signingPassphrase) + sign(publishingExtension.publications) + } + } +} diff --git a/buildSrc/src/main/kotlin/xef-scala-documentation.gradle.kts b/buildSrc/src/main/kotlin/xef-scala-documentation.gradle.kts deleted file mode 100644 index a04bb3fd1..000000000 --- a/buildSrc/src/main/kotlin/xef-scala-documentation.gradle.kts +++ /dev/null @@ -1 +0,0 @@ -apply() \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/xef-scala-publishing-conventions.gradle.kts b/buildSrc/src/main/kotlin/xef-scala-publishing-conventions.gradle.kts new file mode 100644 index 000000000..97fa1cfd8 --- /dev/null +++ b/buildSrc/src/main/kotlin/xef-scala-publishing-conventions.gradle.kts @@ -0,0 +1 @@ +apply() diff --git a/scala-cats/build.gradle.kts b/scala-cats/build.gradle.kts index 4983ad257..c829844d4 100644 --- a/scala-cats/build.gradle.kts +++ b/scala-cats/build.gradle.kts @@ -6,7 +6,7 @@ plugins { signing alias(libs.plugins.semver.gradle) alias(libs.plugins.spotless) - `xef-scala-documentation` + `xef-scala-publishing-conventions` } dependencies { @@ -18,10 +18,8 @@ dependencies { } java { - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 toolchain { - languageVersion = JavaLanguageVersion.of(11) + languageVersion.set(JavaLanguageVersion.of(11)) } withSourcesJar() } @@ -34,58 +32,8 @@ tasks.withType { scalaCompileOptions.additionalParameters = listOf("-Wunused:all", "-Wvalue-discard") } -publishing { - publications { - register("maven") { - val scala3Suffix = "_3" - from(components["java"]) - artifact(tasks.named("scaladocJar")) - - artifactId = base.archivesName.get() + scala3Suffix - - pom { - name.set(project.properties["pom.name"]?.toString()) - description.set(project.properties["pom.description"]?.toString()) - url.set(project.properties["pom.url"]?.toString()) - - licenses { - license { - name.set(project.properties["pom.license.name"]?.toString()) - url.set(project.properties["pom.license.url"]?.toString()) - } - } - - developers { - developer { - id.set(project.properties["pom.developer.id"].toString()) - name.set(project.properties["pom.developer.name"].toString()) - } - } - - scm { - url.set(project.properties["pom.smc.url"].toString()) - connection.set(project.properties["pom.smc.connection"].toString()) - developerConnection.set(project.properties["pom.smc.developerConnection"].toString()) - } - } - } - } -} - -signing { - val isLocal = gradle.startParameter.taskNames.any { it.contains("publishToMavenLocal", ignoreCase = true) } - val signingKeyId: String? = System.getenv("SIGNING_KEY_ID") - val signingKey: String? = System.getenv("SIGNING_KEY") - val signingPassphrase: String? = System.getenv("SIGNING_KEY_PASSPHRASE") - - isRequired = !isLocal - useGpgCmd() - useInMemoryPgpKeys(signingKeyId, signingKey, signingPassphrase) - sign(publishing.publications) -} - spotless { scala { scalafmt("3.7.3").configFile(".scalafmt.conf").scalaMajorVersion("2.13") } -} \ No newline at end of file +} diff --git a/scala/build.gradle.kts b/scala/build.gradle.kts index 435730250..45186b4ac 100644 --- a/scala/build.gradle.kts +++ b/scala/build.gradle.kts @@ -6,7 +6,7 @@ plugins { signing alias(libs.plugins.semver.gradle) alias(libs.plugins.spotless) - `xef-scala-documentation` + `xef-scala-publishing-conventions` } dependencies { @@ -40,58 +40,8 @@ tasks.withType { scalaCompileOptions.additionalParameters = listOf("-Wunused:all", "-Wvalue-discard") } -publishing { - publications { - register("maven") { - val scala3Suffix = "_3" - from(components["java"]) - artifact(tasks.named("scaladocJar")) - - artifactId = base.archivesName.get() + scala3Suffix - - pom { - name.set(project.properties["pom.name"]?.toString()) - description.set(project.properties["pom.description"]?.toString()) - url.set(project.properties["pom.url"]?.toString()) - - licenses { - license { - name.set(project.properties["pom.license.name"]?.toString()) - url.set(project.properties["pom.license.url"]?.toString()) - } - } - - developers { - developer { - id.set(project.properties["pom.developer.id"].toString()) - name.set(project.properties["pom.developer.name"].toString()) - } - } - - scm { - url.set(project.properties["pom.smc.url"].toString()) - connection.set(project.properties["pom.smc.connection"].toString()) - developerConnection.set(project.properties["pom.smc.developerConnection"].toString()) - } - } - } - } -} - -signing { - val isLocal = gradle.startParameter.taskNames.any { it.contains("publishToMavenLocal", ignoreCase = true) } - val signingKeyId: String? = System.getenv("SIGNING_KEY_ID") - val signingKey: String? = System.getenv("SIGNING_KEY") - val signingPassphrase: String? = System.getenv("SIGNING_KEY_PASSPHRASE") - - isRequired = !isLocal - useGpgCmd() - useInMemoryPgpKeys(signingKeyId, signingKey, signingPassphrase) - sign(publishing.publications) -} - spotless { scala { scalafmt("3.7.3").configFile(".scalafmt.conf").scalaMajorVersion("2.13") } -} \ No newline at end of file +}