Arnab Kundu
Arnab Kundu

Reputation: 1527

How to do a maven-publish using Gradle Kotlin DSL (KTS)

I have the below code running in build.gradle file. I want to convert it into build.gradle.kts.

plugins {
    ...
    id 'maven-publish'
}

android {
    ....     
    publishing {
        singleVariant("release")
    }
}

dependencies {
    ...
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                groupId = 'com.arnab.storage'
                artifactId = 'Storage'
                version = "${project.ext.version.major}.${project.ext.version.minor}.${project.ext.version.build}"
                pom {
                    name = 'Android library for Storage system'
                    description = 'Useful library to help Android developers to work with Files in Android 10 and higher verison'
                }
                afterEvaluate {
                    from components.release
                }
            }
        }
    }
}

Upvotes: 0

Views: 39

Answers (1)

Arnab Kundu
Arnab Kundu

Reputation: 1527

The converted build.gradle.kts file code will be some thing like this.

plugins {
    ...
    id("maven-publish")
}

android {
   ...
   publishing {
        singleVariant("release")
    }
}

dependencies {
    ...
}

afterEvaluate {
    publishing {
        publications {
            create<MavenPublication>("release") {
                groupId = "com.arnab.storage"
                artifactId = "Storage"
                version = "${project.ext["major"]}.${project.ext["minor"]}.${project.ext["build"]}"
                pom {
                    name.set("Android library for Storage system")
                    description.set("Useful library to help Android developers to work with Files in Android 10 and higher version")
                }
                afterEvaluate {
                    from(components["release"])
                }
            }
        }
    }
}

Key Changes:

  • create("release"): Used to create the Maven publication instead of Groovy's release(MavenPublication).
  • project.extra[...]: Accesses ext properties using the extra map in Kotlin DSL.
  • name.set(...) and description.set(...): Kotlin DSL requires setting properties explicitly using set() for strings or other objects.
  • components["release"]: References the release component in a type-safe way.

Upvotes: 0

Related Questions