Use .withDependencies { ... } to set up default versions with Gradle 4.4
+ Improve the test for omitted Kotlin module versions. Issues: #KT-21806 Fixed, #KT-21203 Fixed
This commit is contained in:
+10
-3
@@ -457,15 +457,22 @@ class KotlinGradleIT: BaseGradleIT() {
|
||||
|
||||
@Test
|
||||
fun testOmittedStdlibVersion() {
|
||||
val project = Project("kotlinProject", "2.3")
|
||||
val project = Project("kotlinProject", "4.4")
|
||||
project.setupWorkingDir()
|
||||
File(project.projectDir, "build.gradle").modify {
|
||||
it.replace("kotlin-stdlib:\$kotlin_version", "kotlin-stdlib").apply { check(!equals(it)) }
|
||||
it.replace("kotlin-stdlib:\$kotlin_version", "kotlin-stdlib").apply { check(!equals(it)) } + "\n" + """
|
||||
apply plugin: 'maven'
|
||||
install.repositories { maven { url "file://${'$'}buildDir/repo" } }
|
||||
""".trimIndent()
|
||||
}
|
||||
|
||||
project.build("build") {
|
||||
project.build("build", "install") {
|
||||
assertSuccessful()
|
||||
assertContains(":compileKotlin", ":compileTestKotlin")
|
||||
val pomLines = File(project.projectDir, "build/poms/pom-default.xml").readLines()
|
||||
val stdlibVersionLineNumber = pomLines.indexOfFirst { "<artifactId>kotlin-stdlib</artifactId>" in it } + 1
|
||||
val versionLine = pomLines[stdlibVersionLineNumber]
|
||||
assertTrue { "<version>${defaultBuildOptions().kotlinVersion}</version>" in versionLine }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+15
-4
@@ -10,6 +10,7 @@ import com.intellij.util.ReflectionUtil
|
||||
import groovy.lang.Closure
|
||||
import org.gradle.api.*
|
||||
import org.gradle.api.artifacts.Configuration
|
||||
import org.gradle.api.artifacts.ExternalDependency
|
||||
import org.gradle.api.file.ConfigurableFileCollection
|
||||
import org.gradle.api.file.FileCollection
|
||||
import org.gradle.api.file.SourceDirectorySet
|
||||
@@ -396,10 +397,20 @@ internal abstract class AbstractKotlinPlugin(
|
||||
|
||||
private fun configureDefaultVersionsResolutionStrategy(project: Project) {
|
||||
project.configurations.all { configuration ->
|
||||
configuration.resolutionStrategy.eachDependency { details ->
|
||||
val requested = details.requested
|
||||
if (requested.group == "org.jetbrains.kotlin" && requested.version.isEmpty()) {
|
||||
details.useVersion(kotlinPluginVersion)
|
||||
if (isGradleVersionAtLeast(4, 4)) {
|
||||
// Use the API introduced in Gradle 4.4 to modify the dependencies directly before they are resolved:
|
||||
configuration.withDependencies { dependencySet ->
|
||||
dependencySet.filterIsInstance<ExternalDependency>()
|
||||
.filter { it.group == "org.jetbrains.kotlin" && it.version.isNullOrEmpty() }
|
||||
.forEach { it.version { constraint -> constraint.prefer(kotlinPluginVersion) } }
|
||||
}
|
||||
}
|
||||
else {
|
||||
configuration.resolutionStrategy.eachDependency { details ->
|
||||
val requested = details.requested
|
||||
if (requested.group == "org.jetbrains.kotlin" && requested.version.isEmpty()) {
|
||||
details.useVersion(kotlinPluginVersion)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.gradle.utils
|
||||
|
||||
import org.gradle.util.GradleVersion
|
||||
|
||||
internal data class ParsedGradleVersion(val major: Int, val minor: Int) : Comparable<ParsedGradleVersion> {
|
||||
override fun compareTo(other: ParsedGradleVersion): Int {
|
||||
val majorCompare = major.compareTo(other.major)
|
||||
@@ -51,4 +53,8 @@ internal data class ParsedGradleVersion(val major: Int, val minor: Int) : Compar
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun isGradleVersionAtLeast(major: Int, minor: Int) =
|
||||
ParsedGradleVersion.parse(GradleVersion.current().version)
|
||||
?.let { it >= ParsedGradleVersion(major, minor) } ?: false
|
||||
Reference in New Issue
Block a user