https://kotlinlang.org logo
Title
p

Pacane

11/20/2020, 4:00 PM
Is there a way to add an "implementation" dependency to
gradle-kotlin-dsl-extensions-6.7.jar
to a plugin project? I'm trying to access some extension functions in there like
fun DependencyHandler.`implementation`(dependencyNotation: Any): Dependency? =
    add("implementation", dependencyNotation)
m

Marc Knaup

11/20/2020, 4:06 PM
I think it’s auto-generated during your Gradle build based on the plugins that you’re using. So no, you cannot import it from anywhere 😅
p

Pacane

11/20/2020, 4:09 PM
ah, that's a bummer
I guess I should re-implement some then 😞
v

Vampire

11/20/2020, 4:29 PM
Where did you even see this jar file? I get those accessors only as generated files in the Gradle User Home cache
p

Pacane

11/20/2020, 4:30 PM
I saw it in my IDE in the "External Librairies" collapsible section
and I tracked the functions used in a build.gradle.kts file for the
implementation
function inside a
dependencies
block
v

Vampire

11/20/2020, 4:32 PM
Ah, I see. But if you look at the details in the module settings, you see the location as
p

Pacane

11/20/2020, 4:32 PM
and it showed up in there
ahh yeah
v

Vampire

11/20/2020, 4:33 PM
Interesting, if I navigate to the
implementation
call, it is in
👍 1
p

Pacane

11/20/2020, 4:33 PM
but I just tried adding that one jar with an
implementation(files(...))
dependency to my plugin project, and still can't reference those extension functions
yeah that's how I tracked in down
v

Vampire

11/20/2020, 4:35 PM
Anyway, if you use pre-compiled script plugins with the Kotlin DSL, you can also use those accessors, they are also there generated. If you write the plugin as normal Kotlin file, then not. In that case you probably have to just define them yourself, they are one-liners after all.
p

Pacane

11/20/2020, 4:35 PM
yeah that's what I did
fun DependencyHandlerScope.implementation(dependencyNotation: Any): Dependency? =
  add("implementation", dependencyNotation)

fun DependencyHandlerScope.testImplementation(dependencyNotation: Any): Dependency? =
  add("testImplementation", dependencyNotation)

fun DependencyHandlerScope.testRuntimeOnly(dependencyNotation: Any): Dependency? =
  add("testRuntimeOnly", dependencyNotation)
not too bad
but I wouldn't want to have to reimplement the world if I need other stuff 😛
I assumed there was a way to import those
g

gildor

11/20/2020, 5:10 PM
You cannot "import" them, because they generated only if those configurations are exist, so it truly type-safe way, those extensions are not
I think suggested by Björn precompiled script plugins is the best way to develop Gradle plugins in Kotlin, you are getting much more dsl improvements than using raw gradle APIs as in case of Java/Groovy plugins
p

Pacane

11/20/2020, 5:13 PM
how do you use those?
v

Vampire

11/20/2020, 5:13 PM
I wouldn't say "best", it depends on the actual needs
Some things are maybe nicer to do with "raw" Java, Groovy or Kotlin plugins
g

gildor

11/20/2020, 5:14 PM
Yeah, maybe not the best, but if you have experience with build.gradle configs, it looks much more natural
v

Vampire

11/20/2020, 5:14 PM
How you use those is basically you have
foo.gradle.kts
files in your Gradle source set and you apply the
gradle-dsl
plugin.
g

gildor

11/20/2020, 5:14 PM
Depends on complexity of the plugin of course
v

Vampire

11/20/2020, 5:15 PM
The
foo.gradle.kts
files are nearly identical to what you would write in a normal build file. One notable difference is, that you must not have version numbers at applied plugins but define those as dependencies in the build script of the plugin project
p

Pacane

11/20/2020, 5:16 PM
I see
v

Vampire

11/20/2020, 5:16 PM
The optional package statement in the file plus the name in front of
.gradle.kts
gives the plugin id
p

Pacane

11/20/2020, 5:16 PM
I was trying to write a plugin in pure kotlin
but also use some extensions from gradle-kotlin-dsl
p

Pacane

11/20/2020, 5:16 PM
I'll try and see if that thing fits my needs though
and then, in IntelliJ I assume you have to add your script to your standalone scripts to get some autocompletion?
v

Vampire

11/20/2020, 5:19 PM
No
It will just work fine