From 0cf95f84cb2615dd3d500412bc369ae492353034 Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Thu, 10 Jan 2019 23:05:24 +0300 Subject: [PATCH] Link the 'api' configuration to 'apiElements' (KT-28355) As we introduced the 'api' configurations for Kotlin source sets, the Java plugin did not link these configurations to its 'apiElements', and those dependencies would only get published with the 'runtime' scope through the 'implementation' configuration. To fix this, manually specify that 'apiElements' extendsFrom 'api'. Issue #KT-28355 Fixed --- .../kotlin/gradle/KotlinGradlePluginIT.kt | 33 +++++++++++++++++++ .../kotlin/gradle/plugin/KotlinPlugin.kt | 12 +++++++ 2 files changed, 45 insertions(+) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KotlinGradlePluginIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KotlinGradlePluginIT.kt index 7c35c6e2fc2..472d43facf4 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KotlinGradlePluginIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KotlinGradlePluginIT.kt @@ -876,6 +876,39 @@ class KotlinGradleIT : BaseGradleIT() { } } + @Test + fun testKotlinJvmProjectPublishesKotlinApiDependenciesAsCompile() = + with(Project("simpleProject", GradleVersionRequired.AtLeast("4.4"))) { + setupWorkingDir() + gradleBuildScript().appendText( + "\n" + """ + dependencies { + api 'org.jetbrains.kotlin:kotlin-reflect' + } + apply plugin: 'maven-publish' + group "com.example" + version "1.0" + publishing { + repositories { maven { url file("${'$'}buildDir/repo").toURI() } } + publications { maven(MavenPublication) { from components.java } } + } + """.trimIndent() + ) + build("publish") { + assertSuccessful() + val pomText = projectDir.resolve("build/repo/com/example/simpleProject/1.0/simpleProject-1.0.pom").readText() + .replace("\\s+|\\n".toRegex(), "") + assertTrue { + pomText.contains( + "org.jetbrains.kotlin" + + "kotlin-reflect" + + "${defaultBuildOptions().kotlinVersion}" + + "compile" + ) + } + } + } + @Test fun testNoTaskConfigurationForcing() { val gradleVersionRequirement = GradleVersionRequired.AtLeast("4.9") diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt index 7ecfc58f003..2b74b13a0ce 100755 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinPlugin.kt @@ -454,6 +454,18 @@ internal abstract class AbstractKotlinPlugin( val kotlinSourceSet = project.kotlinExtension.sourceSets.maybeCreate(kotlinCompilation.defaultSourceSetName) kotlinCompilation.source(kotlinSourceSet) } + + // Since the 'java' plugin (as opposed to 'java-library') doesn't known anything about the 'api' configurations, + // add the API dependencies of the main compilation directly to the 'apiElements' configuration, so that the 'api' dependencies + // are properly published with the 'compile' scope (KT-28355): + project.whenEvaluated { + project.configurations.apply { + val apiElementsConfiguration = getByName(kotlinTarget.apiElementsConfigurationName) + val mainCompilation = kotlinTarget.compilations.getByName(KotlinCompilation.MAIN_COMPILATION_NAME) + val compilationApiConfiguration = getByName(mainCompilation.apiConfigurationName) + apiElementsConfiguration.extendsFrom(compilationApiConfiguration) + } + } } private fun configureAttributes(