From 134a2ddbebbf2d5391fc06cb56f01b8f7a55b042 Mon Sep 17 00:00:00 2001 From: Aurimas Liutikas Date: Thu, 21 Dec 2023 13:43:46 -0800 Subject: [PATCH] Clean up bundling of project(":core") Previous way was breaking IDE and making it super hard to debug Test: ./gradlew check --- buildlogic/build.gradle.kts | 30 +++++++++++++ buildlogic/settings.gradle.kts | 18 ++++++++ .../kotlin/androidx/build/BundlePlugin.kt | 45 +++++++++++++++++++ gcpbuildcache/build.gradle.kts | 8 ++-- s3buildcache/build.gradle.kts | 8 ++-- settings.gradle.kts | 2 + 6 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 buildlogic/build.gradle.kts create mode 100644 buildlogic/settings.gradle.kts create mode 100644 buildlogic/src/main/kotlin/androidx/build/BundlePlugin.kt diff --git a/buildlogic/build.gradle.kts b/buildlogic/build.gradle.kts new file mode 100644 index 0000000..7a7e1ab --- /dev/null +++ b/buildlogic/build.gradle.kts @@ -0,0 +1,30 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * 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 + * + * http://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. + * + */ + +plugins { + id("java-gradle-plugin") + alias(libs.plugins.kotlin.jvm) +} + +gradlePlugin { + plugins { + create("bundlePlugin") { + id = "bundle" + implementationClass = "androidx.build.BundlePlugin" + } + } +} \ No newline at end of file diff --git a/buildlogic/settings.gradle.kts b/buildlogic/settings.gradle.kts new file mode 100644 index 0000000..5f53798 --- /dev/null +++ b/buildlogic/settings.gradle.kts @@ -0,0 +1,18 @@ +dependencyResolutionManagement { + versionCatalogs { + create("libs") { + from(files("../gradle/libs.versions.toml")) + } + } + repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS + repositories { + mavenCentral() + } +} + +pluginManagement { + repositories { + mavenCentral() + gradlePluginPortal() + } +} diff --git a/buildlogic/src/main/kotlin/androidx/build/BundlePlugin.kt b/buildlogic/src/main/kotlin/androidx/build/BundlePlugin.kt new file mode 100644 index 0000000..0571f1c --- /dev/null +++ b/buildlogic/src/main/kotlin/androidx/build/BundlePlugin.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * 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 + * + * http://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. + * + */ + +package androidx.build + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.tasks.bundling.Jar +import org.gradle.plugin.devel.tasks.PluginUnderTestMetadata + +class BundlePlugin : Plugin { + override fun apply(project: Project) { + val bundleInside = project.configurations.create("bundleInside") { + it.isTransitive = false + it.isVisible = false + it.isCanBeResolved = true + } + project.afterEvaluate { + project.configurations.getByName("compileOnly").extendsFrom(bundleInside) + project.configurations.getByName("testImplementation").extendsFrom(bundleInside) + val jarsToBundle = bundleInside.incoming.artifactView { }.files + project.tasks.named("jar").configure { jarTask -> + jarTask as Jar + jarTask.from(project.provider { jarsToBundle.map { if (it.isDirectory) { it } else { project.zipTree(it) } } }) + } + project.tasks.withType(PluginUnderTestMetadata::class.java).configureEach { + it.pluginClasspath.from(jarsToBundle) + } + } + } +} \ No newline at end of file diff --git a/gcpbuildcache/build.gradle.kts b/gcpbuildcache/build.gradle.kts index 3ce8ce1..4adb0dd 100644 --- a/gcpbuildcache/build.gradle.kts +++ b/gcpbuildcache/build.gradle.kts @@ -18,16 +18,14 @@ plugins { id("maven-publish") id("signing") + id("bundle") alias(libs.plugins.gradle.publish) alias(libs.plugins.kotlin.jvm) } -// Bundle core library directly as we only get to publish one jar per plugin in Gradle Plugin Portal -sourceSets.main { - java.srcDir("../core/src/main/kotlin") -} - dependencies { + // Bundle core library directly as we only get to publish one jar per plugin in Gradle Plugin Portal + bundleInside(project(":core")) implementation(libs.google.cloud.storage) implementation(libs.retrofit.core) implementation(libs.retrofit.converter.gson) diff --git a/s3buildcache/build.gradle.kts b/s3buildcache/build.gradle.kts index 97e4aed..35b13f0 100644 --- a/s3buildcache/build.gradle.kts +++ b/s3buildcache/build.gradle.kts @@ -18,16 +18,14 @@ plugins { id("maven-publish") id("signing") + id("bundle") alias(libs.plugins.gradle.publish) alias(libs.plugins.kotlin.jvm) } -// Bundle core library directly as we only get to publish one jar per plugin in Gradle Plugin Portal -sourceSets.main { - java.srcDir("../core/src/main/kotlin") -} - dependencies { + // Bundle core library directly as we only get to publish one jar per plugin in Gradle Plugin Portal + bundleInside(project(":core")) implementation(libs.retrofit.core) implementation(libs.retrofit.converter.gson) implementation(platform(libs.amazon.bom)) diff --git a/settings.gradle.kts b/settings.gradle.kts index a91d756..2939f49 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -43,3 +43,5 @@ rootProject.name = "gcp-gradle-build-cache" include("core") include("gcpbuildcache") include("s3buildcache") + +includeBuild("buildlogic") \ No newline at end of file