14

I have a custom gradle.kts script I am building that will do our maven publishing for all of our various modules to our sonatype repository, but encountering a strange error. Here are the contents of my maven-deploy.gradle.kts file:

plugins {
    `maven-publish`
    signing
}

publishing {
  //expression 'publishing' cannot be invoked as a function.
  //The function invoke() is not found
}

I can run tasks and whatnot within the maven-deploy.gradle.kts file fine, but trying to use the publishing function from the gradle documentation is proving to be impossible. Any ideas? I'm using gradle version 4.10.3 (I need Android support). The maven-deploy.gradle.kts file is in buildSrc/src/main/kotlin and is being added by id("maven-deploy") in my main project's build.gradle.kts file.

Vít Kotačka
  • 1,472
  • 1
  • 15
  • 40
Shan
  • 541
  • 3
  • 11

1 Answers1

32

This happens because Gradle only imports the generated type-safe accessors for Gradle Kotlin DSL into the main build script, but not into script plugins:

Only the main project build scripts have type-safe model accessors. Initialization scripts, settings scripts, script plugins (precompiled or otherwise) do not. These limitations will be removed in a future Gradle release.

See Understanding when type-safe model accessors are available

In the script you mentioned, you can access the publishing extension dynamically, for example, using configure<PublishingExtension> { ... }:

import org.gradle.api.publish.PublishingExtension

plugins {
    `maven-publish`
    signing
}

configure<PublishingExtension> { 
    // ...
}

This is described here: Project extensions and conventions

UPD: Gradle 5.3 RC1 seems to add a possibility to use the generated extensions in script plugins.

hotkey
  • 140,743
  • 39
  • 371
  • 326
  • Works great! Thank you! :-) And thank you for the documentation links as well (I really needed those) – Shan Feb 12 '19 at 18:19