Anyone has a clue what this even means? ``` protoc...
# gradle
r
Anyone has a clue what this even means?
Copy code
protoc { 
         ^ Type mismatch: inferred type is () -> Unit but Closure<(raw) Any!>! was expected`
Raw? Any? protoc looks like this:
public void protoc(Closure configureClosure) {
o
Closure
is a groovy concept
() -> Unit
is a Kotlin lambda that takes & returns nothing, and it can't be converted to
Closure
implicitly
👍 1
you can use
delegateClosureOf
or
closureOf
to do it explicitly
or the class
KotlinClosure1
which is a little more complicated
r
It is already complicated enough lol 😛 Thanks, I'll look into it!
b
I use such config:
Copy code
import com.google.protobuf.gradle.GenerateProtoTask
import com.google.protobuf.gradle.ProtobufConvention
import com.google.protobuf.gradle.ProtobufPlugin
import com.google.protobuf.gradle.id
import com.google.protobuf.gradle.plugins
import com.google.protobuf.gradle.proto
import com.google.protobuf.gradle.protobuf
import com.google.protobuf.gradle.protoc

buildscript {
  dependencies {
    classpath("com.google.protobuf:protobuf-gradle-plugin:+")
  }
}

apply {
  plugin<JavaPlugin>()
  plugin<ProtobufPlugin>()
}

dependencies {
  "compile"("com.google.protobuf:protobuf-java")
  "compile"("io.grpc:grpc-stub")
  "compile"("io.grpc:grpc-protobuf")

  if (JavaVersion.current().isJava9Compatible) {
    "implementation"("javax.annotation:javax.annotation-api:+")
  }
}

configure<ProtobufConvention> {
  protobuf {
    protoc {
      artifact = "com.google.protobuf:protoc"
    }
  }
}

configure<JavaPluginConvention> {
  sourceSets {
    this["main"].java.srcDir(the<ProtobufConvention>().protobuf.generatedFilesBaseDir + "/main/java")
  }
}

tasks["build"].dependsOn(tasks.withType(GenerateProtoTask::class))
r
@BorzdeG that is actually very usefull! Where does this point?
the<ProtobufConvention>().protobuf.generatedFilesBaseDir + "/main/java"
b
$PROJECT_DIR$/build/generated/source/proto/main/java
I'm sorry - I use dependency management:
Copy code
configurations.all {
    resolutionStrategy {
      failOnVersionConflict()

      eachDependency {
        when (requested.group) {
          "com.google.protobuf"             -> useVersion("3.6.1")
          "io.grpc"                         -> useVersion("1.17.0")}}}}
r
I never used that, looks nice to centralize versions. But I copied it in, still the same error. Should gradle take care of installing protobuf or should I do it manually and change my classpath?
Maybe because the last few lines use "main"-sourceSet and mine is supposed to be "commonMain" (which does not work when i changed it 😛 )
b
working example: https://github.com/BorzdeG/examples-proto-kotlin-multiplatform yes, it loads the protoc compiler for the OS you need did not add yet the plugin
kotlin-multiplatform
-example sketched in a quick way
r
That is so awesome, thank you very very much, i'm checking that out!
👍 1
IT WORKED!
I changed some things around, but some things in your example were crucial I guess! Thank you very much. Been struggling with this for over 2 days! Finally, I got output
đŸ”„ 3
@BorzdeG one more question, sorry, I hope you don't mind too much. My output is now Java. It should've been Kotlin because of the
kotlin-gradle-plugin
right? Any clue? Or shouldn't I be looking in
build/generated/source/proto/main/java
?
b
Yes, Java classes are generated. And then Java classes are used in Kotlin code. Something like this: http://tinyurl.com/ydefd47v
r
Ah, when i read this i thought it would be Kotlin code: https://grpc.io/blog/kotlin-gradle-projects but I guess they mean what you showed me with "being picked up by Kotlin/grpc" 🙂
g
There are a couple community projects about generating Kotlin code for grpc, they allow to use more Kotlin features
@BorzdeG You shouldn't use legacy `buildscipt`API for applying plugins, especially with Kotlin DSL If you use
plugins{}
you will get much more help from Gradle, including generated type safe accessors for things like
proc
extension, java convention and even to task.build
@BorzdeG @Robert I cleaned up Victor’s config to be more idiomatic and use all the features Kotlin DSL, as you can see everything is type safe now, do not require knowledge about particular types used by plugin (except GenerateProtoTask) and tricks how to access convention configs: https://gist.github.com/gildor/cf37ebb30cd8cf43b65cc70a055ec017 Just a side note, probably instead add genereteProto tasks as dependencies to build which is meta task, you should do that as dependency to assemble, or to be even more precise, use something like this:
Copy code
tasks.assemble {
    dependsOn(tasks.named("generateProto"))
}

tasks.test {
    dependsOn(tasks.named("generateTestProto"))
}
👍 1
Actually I just checked, and looks that those tasks already added as dependencies to assemble and test, so no need to configure it explicitly
One more thing, looks that no need to register source set manually, protobuf does this automatically, but to have integration with Idea you should just apply
idea
plugin
I removed grpc dependencies from my example, because it’s not enough to configure grpc, but you can return them in your real project. As you can see final result is much cleaner
b
@gildor I use buildscript instead of plugins because of several things: 0) it allows you to specify dynamic versions of plugins 1) Plug-ins can be applied "as needed", i.e. depending on conditions At the same time, I have not so much large projects as to have an impact on performance. Including the fact that I try not to use the Gradle daemon for "clean" launches, so as not to catch artifacts from previous builds
But here it is more convenient for someone. PS: I did not find information on the Gradle website that buildscript is outdated. And the collector itself does not inform that this section is outdated.
g
Plug-ins can be applied “as needed”, i.e. depending on conditions
You also can do this with plugins block
what is advantage of dynamic version of plugins? Why do you need this? This slow down whole build pipeline and make it fragile
buildscript is not yet deprecated, but it’s recommended to use
plugins
instead and I believe eventually buildscript will be used only for non-plugin dependencies
b
For example, I do not want to follow the updates of a plugin, but I always want to use its latest version. Or the version of the plugin always coincides with the version of dependencies from the same package - I don’t want to write the same version twice
g
Or the version of the plugin always coincides with the version of dependencies from the same package - I don’t want to write the same version twice
No need to do that, you can extract it to buildSrc or use some other way to share versions
For example, I do not want to follow the updates of a plugin, but I always want to use its latest version.
If plugin works, why would you update it implicitly, it’s against reproducible builds and performance
but I always want to use its latest version
But why? What is you real use case?
b
Or here is a small project that I don’t know how to implement without buildscript: https://github.com/itbasis/kotlin-assistant-config-project-plugin
g
What is exactly doesn’t work without buildscript?
b
But why? What is you real use case?
The version file contains an explicit version of the plugin. But at the same time, there is a separate Job, which tests on the latest versions. Thus, it is operatively visible when the assembly fails with updates. This allows you to “look ahead” to CI and have less headaches when updating
g
tasks.withType(Wrapper::class.java).configureEach {}
->
tasks.withType<Wrapper> { }
or even
tasks.wrapper {}
(which is not exactly the same, but in your case will be the same) Also you usually don’t need this task, easier just update wrapper version using command line or manually
If you would use plugins dsl for this, everything will be type safe and clean: https://github.com/itbasis/kotlin-assistant-config-project-plugin/blob/master/samples/sample-jvm-junit4/build.gradle.kts
Copy code
plugins {
    kotlin("platform-jvm")
}

