vanniktech / gradle-maven-publish-plugin

A Gradle plugin that publishes your Android and Kotlin libraries, including sources and javadoc, to Maven Central or any other Nexus instance.
https://vanniktech.github.io/gradle-maven-publish-plugin
Apache License 2.0
1.29k stars 119 forks source link

Unable to upload artifacts to github repository #308

Closed abrolrahul closed 2 years ago

abrolrahul commented 3 years ago

First, thanks for the amazing work guys. I discovered that my artifacts were not published with the command inside CircleCI.

/gradlew publish --no-daemon --no-parallel

Its a Kotlin multiplatform project. and when I run a build no repository is published in the Github Packages (empty). I tried without the plugin with a manual script in build.gradle and it is published correctly, Now I'm trying to publish with this plugin.

Here is my root build.gradle:

import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.FileInputStream
import java.util.*

plugins {
    kotlin("multiplatform") version Versions.kotlin
    kotlin("plugin.serialization") version Versions.serialization
    id("com.diffplug.spotless") version Versions.spotless
    id("org.jetbrains.dokka") version Versions.dokka
    id("org.jetbrains.kotlinx.binary-compatibility-validator") version Versions.compatibility
    id("com.vanniktech.maven.publish") version Versions.vanniktech
}

val versionName: Any
    get() = findProperty("VERSION_NAME") as Any

group = "com.*********"
version = versionName

val localProperties = Properties()

val pomArtifactId: String?
    get() = findProperty("POM_ARTIFACT_ID") as String?

val pomName: String?
    get() = findProperty("POM_NAME") as String?

val githubPackagesUrl: String?
    get() = findProperty("POM_URL") as String?

val pomGroupId: String?
    get() = findProperty("GROUP") as String?

@Suppress("DEPRECATION")
val javadocsJar by tasks.creating(Jar::class) {
    val dokkaGfm by tasks.getting {}

    dependsOn(dokkaGfm)

    classifier = "javadoc"

    from("$buildDir/dokka/gfm")
}

val enableNativeTarget = System.getProperty("enableNativeTarget", "true")?.toBoolean() ?: true

val enableJsTarget = System.getProperty("enableJsTarget", "true")?.toBoolean() ?: true

val browserSafari = System.getProperty("browserSafari", "false")?.toBoolean() ?: false

val fatJar = System.getProperty("fatJar", "false")?.toBoolean() ?: false

val disableApiValidation = System.getProperty("apiValidation", "false")?.toBoolean() ?: false

repositories {
    google()
    gradlePluginPortal()
    mavenCentral()
}

