Lukasz Kalnik
07/01/2022, 8:58 AMtestImplementation(project(":myProject"))
I need the syntax for writing a Kotlin plugin:
project.dependencies.add("testImplementation", /* How do I create a project dependency notation here? */)
jendrik
07/01/2022, 9:03 AMproject(":myProject")
Which error are you getting if you do that?Lukasz Kalnik
07/01/2022, 9:04 AMjendrik
07/01/2022, 9:05 AMLukasz Kalnik
07/01/2022, 9:05 AMjendrik
07/01/2022, 9:06 AMproject()
method is on the Project
interface.
So you can do project.project(...)
Lukasz Kalnik
07/01/2022, 9:06 AMjendrik
07/01/2022, 9:12 AMwith
to have the whole method in the project
context, then it looks like in the DSL. E.g.:
override fun apply(project: Project): Unit = with(project) {
...
}
Lukasz Kalnik
07/01/2022, 9:12 AMjendrik
07/01/2022, 9:12 AMLukasz Kalnik
07/01/2022, 9:13 AMProject
is indeed always the implicit receiver for the whole syntax. I was not aware of this (because the DSL is so convenient that I never actually thought how it works under the hood and how it connects to the underlying Gradle Java API).Vampire
07/01/2022, 9:53 AMproject.project(...)
?
I thought it should be project.dependencies.project(...)
.
project(...)
in a build script uses the latter (just navigate to it from a build script)
project.project
returns a Project
while project.dependencies.project
returns a ProjectDependency
.
Latest if you need to supply a configuration to depend on, you need to use project.dependencies.project
even if it otherwise works with project.project
.Lukasz Kalnik
07/01/2022, 9:59 AMDependencyHandler.add()
just accepts Object
...override fun apply(project: Project) {
with(project) {
dependencies.add("testImplementation", project(":testFixtures"))
}
}
Vampire
07/01/2022, 10:02 AMProjectDependency
, String
and others.
I'm not sure whether it accepts Project
, could well be, I just was not awareLukasz Kalnik
07/01/2022, 10:02 AMString
Dependency
Vampire
07/01/2022, 10:03 AMLukasz Kalnik
07/01/2022, 10:04 AMVampire
07/01/2022, 10:04 AMLukasz Kalnik
07/01/2022, 10:08 AMproject.dependencies.project()
doesn't allow String as argument:Vampire
07/01/2022, 10:09 AMMap
project.dependencies.project(mapOf("path" to ":foo"))
project.project
you can also just stick with it, I just was not aware that is accepted.Lukasz Kalnik
07/01/2022, 10:15 AMgradlePlugins
module) I need to somehow add dependency on the Kotlin DSL in this module, right?DependencyHandler.project()
extension function from org.gradle.kotlin.dsl.DependencyHandlerExtensions.kt
Vampire
07/01/2022, 10:23 AMLukasz Kalnik
07/01/2022, 10:23 AMVampire
07/01/2022, 10:23 AMkotlin-dsl
plugin if you also want the other bits it does, or there is a helper for it, I have to search for. Probably something like implementation(kotlinDsl())
Lukasz Kalnik
07/01/2022, 10:25 AMimplementation(gradleKotlinDsl())
Vampire
07/01/2022, 10:25 AMkotlin-dsl
applies java-gradle-plugin
, kotlin-dsl.base
, and kotlin-dsl.precompiled-script-plugins
plugins
kotlin-dsl.base
plugin applies embedded-kotlin
, adds gradleKotlinDsl()
to the dependencies of compileOnly
and testImplementation
configurations, and configures the Kotlin DSL compiler plugins for example for proper SAM conversion for Action
and similar.compileOnly(gradleKotlinDsl())
would be more appropriate as the classes are provided by the runtime environment already.Javier
07/01/2022, 10:28 AMimport org.gradle.api.artifacts.dsl.project
Lukasz Kalnik
07/01/2022, 10:28 AMimport org.gradle.kotlin.dsl.project
Javier
07/01/2022, 10:29 AMLukasz Kalnik
07/01/2022, 10:29 AMJavier
07/01/2022, 10:29 AMgradleKotlinDsl
Lukasz Kalnik
07/01/2022, 10:30 AMJavier
07/01/2022, 10:30 AMLukasz Kalnik
07/01/2022, 10:30 AMJavier
07/01/2022, 10:30 AMLukasz Kalnik
07/01/2022, 10:31 AMVampire
07/01/2022, 10:31 AMJavier
07/01/2022, 10:33 AMtestImplementation
and so on are accessors whose need to be generated instead of being in an interfaceVampire
07/01/2022, 10:49 AMJavier
07/01/2022, 11:17 AMVampire
07/01/2022, 12:12 PMimplementation(...)
in Kotlin DSL, then try navigating to it and looking at the file path and you will see it is generated.Javier
07/01/2022, 12:26 PMVampire
07/01/2022, 12:28 PMimplementation
is always usable. So it can also be hard-coded.Javier
07/01/2022, 12:29 PM