dependencies {
  expectedBy(project(":samples:sample-common"))
}
Also, see Kotlin DSL migration guide, which also strongly recommend to use plugins dsl instead https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/#applying_plugins
b
kotlin("platform-jvm")
But this is not a static compilation and "magic". When explicitly calling a class, it is always clear where it comes from and where to look at the sources and documentation. This allows you to view the code without IDEA - in some other IDE or in VIM
g
this is not a magic, it’s application of plugin by plugin id
Also everything on this script depends on it
and will fail in configuration time
Even for such small script you have dynamic things which completely non-necessary
I don’t want to convince you against your will. I just pretty sure this is against best practices and much less safe, you have no IDE help and slower. So I wouldn’t recommend use it, especially when we have now so many nice static accessors, I finally can write configs without magic class names and just have help from IDE So I just want to show that Kotlin DSL is actually great if you use it in a right way
Or here is a small project that I don’t know how to implement without buildscript
I see what you mean, you have custom plugin in buildSrc, and it’s completely fine to use with
plugins
block and have type safe accessors for extensions and tasks. You can check this example https://github.com/gradle/kotlin-dsl/tree/master/samples/buildSrc-plugin Also there are a few tasks that should simplify it even more, you already can use precompiled plugins API for simple cases which is not exactly the same, but allows to create simple plugins without registration, just putting them to plugin-name.gradle.kts files in buildSrc
b
you have no IDE help
On the contrary - it is much more, since I can "jump" in the IDE via Ctrl + Click into the description of a particular class. While IDEA does not allow me to do this with the "best practices". Although it may have already learned - it is necessary to check.
I really want to show you what it is.
I proceed from the simple - if something cannot be used, then it should be prohibited at the language level. If the use of the buildscript section is prohibited, but you need to leave it only in Groovy DSL and not drag in Kotlin DSL Also, once, it was a problem to use plug-ins used in external * .kts files - the configuration was described not by one file, but was split into several semantic ones - testing.gradle.kts, application.gradle.kts, etc ... I'll see how things are with this now and maybe update the configuration.
g
e IDEA does not allow me to do this with the “best practices”
It’s completely not true
b
Although it may have already learned - it is necessary to check.
g
Also, once, it was a problem to use plug-ins used in external * .kts files - the configuration was described not by one file, but was split into several semantic ones - testing.gradle.kts, application.gradle.kts, etc ...
this is different story not about plugins/builscript, related to script plugins. It’s also not the best way to deal with configuration with Kotlin DSL, because script plugins has only dynamic API for now. If you really want to extract big parts of config, better to use buildSrc And I’m talking about precompiled plugins, not just a separate kts file applied using
apply(from=)
precompiled plugins is relatevely new thing
if you have some real use cases for buildscript and want to use Kotlin DSL, I would report them to Gradle team, maybe they have better solution than use builscript and do not have any help from Kotlin DSL
If the use of the buildscript section is prohibited, but you need to leave it only in Groovy DSL and not drag in Kotlin DSL
It’s of course not prohibited, there are some valid use cases even now, for example to add some dependency to buildscript, but usually better to use plugins and for plugins you don’t need buildscript
Although it may have already learned
it’s never was true. Maybe just some IDE problems with navigation, but all static accessors code is available and you can see what they actually do and see used classes
message has been deleted
The rest does not want to collect / search. When problems arise, I report them to YouTrack with enough examples to reproduce. Usually, either the error in IDEA is corrected, or it is written how to do it so that it is correct.
Moreover, an example that in the screenshot, works great in the Groovy DSL. I would like the same from Kotlin DSL
g
yes, you don’t have access to
compile
because you don’t use plugins block
Moreover, an example that in the screenshot, works great in the Groovy DSL
Yes because in groovy it’s completely dynamic and checked only on runtime
See, the same example with plugins DSL, everything works perfectly, if you really want you can also remove version and use resolutionStrategy instead, how you do in your example
I have autocomplete and navigation in this case, also less typing
b
The plugin is applied in a clearly legal way for Gradle - at this stage you can already see what it is and use it.
g
It’s legal, but not recomended for Kotlin DSL
otherwise you don’t have statically typed accessors
b
"not recommended" does not mean that he should tell me how to use it - just advise. If it is not possible to use it this way, then such an entry should be prohibited. Otherwise, he should understand what he wrote.
g
Yes, this is just an advice. You show code snippet with problem, I advice you better solution
b
g
then such an entry should be prohibited
I would happy, but it’s not so easy, requires much longer deprecation cycle.
But I sure at some point plugins dsl
also it doesn’t solve all the possible cases, but solves most of them
This part about external dependencies! Which is much wider thing, not only plugins can be applied using buildscript
Instead, please read chapter about plugins https://docs.gradle.org/5.0/userguide/plugins.html
Which covers exactly what I said
and also, if you want to know best practices with Kotlin DSL, just check official migration guide that focus on such things
b
Was reading. I use section https://docs.gradle.org/5.0/userguide/plugins.html#sec:old_plugin_application It is legal. Yes, legacy, but not deprecated
đŸ’© 1
g
This is exactly what I said in my first message
Also it’s not just “legacy because we have new shiny thing”, but because Kotlin DSL just doesn’t work properly with buildscript, you cannot use most of features, so plugins is even more important for Kotlin DSL
b
But if you look at the link, then there are examples for the Kotlin DSL and it is not written that it is impossible to write like that at all. Therefore - there are no deviations from the documentation.
g
There is very clear instructions about this in official migration guide on Gradle website. Be free to report documentation issue if you think it should be added to section about plugins
then there are examples for the Kotlin DSL and it is not written that it is impossible to write like that at all
Not sure what you mean. Do you see some invalid snippet in docs?
b
message has been deleted
message has been deleted
g
All those samples are valid
but even on exactly the same page explained, that this is legacy API
and better to use plugins dsl
I really don’t understand what is not clear, this is reference, that shows all available APIs, also has references to other sections If you need best practices read migration guide
b
e walk in a circle. When this is marked as "deprecated", and not "legacy", then you can say that you can not write like that. In the meantime, it's just "not recommended"
đŸ’© 1
g
I give up %)
Even if something is deprecated, nobody, even I, cannot force you to write in a particular way I just tried to help, sorry for that
👍 2
b
But I have long agreed with you:
I'll see how things are with this now and maybe update the configuration.
g
why do you arguing than? I mean yeah, your config is valid, I just suggested how you can improve it.
b
And I agreed with this 🙂 PS: probably here's an example, why I am writing this way. It will be necessary to try to rewrite it on Kotlin DSL without losing functionality https://github.com/BorzdeG/name.alenkov.habr.gradle-dynamic-dependency
g
@BorzdeG I see what you trying to do. But I don’t think that you need all this dynamic stuff with dynamically include some directory. Now instead of this you can just use composite builds
➕ 1
Also with coposite build you have no problems with versions
it still uses buildscript because you have dynamic version of plugin to apply
b
accepted pull request. Thank!