3

I've written a .gradle script named publish.gradle which configures publishing {} for releasing my artifact.

Why on a separate script? I have multiple modules and by doing this every releasable module simply defines some variables.

Module build.gradle.kts:

// Module's blah blah

apply(from = "../publish.gradle")

publish.gradle:

apply plugin: 'maven-publish'



publishing {
   publications {
      // configure release process
   }
}
  

I've recently decided to migrate to Gradle Kotlin DSL. However, there's an issue:

Adding publication {} like this:

plugins {
    `maven-publish`
}

publication {

}

Lead to this error:

Expression 'publishing' cannot be invoked as a function. The function 'invoke()' is not found
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val PluginDependenciesSpec.publishing: PluginDependencySpec defined in org.gradle.kotlin.ds

Which is summarized to

PluginDependenciesSpec is not present as a receiver

What is the difference?

TL; DR

I've added publishing {} config to a separate script which works when in .gradle groovy format but I can not convert to .gradle.kts kotlin format. The publishing is extension of PluginDependenciesSpec class which is not present in the script.

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117

1 Answers1

2

Here's what worked for me:

plugins {
    id("maven-publish")
}

configure<PublishingExtension> {
    publications.create<MavenPublication>("myPlugin") {
        groupId = "com.myCompany.android"
        artifactId = "MyPlugin"
        version = "1.0.0"
        pom.packaging = "jar"
        artifact("$buildDir/libs/MyPlugin.jar")

    }
    repositories {
        mavenLocal()
   }
}

I understand where you're coming from, converting from groovy to kotlin script is not a simple one to one translation, and most of us, including myself, code by example. In other words, you just need to see a simple example and you can figure out the rest. This works great when examples are readily available. What you need to do when you don't find an example is to turn to the API document. For example, https://docs.gradle.org/current/dsl/org.gradle.api.publish.PublishingExtension.html shows you the available properties for the PublishingExtension and you can keep drilling in to see what properties you have at the next level. This is especially important when examples may be working with an older version and may no longer be applicable. I will say that it wasn't as obvious is that for accessing extensions in kotlin script, requires the configure block. That's a big difference, but I like that approach, because it makes it clearer what the extension properties are a part of. And by the way, kotlin wants double quote, single quotes are no longer acceptable.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15