this post was submitted on 27 Jan 2024
7 points (100.0% liked)

Kotlin

671 readers
2 users here now

Kotlin is a statically typed programming language for the JVM, Android, JavaScript, and native.

Subreddit rules:

Resources:

founded 1 year ago
MODERATORS
 

Hello developers!

I need some programming advice and I hope that I am not off-topic. I couldn't find a more relevant group in Lemmy.

I have a Gradle convention plugin (written in Kotlin) that needs a buildscript classpath. But I can't get it to resolve correctly in the project.

  1. If I add the buildscript classpath to the convention plugin and then apply the plugin to the project, it throws an error saying that buildscript is not allowed and the plugins section should be used instead.
  2. If I add the buildscript classpath to the project itself (although this doesn't feel right because the convention plugin should be already compiled), then it throws an error saying that the dependencies are not met (classpath not applied).

Any advice?

all 5 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 9 months ago* (last edited 9 months ago) (1 children)

I'm not sure what you mean with "needs a buildscript classpath". Are you trying to use a dependency in your convention plugin? If so you should add it to build.gradle in the buildSrc directory.

An example of what you are trying to do would help a lot.

[–] puppy 2 points 9 months ago* (last edited 9 months ago) (1 children)

Hi. Thank you for responding. I basically want to add the jib extension mentioned here (code snippet provided). https://github.com/GoogleContainerTools/jib-extensions/tree/master/first-party/jib-ownership-extension-gradle

Instead of applying it directly to the project, I want to add all this config to a convention plugin and add that to the project. Hoping to reuse the config across many projects.

[–] [email protected] 2 points 9 months ago* (last edited 9 months ago) (1 children)

I threw this together really quick: https://github.com/foip/jib-convention-test . The crux is adding the jib plugin and the extension to buildSrc/build.gradle.kts. I don't know if this matches your project setup, so let me know if this does or doesn't work for you (:

Edit:

To put the answer to the original question in more general terms for anyone who stumbles upon this thread: In this case the jib-gradle-plugin is applied as a plugin in the root build.gradle.kts, but it needs extra runtime dependencies for its extensions. You normally would declare those dependencies like so:

// in root build.gradle.kts
buildscript {
  dependencies {
    classpath(/* dependency */)
  }
}

When you want to write a convention plugin to wrap that configuration, basically everything that went inside that buildscript block now goes into buildSrc/build.gradle.kts:

// in buildSrc/build.gradle.kts
repositories {
  mavenCentral()
}

dependencies {
  implementation(/* dependency */)
}

That goes for other dependencies as well, for example if you want to use a library to write a custom task, and then you refactor that task into a plugin inbuildSrc.

[–] puppy 2 points 9 months ago* (last edited 9 months ago)

The extension seems to be finally applied. Thank you so much!