Search code examples
kotlingradlepackagegradle-kotlin-dslgithub-package-registry

Kotlin DSL "from" keyword not found


I have been trying to follow GitHub tutorial to publish a package. The problem is that I get the following error when trying to run Gradle:

Script compilation error:

Line 49:           from(components["java"])
                   ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
                       public inline fun <reified T : VersionControlSpec> VcsMapping.from(noinline configureAction: TypeVariable(T).() -> Unit): Unit defined in org.gradle.kotlin.dsl
                       public inline fun <T : VersionControlSpec> VcsMapping.from(type: KClass<TypeVariable(T)>, configureAction: Action<in TypeVariable(T)>): Unit defined in org.gradle.kotlin.dsl

For some reason, the "from" keyword is not recognized.

Here is my build.gradle.kts script:

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.5.0"
    `java-library`
    `maven-publish`
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("com.google.guava:guava:30.0-jre")
    testImplementation("org.jetbrains.kotlin:kotlin-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
    api("org.apache.commons:commons-math3:3.6.1")
}

publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/jorgeparavicini/draughts")
            credentials {
                username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
                password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
            }
        }
    }
    publications {
        register("gpr") {
            from(components["java"])
        }
    }

How can I fix this?


Solution

  • You did not provide the type of publication, so you use just a basic Publication. from() is a function of MavenPublication, so you need to explicitly specify that you need a MavenPublication:

    publications {
        register<MavenPublication>("gpr") {
            from(components["java"])
        }
    }