From aaed6fb722832cd0e459737a78b5e764a4ce8966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Muller?= Date: Wed, 13 Mar 2024 14:09:02 +0100 Subject: [PATCH] Improve publishing configuration (#468) --- buildSrc/build.gradle.kts | 4 +- buildSrc/settings.gradle.kts | 19 ++++++ buildSrc/src/main/java/VersionConfig.kt | 5 -- .../gradle/PillarboxPublishingPlugin.kt | 60 +++++++++++++++++++ ...ssr.pillarbox.gradle.publishing.properties | 1 + gradle/libs.versions.toml | 1 + pillarbox-analytics/build.gradle.kts | 39 ++---------- pillarbox-core-business/build.gradle.kts | 38 ++---------- pillarbox-player/build.gradle.kts | 38 ++---------- pillarbox-ui/build.gradle.kts | 38 ++---------- 10 files changed, 107 insertions(+), 136 deletions(-) create mode 100644 buildSrc/settings.gradle.kts create mode 100644 buildSrc/src/main/java/ch/srgssr/pillarbox/gradle/PillarboxPublishingPlugin.kt create mode 100644 buildSrc/src/main/resources/META-INF/gradle-plugins/ch.srgssr.pillarbox.gradle.publishing.properties diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 2646a996a..fdb41beda 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -6,6 +6,6 @@ plugins { `kotlin-dsl` } -repositories { - mavenCentral() +dependencies { + api(libs.android.gradle.api) } diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts new file mode 100644 index 000000000..fc0b70257 --- /dev/null +++ b/buildSrc/settings.gradle.kts @@ -0,0 +1,19 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ + +dependencyResolutionManagement { + repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS + + repositories { + google() + mavenCentral() + } + + versionCatalogs { + create("libs") { + from(files("../gradle/libs.versions.toml")) + } + } +} diff --git a/buildSrc/src/main/java/VersionConfig.kt b/buildSrc/src/main/java/VersionConfig.kt index eda02fa56..12f4cff57 100644 --- a/buildSrc/src/main/java/VersionConfig.kt +++ b/buildSrc/src/main/java/VersionConfig.kt @@ -21,11 +21,6 @@ object VersionConfig { private val ENV_VERSION_NAME: String? = System.getenv("VERSION_NAME") private val versionOnlyRegex = "[0-9]+.[0-9].[0-9]".toRegex() - /** - * Maven artifact group - */ - const val GROUP = "ch.srgssr.pillarbox" - /** * Version name * diff --git a/buildSrc/src/main/java/ch/srgssr/pillarbox/gradle/PillarboxPublishingPlugin.kt b/buildSrc/src/main/java/ch/srgssr/pillarbox/gradle/PillarboxPublishingPlugin.kt new file mode 100644 index 000000000..843d656b3 --- /dev/null +++ b/buildSrc/src/main/java/ch/srgssr/pillarbox/gradle/PillarboxPublishingPlugin.kt @@ -0,0 +1,60 @@ +/* + * Copyright (c) SRG SSR. All rights reserved. + * License information is available from the LICENSE file. + */ +package ch.srgssr.pillarbox.gradle + +import VersionConfig +import com.android.build.api.dsl.LibraryExtension +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.publish.PublishingExtension +import org.gradle.api.publish.maven.MavenPublication +import org.gradle.kotlin.dsl.configure +import org.gradle.kotlin.dsl.get +import org.gradle.kotlin.dsl.register + +/** + * Custom Gradle plugin to manage publication of a Pillarbox's library modules. + */ +class PillarboxPublishingPlugin : Plugin { + override fun apply(target: Project) = with(target) { + pluginManager.apply("com.android.library") + pluginManager.apply("org.gradle.maven-publish") + + extensions.configure { + defaultConfig { + group = "ch.srgssr.pillarbox" + version = VersionConfig.versionName() + } + + publishing { + singleVariant("release") { + withSourcesJar() + withJavadocJar() + } + } + } + + extensions.configure { + publications { + register("gpr") { + afterEvaluate { + from(components["release"]) + } + } + } + + repositories { + maven { + name = "GitHubPackages" + url = uri("https://maven.pkg.github.com/SRGSSR/pillarbox-android") + credentials { + username = findProperty("gpr.user") as String? ?: System.getenv("USERNAME") + password = findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") + } + } + } + } + } +} diff --git a/buildSrc/src/main/resources/META-INF/gradle-plugins/ch.srgssr.pillarbox.gradle.publishing.properties b/buildSrc/src/main/resources/META-INF/gradle-plugins/ch.srgssr.pillarbox.gradle.publishing.properties new file mode 100644 index 000000000..ab5bd13e2 --- /dev/null +++ b/buildSrc/src/main/resources/META-INF/gradle-plugins/ch.srgssr.pillarbox.gradle.publishing.properties @@ -0,0 +1 @@ +implementation-class=ch.srgssr.pillarbox.gradle.PillarboxPublishingPlugin diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6d10b72e9..dddd66d6b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -41,6 +41,7 @@ turbine = "1.0.0" [libraries] accompanist-navigation-material = { module = "com.google.accompanist:accompanist-navigation-material", version.ref = "accompanist" } +android-gradle-api = { module = "com.android.tools.build:gradle-api", version.ref = "android-gradle-plugin" } androidx-activity = { module = "androidx.activity:activity", version.ref = "androidx-activity" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activity" } androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "androidx-activity" } diff --git a/pillarbox-analytics/build.gradle.kts b/pillarbox-analytics/build.gradle.kts index 0d1c8fd2a..53870b958 100644 --- a/pillarbox-analytics/build.gradle.kts +++ b/pillarbox-analytics/build.gradle.kts @@ -1,24 +1,25 @@ -import org.gradle.api.tasks.testing.logging.TestExceptionFormat - /* * Copyright (c) SRG SSR. All rights reserved. * License information is available from the LICENSE file. */ + +import ch.srgssr.pillarbox.gradle.PillarboxPublishingPlugin +import org.gradle.api.tasks.testing.logging.TestExceptionFormat + plugins { alias(libs.plugins.android.library) alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlinx.kover) - `maven-publish` } +apply() + android { namespace = "ch.srgssr.pillarbox.analytics" compileSdk = AppConfig.compileSdk defaultConfig { minSdk = AppConfig.minSdk - version = VersionConfig.versionName() - group = VersionConfig.GROUP buildConfigField("String", "BUILD_DATE", "\"${AppConfig.getBuildDate()}\"") buildConfigField("String", "VERSION_NAME", "\"${version}\"") @@ -47,12 +48,6 @@ android { buildConfig = true resValues = false } - publishing { - singleVariant("release") { - withSourcesJar() - withJavadocJar() - } - } testOptions { unitTests { isIncludeAndroidResources = true @@ -90,25 +85,3 @@ koverReport { } } } - -publishing { - - publications { - register("gpr") { - afterEvaluate { - from(components["release"]) - } - } - } - repositories { - maven { - name = "GitHubPackages" - url = uri("https://maven.pkg.github.com/SRGSSR/pillarbox-android") - credentials { - username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME") - password = - project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") - } - } - } -} diff --git a/pillarbox-core-business/build.gradle.kts b/pillarbox-core-business/build.gradle.kts index c306ae3ee..faeb3243e 100644 --- a/pillarbox-core-business/build.gradle.kts +++ b/pillarbox-core-business/build.gradle.kts @@ -1,25 +1,26 @@ -import org.gradle.api.tasks.testing.logging.TestExceptionFormat - /* * Copyright (c) SRG SSR. All rights reserved. * License information is available from the LICENSE file. */ + +import ch.srgssr.pillarbox.gradle.PillarboxPublishingPlugin +import org.gradle.api.tasks.testing.logging.TestExceptionFormat + plugins { alias(libs.plugins.android.library) alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.serialization) alias(libs.plugins.kotlinx.kover) - `maven-publish` } +apply() + android { namespace = "ch.srgssr.pillarbox.core.business" compileSdk = AppConfig.compileSdk defaultConfig { minSdk = AppConfig.minSdk - version = VersionConfig.versionName() - group = VersionConfig.GROUP testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles("consumer-rules.pro") @@ -42,12 +43,6 @@ android { buildConfig = true resValues = false } - publishing { - singleVariant("release") { - withSourcesJar() - withJavadocJar() - } - } testOptions { unitTests { isIncludeAndroidResources = true @@ -108,24 +103,3 @@ koverReport { } } } - -publishing { - publications { - register("gpr") { - afterEvaluate { - from(components["release"]) - } - } - } - repositories { - maven { - name = "GitHubPackages" - url = uri("https://maven.pkg.github.com/SRGSSR/pillarbox-android") - credentials { - username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME") - password = - project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") - } - } - } -} diff --git a/pillarbox-player/build.gradle.kts b/pillarbox-player/build.gradle.kts index 539edc6da..38472f71b 100644 --- a/pillarbox-player/build.gradle.kts +++ b/pillarbox-player/build.gradle.kts @@ -1,24 +1,25 @@ -import org.gradle.api.tasks.testing.logging.TestExceptionFormat - /* * Copyright (c) SRG SSR. All rights reserved. * License information is available from the LICENSE file. */ + +import ch.srgssr.pillarbox.gradle.PillarboxPublishingPlugin +import org.gradle.api.tasks.testing.logging.TestExceptionFormat + plugins { alias(libs.plugins.android.library) alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlinx.kover) - `maven-publish` } +apply() + android { namespace = "ch.srgssr.pillarbox.player" compileSdk = AppConfig.compileSdk defaultConfig { minSdk = AppConfig.minSdk - version = VersionConfig.versionName() - group = VersionConfig.GROUP testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" consumerProguardFile("consumer-rules.pro") @@ -44,12 +45,6 @@ android { buildConfig = true resValues = false } - publishing { - singleVariant("release") { - withSourcesJar() - withJavadocJar() - } - } // Mockk includes some licenses information, which may conflict with other license files. This block merges all licenses together. // Mockk excludes all licenses instead: @@ -118,24 +113,3 @@ koverReport { } } } - -publishing { - publications { - register("gpr") { - afterEvaluate { - from(components["release"]) - } - } - } - repositories { - maven { - name = "GitHubPackages" - url = uri("https://maven.pkg.github.com/SRGSSR/pillarbox-android") - credentials { - username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME") - password = - project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") - } - } - } -} diff --git a/pillarbox-ui/build.gradle.kts b/pillarbox-ui/build.gradle.kts index ef0b34450..95b1574ef 100644 --- a/pillarbox-ui/build.gradle.kts +++ b/pillarbox-ui/build.gradle.kts @@ -1,24 +1,25 @@ -import org.gradle.api.tasks.testing.logging.TestExceptionFormat - /* * Copyright (c) SRG SSR. All rights reserved. * License information is available from the LICENSE file. */ + +import ch.srgssr.pillarbox.gradle.PillarboxPublishingPlugin +import org.gradle.api.tasks.testing.logging.TestExceptionFormat + plugins { alias(libs.plugins.android.library) alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlinx.kover) - `maven-publish` } +apply() + android { namespace = "ch.srgssr.pillarbox.ui" compileSdk = AppConfig.compileSdk defaultConfig { minSdk = AppConfig.minSdk - version = VersionConfig.versionName() - group = VersionConfig.GROUP testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" consumerProguardFile("consumer-rules.pro") @@ -44,12 +45,6 @@ android { composeOptions { kotlinCompilerExtensionVersion = libs.versions.androidx.compose.compiler.get() } - publishing { - singleVariant("release") { - withSourcesJar() - withJavadocJar() - } - } } tasks.withType().configureEach { @@ -90,24 +85,3 @@ koverReport { } } } - -publishing { - publications { - register("gpr") { - afterEvaluate { - from(components["release"]) - } - } - } - repositories { - maven { - name = "GitHubPackages" - url = uri("https://maven.pkg.github.com/SRGSSR/pillarbox-android") - credentials { - username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME") - password = - project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") - } - } - } -}