[Gradle] Getting a root project of root gradle after project loading

#KT-59863 Fixed
This commit is contained in:
Anton Prokhorov
2023-07-07 11:39:02 +02:00
committed by Space Team
parent e5f9eb5b24
commit 0e6930e7ef
8 changed files with 73 additions and 6 deletions
@@ -364,4 +364,14 @@ class MppCompositeBuildIT : KGPBaseTest() {
}
}
}
@GradleTest
@GradleTestVersions(minVersion = TestVersions.Gradle.G_7_0)
fun `test sample7`(gradleVersion: GradleVersion) {
val producer = project("mpp-composite-build/sample7-KT-59863-pluginManagement.includeBuild/producerBuild", gradleVersion)
project("mpp-composite-build/sample7-KT-59863-pluginManagement.includeBuild/consumerBuild", gradleVersion, defaultBuildOptions) {
settingsGradleKts.toFile().replaceText("<producer_path>", producer.projectPath.toUri().path)
build("projects")
}
}
}
@@ -0,0 +1,7 @@
pluginManagement {
includeBuild("<producer_path>")
}
plugins {
id("org.jetbrains.example.gradle.plugin")
}
@@ -0,0 +1,18 @@
plugins {
`java-gradle-plugin`
kotlin("jvm")
}
repositories {
mavenLocal()
mavenCentral()
}
gradlePlugin {
plugins {
create("exampleGradlePlugin") {
id = "org.jetbrains.example.gradle.plugin"
implementationClass = "ExampleGradlePlugin"
}
}
}
@@ -0,0 +1,12 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
import org.gradle.api.Plugin
import org.gradle.api.initialization.Settings
class ExampleGradlePlugin : Plugin<Settings> {
override fun apply(target: Settings) {
}
}
@@ -10,11 +10,11 @@ import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.google.gson.stream.JsonWriter
import org.gradle.api.Project
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.GradleKpmModule
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.currentBuildId
import org.jetbrains.kotlin.gradle.plugin.sources.KotlinDependencyScope
@@ -419,10 +419,12 @@ internal object GlobalProjectStructureMetadataStorage {
fun propertyName(buildName: String, projectPath: String) = "$propertyPrefix.$buildName.path.$projectPath"
fun registerProjectStructureMetadata(project: Project, metadataProvider: () -> KotlinProjectStructureMetadata) {
project.compositeBuildRootProject.extensions.extraProperties.set(
propertyName(project.currentBuildId().name, project.path),
{ metadataProvider().toJson() }
)
project.compositeBuildRootProject {
(it as ExtensionAware).extensions.extraProperties.set(
propertyName(project.currentBuildId().name, project.path),
{ metadataProvider().toJson() }
)
}
}
fun getProjectStructureMetadataProvidersFromAllGradleBuilds(project: Project): Map<ProjectPathWithBuildName, Lazy<KotlinProjectStructureMetadata?>> {
@@ -17,7 +17,13 @@ import kotlin.reflect.KClass
val Gradle.projectCacheDir
get() = startParameter.projectCacheDir ?: this.rootProject.projectDir.resolve(".gradle")
internal val Project.compositeBuildRootProject: Project get() = generateSequence(project.gradle) { it.parent }.last().rootProject
internal val Project.compositeBuildRootGradle: Gradle get() = generateSequence(project.gradle) { it.parent }.last()
internal val Project.compositeBuildRootProject: Project get() = compositeBuildRootGradle.rootProject
/**
* Run block function on root project of the root build in composite build only when a root project becomes available
*/
internal fun Project.compositeBuildRootProject(block: (Project) -> Unit) = compositeBuildRootGradle.rootProject(block)
internal fun <T : BuildService<P>, P : BuildServiceParameters> Gradle.registerClassLoaderScopedBuildService(
serviceClass: KClass<T>,