How do you access a root (i.e. from `project`) pro...
# kotlin-dsl
t
How do you access a root (i.e. from
project
) property from a block where the local
this
has a property of the same name? …when that block also has a deprecated
project
property 😉 I have something like the following:
Copy code
testing {
    suites {
        named<JvmTestSuite>("test") {
            dependencies {
                implementation(project())
                implementation(project.dependencies.embeddedKotlin("gradle-plugin") as String)
            }
        }
    }
}
The thing is: that
dependencies
block (
JvmComponentDependencies
) has a
project
property that's now deprecated in 9.6.0, it also has no
embeddedKotlin()
extension. Is there a way (with a
this@xxx
notation?) to access the "root"
project
or
dependencies
property? Or is my only option to replace the
embeddedKotlin
helper with a more explicit
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:$embeddedKotlinVersion")
? Or would you rather pull that
embeddedKotlin()
call outside of the test suite into a variable? (btw, this is for unit-testing –with `ProjectBuilder`– a plugin that depends on KGP)
m
Copy code
val topLevelProject = project
testing {
    suites {
        named<JvmTestSuite>("test") {
            dependencies {
                implementation(project())
                implementation(topLevelProject.dependencies.embeddedKotlin("gradle-plugin") as String)
            }
        }
    }
}
?
t
Yes, an intermediate variable, moving that call outside of the problematic block. I was going with:
Copy code
testing {
    suites {
        named<JvmTestSuite>("test") {
            val kotlinGradlePlugin = project.dependencies.embeddedKotlin("gradle-plugin") as String
            dependencies {
                implementation(project())
                implementation(kotlinGradlePlugin)
            }
        }
    }
}
b
> Is there a way (with a
this@xxx
notation?) to access the "root"
project
or
dependencies
property? I really wanted there to be a
this@xxx
solution, but I think there isn't one. According to IntelliJ IDEA, the available
this
variants inside that
dependencies
block are: •
this
, which has type
JvmComponentDependencies
•
this@dependencies
, which has type
JvmComponentDependencies
•
this@named
, which has type
JvmTestSuite
•
this@suites
, which has type
NamedDomainObjectContainerScope<TestSuite>
•
this@testing
, which has type
TestingExtension
Unless I've overlooked something, none of these has a field that gets you to the “root”
project
or
dependencies
property. And there's just no named scope that refers to the top-level scope of the script itself.
👍 1
e
in K1 scripting, there was
this@Build_gradle
, but that depends on the script filename and doesn't work in K2
👍 1
🤔 1