3

I wrote a library with Kotlin MultiPlatform (link) I can add the library as a jar file to the project But now I want to do this with bintray

I went ahead with the Link, but I had a problem

My problem was that the value of publicationName variable I was giving Gradle was wrong for me :

val publicationName = "MySharedCode"
publishing {
    publications.invoke {
        publicationName(MavenPublication::class) {
            artifactId = artifactID
            artifact(shadowJar)
            pom.addDependencies()
        }
    }
}



   Gradle Sync Error :  Publication with name 'MySharedCode' not found.

My Kotlin Gradle DSL File :

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.jfrog.bintray.gradle.BintrayExtension
import org.gradle.api.publish.maven.MavenPom
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("multiplatform")
    `maven-publish`
    id("com.jfrog.bintray") version "1.8.4"
    id ("com.github.johnrengelman.shadow") version "5.2.0"
    java
}


val ktorVersion = "1.3.0-rc2"
val serialization = "0.11.0"
val coroutines = "1.2.1"

project.group = "com.my.domain"
project.version = "0.0.3"
val artifactID = "my-shared-lib"

kotlin {
    //select iOS target platform depending on the Xcode environment variables
    val iOSTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
            ::iosArm64
        else
            ::iosX64

    iOSTarget("ios") {
        binaries {
            framework {
                baseName = "MySharedCode"
            }
        }
    }

    jvm("android")

    sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
        // HTTP
        implementation("io.ktor:ktor-client-core:$ktorVersion")
        implementation("io.ktor:ktor-client-json:$ktorVersion")
        implementation("io.ktor:ktor-client-serialization:$ktorVersion")

        // Coroutines
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines")


    }

    sourceSets["androidMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib")
        // HTTP
        implementation("io.ktor:ktor-client-android:$ktorVersion")
        implementation("io.ktor:ktor-client-json-jvm:$ktorVersion")
        implementation("io.ktor:ktor-client-serialization-jvm:$ktorVersion")

        // Coroutines
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines")
    }
    sourceSets["iosMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib")
        // HTTP
        implementation("io.ktor:ktor-client-ios:$ktorVersion")
        implementation("io.ktor:ktor-client-json-native:$ktorVersion")
        implementation("io.ktor:ktor-client-serialization-iosx64:$ktorVersion")

        // Coroutines
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines")
    }
}


val packForXcode by tasks.creating(Sync::class) {
    group = "build"

    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)

    inputs.property("mode", mode)
    dependsOn(framework.linkTask)

    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)

    doLast {
        val gradlew = File(targetDir, "gradlew")
        gradlew.writeText("#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n")
        gradlew.setExecutable(true)
    }
}

tasks.getByName("build").dependsOn(packForXcode)


val shadowJar: ShadowJar by tasks
shadowJar.apply {
    baseName = artifactID
    classifier = null
}
fun MavenPom.addDependencies() = withXml {
    asNode().appendNode("dependencies").let { depNode ->
        configurations.commonMainImplementation.allDependencies.forEach {
            depNode.appendNode("dependency").apply {
                appendNode("groupId", it.group)
                appendNode("artifactId", it.name)
                appendNode("version", it.version)
            }
        }
    }
}

val publicationName = "MySharedCode"
publishing {


    publications.invoke {
        publicationName(MavenPublication::class) {
            artifactId = artifactID
            artifact(shadowJar)
            pom.addDependencies()
        }
    }
}


fun findProperty(s: String) = project.findProperty(s) as String?
bintray {
    user = "xxxx"
    key = "xxxx"
    publish = true
    setPublications(publicationName)
    pkg(delegateClosureOf<BintrayExtension.PackageConfig> {
        repo = "lib"
        name = "my repo name"
        userOrg = "my user org"
        websiteUrl = "https://my-domain.com/"
        githubRepo = "myRepo"
        vcsUrl = "myRepo url"
        description = "my repo description"
        setLabels("kotlin")
        setLicenses("MIT")
        desc = description
    })
}

tasks {
    withType(GradleBuild::class.java) {
        dependsOn(shadowJar)
    }
    withType<KotlinCompile> {
        kotlinOptions.jvmTarget = "1.8"
    }
    withType(Test::class.java) {
        testLogging.showStandardStreams = true
    }
    withType<GenerateMavenPom> {
        destination = file("$buildDir/libs/${shadowJar.name}.pom")
    }
}
Farshid roohi
  • 722
  • 9
  • 23

2 Answers2

1

You should create a gradle script for publishing mechanism by bintray and apply that into build.gradle.kts of MMP project scope.

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget


plugins {
    id("com.android.library")
    id("org.jetbrains.kotlin.multiplatform")
}


apply(from = rootProject.file("publishSystem/publish.gradle"))

//
//
//
//

In the following put that it publishLibraryVariants in android extension into kotlin extension


kotlin {
   android { publishLibraryVariants("release") }

//
//
//
//

}

ImanX
  • 789
  • 6
  • 23
0

This my first time seeing the "bintray" in Kotlin DSL. But as per their documentation

bintray {
    user = "xxxx"
    key = "xxxx"
    publish = true
    publications = [publicationName] // this is an array
    pkg {
        repo = "lib"
        name = "my repo name"
        vcsUrl = "myRepo url"
        licenses = ["MIT"]
        version {
            name = "1.0.0"
            desc = "Release one Woraay ^_^"
            released  = new Date()
        }
    }
}

Personally, I'm using something else on my end. Why don't you try this one out?

publishing {
    repositories.maven("https://api.bintray.com/maven/THE REST OF THE URL;publish=1") {
        name = "bintray"

        credentials {
            username = "ADMIN"
            password = "ADMIN"
        }
    }

    publications {
        register("plugin", MavenPublication::class) {
            groupId = "com.example.MySharedCode"
            artifactId = "MySharedCode"
            version = "1.0.0"

            from(components["java"])
        }
    }
}

gradlePlugin {
    plugins {
        create("Gradle-Plugin") {
            this.id = "com.example.MySharedCode"
            this.implementationClass = "com.example.MySharedCode.gradle.MainClassNameHere"
        }
    }
}
hamada147
  • 495
  • 1
  • 8
  • 14
  • You seem very bintray savy. Can you maybe have a look at my question: https://stackoverflow.com/questions/60608602/gradle-6-2-2-broke-my-bintray-publishing-artifact-names-instead-of-version-numb – gutenmorgenuhu Mar 10 '20 at 07:43
  • Thanks. I believe someone else answered your question before I got the change. – hamada147 Mar 11 '20 at 12:48
  • Yes. But not that one : https://stackoverflow.com/questions/60595853/gradle-6-0-breaks-source-set-dependency Maybe you can give it a shot – gutenmorgenuhu Mar 11 '20 at 18:50
  • I'm sorry, it looks like I was also late in this one. – hamada147 Mar 13 '20 at 15:09