Why is 'publishing' function not being found in my custom gradle.kts file within buildSrc directory?
Asked Answered
T

1

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.

Towhead answered 12/2, 2019 at 16:16 Comment(0)
B
37

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.

Blueing answered 12/2, 2019 at 18:1 Comment(2)
Works great! Thank you! :-) And thank you for the documentation links as well (I really needed those)Towhead
A small step but it's missing from every tutorial, even googles documentation. thanks.Keitloa

© 2022 - 2024 — McMap. All rights reserved.