kotlin {
    explicitApi()
    explicitApiWarning()

    spotless {
        kotlin {
            trimTrailingWhitespace()
            ktlint(Versions.ktlint).userData(
                mapOf(
                    "disabled_rules" to "no-wildcard-imports"
                )
            )
        }
        kotlinGradle {
            trimTrailingWhitespace()
            ktlint(Versions.ktlint).userData(
                mapOf(
                    "disabled_rules" to "no-wildcard-imports"
                )
            )
        }
    }

    targets.all {
        compilations.all {
            kotlinOptions {
                allWarningsAsErrors = true
            }
        }
    }

    jvm {
        withJava()

        tasks.withType<KotlinCompile> {
            kotlinOptions {
                freeCompilerArgs = mutableSetOf("-Xjsr305=strict").toList()
                jvmTarget = JavaVersion.VERSION_11.toString()
            }
        }

        if (fatJar) {
            @Suppress("UNUSED_VARIABLE")
            val jvmJar by tasks.getting(org.gradle.jvm.tasks.Jar::class) {
                doFirst {
                    from(
                        configurations.getByName("runtimeClasspath")
                            .map {
                                if (it.isDirectory) it else zipTree(it)
                            }
                    )
                }
            }
        }
    }

    if (enableJsTarget) {
        js {
            binaries.executable()

            compilations.all {
                kotlinOptions {
                    moduleKind = "umd"
                    sourceMap = true
                    metaInfo = true
                }
            }

            useCommonJs()
            nodejs {
                testTask {
                    useMocha {
                        timeout = "30s"
                    }
                }
            }
            browser {
                testTask {
                    useKarma {
                        if (browserSafari) {
                            useSafari()
                        } else {
                            useChromeHeadless()
                            useFirefoxHeadless()
                        }
                    }
                }
                commonWebpackConfig {
                    cssSupport.enabled = true
                    outputFileName = "circleci.js"
                }
            }
        }
    }

    if (enableNativeTarget) {
        val xcf = XCFramework()

        val name = pomName

        iosX64("ios") {
            binaries {
                framework {
                    baseName = name

                    xcf.add(this)
                }
            }
        }
    }

    sourceSets {
        all {
            languageSettings.apply {
                optIn("kotlin.RequiresOptIn")
                optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
            }
        }

        @Suppress("UNUSED_VARIABLE")
        val commonMain by getting {
            dependencies {
                api("org.jetbrains.kotlinx:kotlinx-datetime:${Versions.datetime}")
                api("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.jsonSerialization}")
                api("io.ktor:ktor-client-logging:${Versions.ktor}")
                api("io.ktor:ktor-client-core:${Versions.ktor}")
                api("io.ktor:ktor-client-json:${Versions.ktor}")
                api("io.ktor:ktor-client-logging:${Versions.ktor}")
                api("io.ktor:ktor-client-serialization:${Versions.ktor}")

                api("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}-native-mt") {
                    version {
                        strictly("${Versions.coroutines}-native-mt")
                    }
                }

                api("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.jsonSerialization}")
            }
        }

        @Suppress("UNUSED_VARIABLE")
        val commonTest by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
                implementation("app.cash.turbine:turbine:${Versions.turbine}")

                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }

        @Suppress("UNUSED_VARIABLE")
        val jvmMain by getting {
            dependencies {
                api("io.ktor:ktor-client-cio:${Versions.ktor}")
                api("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
                api("org.slf4j:slf4j-simple:${Versions.sl4j}")
            }
        }

        @Suppress("UNUSED_VARIABLE")
        val jvmTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("org.assertj:assertj-core:3.12.2")
                implementation("org.jetbrains.kotlin:kotlin-test-junit")
            }
        }

        if (enableJsTarget) {
            @Suppress("UNUSED_VARIABLE")
            val jsMain by getting {
                dependencies {
                    api("org.jetbrains.kotlin:kotlin-stdlib-js")
                    api(npm("big.js", Versions.bigJs))
                }
            }

            @Suppress("UNUSED_VARIABLE")
            val jsTest by getting {
                dependencies {
                    implementation(kotlin("test-js"))
                }
            }
        }

        if (enableNativeTarget) {
            @Suppress("UNUSED_VARIABLE")
            val iosMain by getting {
                dependencies {
                    api("io.ktor:ktor-client-ios:${Versions.ktor}")
                }
            }

            @Suppress("UNUSED_VARIABLE")
            val iosTest by getting
        }
    }

    // TODO:: iOS networking tests fail (https://youtrack.jetbrains.com/issue/KT-42165)
    // TODO:: requires to start an emulator while running the tests ( work around )
    tasks.register("runIosTests") {
        val device = project.findProperty("iosDevice") as? String ?: "iPhone 8"

        dependsOn("linkDebugTestIos")

        group = JavaBasePlugin.VERIFICATION_GROUP

        description = "Runs tests for target 'ios' on an iOS simulator"

        doLast {
            val binary = (
                    kotlin.targets["ios"] as org.jetbrains.kotlin
                    .gradle.plugin.mpp.KotlinNativeTarget
                    )
                .binaries.getTest("DEBUG").outputFile

            exec {
                commandLine("xcrun", "simctl", "spawn", device, binary.absolutePath)
            }
        }
    }
}

apiValidation {
    ignoredPackages.add("com.priceline.circle.internal")

    validationDisabled = disableApiValidation
}

run {
    if (file("${project.rootDir}/local.properties").exists()) {
        try {
            localProperties.load(FileInputStream(rootProject.file("local.properties")))
        } catch (e: Exception) {
            println(e.toString())
        }
    }
}

extensions.getByType<PublishingExtension>().apply {
    publications {
        all {
            if (this !is MavenPublication) {
                println("this is not a maven publication $this")

                return@all
            }
            groupId = pomGroupId
            artifact(javadocsJar)
        }

        @Suppress("UNUSED_VARIABLE")
        val kotlinMultiplatform by getting {
            if (this !is MavenPublication) {
                return@getting
            }

            artifactId = "$pomArtifactId-multiplatform"
        }

        val jvm = findByName("jvm")

        if (jvm is MavenPublication) {
            jvm.artifactId = pomArtifactId
            signing {
                useInMemoryPgpKeys(**************** , *************)
            }
        }
    }

    repositories {
        maven {
            setUrl(githubPackagesUrl!!)
            credentials {
                username = localProperties["gpr.usr"]?.toString() ?: System.getenv("GPR_USER")
                password = localProperties["gpr.key"]?.toString() ?: System.getenv("GPR_API_KEY")
            }
        }
    }
}

