Attempting to use a buildSrc plugin in a subproject and the accessors are not being generated.
Static accessor generation should work.
Accessors are not generated and not available on classpath for subprojects, and depending on state of the project can either succeed or fail depending on what accessors have been stored in the Gradle home cache.
In a multi-project build this leads to an incredibly confusing experience that is difficult to figure out. The workaround is to use the dynamic string invocation accessors.
The expectation below is the subproject :r has task accessors from the "mybuild.my-base" plugin and the root project get's accessors for base and the built-in wrapper task.
Setup a Gradle 5.0 project. I did this running inside of a gradle:5.0.0-jdk8 Docker Hub container to make sure everything was clean.
$ docker run -it --rm gradle:5.0.0-jdk8
mkdir -p my-project && cd my-project
gradle init --type basic --dsl kotlin --project-name my-project
write root settings file (project name is just reduced from my original project)
cat << EOF > settings.gradle.kts
rootProject.name = "my-project"
include(":r")
EOF
write buildSrc build file buildSrc/build.gradle.kts
mkdir -p buildSrc && cat << 'EOF' > buildSrc/build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
`kotlin-dsl`
}
repositories {
jcenter()
}
gradlePlugin {
plugins {
register("myBase") {
id = "mybuild.my-base"
implementationClass = "mybuild.MyBasePlugin"
}
}
}
EOF
write plugin file
mkdir -p buildSrc/src/main/kotlin/mybuild/ && cat << EOF > buildSrc/src/main/kotlin/mybuild/MyBasePlugin.kt
package mybuild
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.invoke
import org.gradle.language.base.plugins.LifecycleBasePlugin
class MyBasePlugin : Plugin<Project> {
override fun apply(target: Project) {
target.run {
tasks {
register("myBaseTask") {
description = "My base task"
}
}
}
}
}
EOF
write build script for project :r.
mkdir -p r && cat << 'EOF' > r/build.gradle.kts
plugins {
base
mybuild.`my-base`
}
tasks {
// prove accessing a `base` plugin task works
assemble {
}
myBaseTask {
doLast {
println("my base")
}
}
}
EOF
run ./gradlew - static accessors can be accessed, and build success
create root build script build.gradle.kts
cat << EOF > build.gradle.kts
plugins {
base
}
tasks {
wrapper {
gradleVersion = "5.0"
}
assemble {
doFirst {
println("assembling!")
}
}
}
EOF
run ./gradlew - failure with static accessors
> Configure project :
e: /home/gradle/my-project/build.gradle.kts:6:3: Expression 'wrapper' cannot be invoked as a function. The function 'invoke()' is not found
e: /home/gradle/my-project/build.gradle.kts:6:3: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val PluginDependenciesSpec.wrapper: PluginDependencySpec defined in org.gradle.kotlin.dsl
e: /home/gradle/my-project/build.gradle.kts:7:5: Unresolved reference: gradleVersion
FAILURE: Build failed with an exception.
* Where:
Build file '/home/gradle/my-project/build.gradle.kts' line: 6
* What went wrong:
Script compilation errors:
Line 6: wrapper {
^ Expression 'wrapper' cannot be invoked as a function. The function 'invoke()' is not found
Line 6: wrapper {
^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val PluginDependenciesSpec.wrapper: PluginDependencySpec defined in org.gradle.kotlin.dsl
Line 7: gradleVersion = "5.0"
^ Unresolved reference: gradleVersion
3 errors
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.0/userguide/command_line_interface.html#sec:command_line_warnings
remove current caches
rm -rf ~/.gradle/
Run ./gradlew again to see a different build failure
> Configure project :r
e: /home/gradle/my-project/r/build.gradle.kts:10:3: Unresolved reference: myBaseTask
e: /home/gradle/my-project/r/build.gradle.kts:11:5: Unresolved reference: doLast
FAILURE: Build failed with an exception.
* Where:
Build file '/home/gradle/my-project/r/build.gradle.kts' line: 10
* What went wrong:
Script compilation errors:
Line 10: myBaseTask {
^ Unresolved reference: myBaseTask
Line 11: doLast {
^ Unresolved reference: doLast
2 errors
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.0/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 1m 25s
gradle --version
------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------
Build time: 2018-11-26 11:48:43 UTC
Revision: 7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987
Kotlin DSL: 1.0.4
Kotlin: 1.3.10
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_191 (Oracle Corporation 25.191-b12)
OS: Linux 4.15.0-43-generic amd64
It also still happens on Gradle 5.1-rc-3/Kotlin DSL: 1.1.0
Thanks for the thorough report, @mkobit. Fixed in 1a91c27.
Fix merged in 114ad450402555992557163ca4a9e4f14c02ffa3 and should be in the upcoming Gradle 5.1.1.
Most helpful comment
Thanks for the thorough report, @mkobit. Fixed in 1a91c27.