Fix failure when creating a compilation in afterEvaluate (KT-28896)

To configure a compilation's sources, we run an action in
`whenEvaluated { ... }`, expecting that the compilation's task already
exists. This was not true with compilations created from a user build
script or a 3rd-party plugin in `afterEvaluate { ... }`.

Fix this by expecting that a task may possibly not exist at that point,
and also using `whenEvaluated { ... }` instead of
`afterEvaluate { ... }` in several places that are executed for each new
compilation as a workaround for
https://github.com/gradle/gradle/issues/1135.

Issue #KT-28896 Fixed
This commit is contained in:
Sergey Igushkin
2019-01-09 20:06:07 +03:00
parent de5e86ae9f
commit 2fd524f1c0
3 changed files with 31 additions and 2 deletions
@@ -53,4 +53,14 @@ publishing {
repositories {
maven { setUrl("file://${projectDir.absolutePath.replace('\\', '/')}/repo") }
}
}
// Check that a compilation may be created after project evaluation, KT-28896:
afterEvaluate {
kotlin {
jvm("jvm6").compilations.create("benchmark") {
defaultSourceSet.dependsOn(sourceSets["jvm6Main"])
tasks["assemble"].dependsOn(compileKotlinTask)
}
}
}
@@ -83,4 +83,14 @@ publishing {
repositories {
maven { url "file://${projectDir.absolutePath.replace('\\', '/')}/repo" }
}
}
// Check that a compilation may be created after project evaluation, KT-28896:
afterEvaluate {
kotlin {
jvm('jvm6').compilations.create('benchmark') {
defaultSourceSet.dependsOn(sourceSets.jvm6Main)
assemble.dependsOn compileKotlinTask
}
}
}
@@ -81,13 +81,22 @@ abstract class AbstractKotlinCompilation<T : KotlinCommonOptions>(
commonSourceSet += sourceSet.kotlin
}
}
// Note! Invocation of getByName results in preliminary task instantiation. After fix of this issue the following code should be uncommented:
// Note! Invocation of withType-all results in preliminary task instantiation.
// After fix of this issue the following code should be uncommented:
// if (useLazyTaskConfiguration) {
// (target.project.tasks.named(compileKotlinTaskName) as TaskProvider<AbstractKotlinCompile<*>>).configure {
// it.configureAction()
// }
// }
(target.project.tasks.getByName(compileKotlinTaskName) as AbstractKotlinCompile<*>).configureAction()
target.project.tasks
// To configure a task that may have not yet been created at this point, use 'withType-matching-all`:
.withType(AbstractKotlinCompile::class.java)
.matching { it.name == compileKotlinTaskName }
.all { compileKotlinTask ->
compileKotlinTask.configureAction()
}
}
override fun source(sourceSet: KotlinSourceSet) {