tasks.register("printVersion") {
    doLast {
        print(versionName)
    }
}

Here is my gradle.properties lib lvl:

kotlin.code.style=official
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.native.enableDependencyPropagation=false
kotlin.mpp.stability.nowarn=true

# Gradle
org.gradle.daemon=true
org.gradle.caching=true
org.gradle.configureondemand=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx2048M -Dkotlin.daemon.jvm.options\="-Xmx2048M" -XX:+UseParallelGC

# Allow kapt to use workers, incremental processing
kapt.use.worker.api=true
kapt.incremental.apt=true
kapt.include.compile.classpath=false

# Android
android.useAndroidX=true

# kotlin
kotlin.incremental.multiplatform=true

#Vanniktech Maven Publish
GROUP=com.****
POM_ARTIFACT_ID=LIB
POM_NAME=MyLib
VERSION_NAME=1.0.6
POM_PACKAGING=jar

POM_DESCRIPTION=Library
POM_INCEPTION_YEAR=2021

POM_URL=https://maven.pkg.github.com/**/*********/
POM_SCM_URL=https://github.com/**/*********/
POM_SCM_CONNECTION=*******
POM_SCM_DEV_CONNECTION=******

POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo

POM_DEVELOPER_ID=***
POM_DEVELOPER_NAME=***
POM_DEVELOPER_URL=https://github.com/***/

Please let me know what I am doing wrong here. Any help would be appreciated.

oradkovsky commented 3 years ago

I was also not able to make it work with Github Packages. If author could find a bit of time to explain what we are doing wrong or provide a sample, that would be great. Otherwise great tool.

gabrielittner commented 3 years ago

Could you share the output of the build when publishing?

abrolrahul commented 2 years ago

I solved it by adding below block

mavenPublish {
        releaseSigningEnabled = false
        sonatypeHost = null
    }
abrolrahul commented 2 years ago

Adding the command and output which don't upload the artifact to GitHub packages.

./gradlew clean publish --no-daemon --no-parallel --warning-mode all

To honor the JVM settings for this build a single-use Daemon process will be forked. See https://docs.gradle.org/7.2/userguide/gradle_daemon.html#sec:disabling_the_daemon.
Daemon will be stopped at the end of the build 
> Task :buildSrc:compileKotlin
> Task :buildSrc:compileJava NO-SOURCE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:pluginDescriptors UP-TO-DATE
> Task :buildSrc:processResources NO-SOURCE
> Task :buildSrc:classes UP-TO-DATE
> Task :buildSrc:inspectClassesForKotlinIC UP-TO-DATE
> Task :buildSrc:jar UP-TO-DATE
> Task :buildSrc:assemble UP-TO-DATE
> Task :buildSrc:compileTestKotlin NO-SOURCE
> Task :buildSrc:pluginUnderTestMetadata UP-TO-DATE
> Task :buildSrc:compileTestJava NO-SOURCE
> Task :buildSrc:compileTestGroovy NO-SOURCE
> Task :buildSrc:processTestResources NO-SOURCE
> Task :buildSrc:testClasses UP-TO-DATE
> Task :buildSrc:test NO-SOURCE
> Task :buildSrc:validatePlugins UP-TO-DATE
> Task :buildSrc:check UP-TO-DATE
> Task :buildSrc:build UP-TO-DATE
Configuration on demand is an incubating feature.

The following Kotlin source sets were configured but not added to any Kotlin compilation:
 * androidAndroidTestRelease
 * androidTestFixtures
 * androidTestFixturesDebug
 * androidTestFixturesRelease
You can add a source set to a target's compilation by connecting it with the compilation's default source set using 'dependsOn'.
See https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#connecting-source-sets

Some Kotlin/Native targets cannot be built on this linux_x64 machine and are disabled:
    * In project ':shared':
        * targets 'iosArm64', 'iosX64' (can be built with one of the hosts: macos_x64, macos_arm64)
To hide this message, add 'kotlin.native.ignoreDisabledTargets=true' to the Gradle properties.

> Task :androidApp:clean
> Task :shared:cleanIosX64Test UP-TO-DATE
> Task :shared:cleanAllTests UP-TO-DATE
The AbstractCompile.destinationDir property has been deprecated. This is scheduled to be removed in Gradle 8.0. Please use the destinationDirectory property instead. Consult the upgrading guide for further information: https://docs.gradle.org/7.2/userguide/upgrading_version_7.html#compile_task_wiring
> Task :shared:clean
> Task :shared:publish UP-TO-DATE

BUILD SUCCESSFUL in 30s
9 actionable tasks: 3 executed, 6 up-to-date