繁体   English   中英

发布 Kotlin MultiPlatform 库 Bintray 使用 Kotlin DSL

[英]Publish Kotlin MultiPlatform Library Bintray Use Kotlin DSL

我用 Kotlin MultiPlatform (链接) 编写了一个库,我可以将该库作为jar文件添加到项目中,但现在我想用bintray做到这一点

我继续使用Link ,但我遇到了问题

我的问题是我给 Gradle 的PublicationName变量的值对我来说是错误的:

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



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

我的 Kotlin Gradle DSL 文件:

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")
    }
}

您应该创建一个用于通过bintray发布机制的 gradle 脚本,并将其applyMMP项目范围的build.gradle.kts中。

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"))

//
//
//
//

在下面放它publishLibraryVariantsandroid扩展到kotlin扩展


kotlin {
   android { publishLibraryVariants("release") }

//
//
//
//

}

这是我第一次在 Kotlin DSL 中看到“bintray”。 但根据他们的文档

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()
        }
    }
}

就个人而言,我正在使用其他东西。 你为什么不试试这个呢?

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"
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM