Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 697
Shan Avatar asked Feb 12 '19 16:02

Shan


People also ask

What is buildSrc in Gradle?

buildSrc is a separate build whose purpose is to build any tasks, plugins, or other classes which are intended to be used in build scripts of the main build, but don't have to be shared across builds.


1 Answers

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.

like image 200
hotkey Avatar answered Sep 17 '22 00:09

hotkey