【问题标题】:How to get publish to wait on AAR file to exist before trying to publish?如何在尝试发布之前让发布等待 AAR 文件存在?
【发布时间】:2022-01-10 11:44:00
【问题描述】:

问题说明 我正在尝试根据我通过命令行传入的属性创建一个构建并发布一个库。我想使用:

 ./gradlew clean build publish -PnexusUsername=* -PnexusPassword=* -PmajorVersion=x -PminorVersion=x -PbuildNumber=x 

解决方法 我目前的解决方法是我使用

./gradlew clean; ./gradlew build; ./gradlew publish -PnexusUsername=* -PnexusPassword=* -PmajorVersion=x -PminorVersion=x -PbuildNumber=x 

您将在下面找到我的主要构建 gradle。我尝试了许多等待添加 assembleRelease 并由它完成发布的想法,但可惜我不是 gradle 专家,所以我正在寻求帮助。我有两个 POJO/POKO 库和一个 Android 库,我将它们作为 AAR 发布。我收到的错误是由于 AAR 不存在而无法制作 POM 文件。

import CiCdDependencies.BOSS_SDK_SNAPSHOT
import CiCdDependencies.buildNumber
import CiCdDependencies.majorVersion
import CiCdDependencies.minorVersion
import CiCdDependencies.nexusPassword
import CiCdDependencies.NEXUS_URL
import CiCdDependencies.nexusUserName
import CiCdDependencies.productVersion
import CiCdDependencies.VERSION_NAME
import Coverage.androidCoverage
import Coverage.testCoverage

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.0.4")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0")
        classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.3")
    }
}
plugins {
    id("java")
    id("maven-publish")
    id("org.sonarqube").version("3.3")
}


ext {
    // Major / Minor / Release and Build numbering
    productVersion = (project.findProperty("productVersion") ?: (System.getenv("PRODUCT_VERSION")
        ?: "0")).toString()
    majorVersion =
        (project.findProperty("majorVersion") ?: (System.getenv("MAJOR_VERSION") ?: "0")).toString()
    minorVersion =
        (project.findProperty("minorVersion") ?: (System.getenv("MINOR_VERSION") ?: "0")).toString()
    buildNumber =
        (project.findProperty("buildNumber") ?: (System.getenv("BUILD_NUMBER") ?: "0")).toString()
    //for publishing
    nexusUserName =
        (project.findProperty("nexusUsername") ?: (System.getenv("NEXUS_USERNAME"))).toString()
    nexusPassword =
        (project.findProperty("nexusPassword") ?: (System.getenv("NEXUS_PASSWORD"))).toString()

    testCoverage = project.hasProperty("TEST_COVERAGE")
    androidCoverage = project.hasProperty("ANDROID_COVERAGE")
}

description = "MAFIA BOSS SDK (Kotlin)"

allprojects {
    apply {
        plugin("jacoco")
    }
    // Read only repositories for dependencies; this should never be used to publish
    repositories {
        google()
        mavenCentral()
        maven {
            name = "DI2E-BossSdkReleases"
            setUrl("$NEXUS_URL$BOSS_SDK_RELEASES")
            credentials {
                username = nexusUserName
                password = nexusPassword
            }
        }
        maven {
            name = "DI2E-BossSdkSnapshots"
            setUrl("$NEXUS_URL$BOSS_SDK_SNAPSHOT")
            credentials {
                username = nexusUserName
                password = nexusPassword
            }
        }
    }
}

sonarqube {
    properties {
        val gitBranch = "git rev-parse --abbrev-ref HEAD".runCommand()

        property("sonar.sourceEncoding", "UTF-8")
        property("sonar.host.url", "https://sonarqube.di2e.net")
        property("sonar.projectVersion", gitBranch)
        property("sonar.projectKey", "MAFIA-KotlinSDK")
        property("sonar.login", "c58feafd2a81797dec0361dd2d8758885e4145d7")
        property("sonar.junit.reportPaths", "**/test-results/**/*.xml")
        property("sonar.coverage.jacoco.xmlReportPaths", "**/reports/jacoco/jacocoTestReport/jacocoTestReport.xml")
    }
}

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

/**
 * Publishing configuration
 */
afterEvaluate {
    publishing {
        logger.lifecycle("Publishing ${VERSION_NAME()}")
        publications {
            val isSnapshot = !project.hasProperty("RELEASE")
            create<MavenPublication>("BOSSSDKJAVA") {
                groupId = "BOSS"
                artifactId = "SDK-KOTLIN"

                version = if (isSnapshot) {
                    "${VERSION_NAME()}-SNAPSHOT"
                } else {
                    VERSION_NAME()
                }
                // The interfaces JAR
                artifact("${rootProject.projectDir}/InfrastructureBase/build/libs/InfrastructureBase.jar") {
                    classifier = "InfrastructureBase"
                }

                // The implementations JAR
                artifact("${rootProject.projectDir}/InfrastructureImpl/build/libs/InfrastructureImpl.jar") {
                    classifier = "InfrastructureImpl"
                }
            }

            create<MavenPublication>("BOSSSDKANDROID") {
                groupId = "BOSS"
                artifactId = "SDK-ANDROID"

                version = if (isSnapshot) {
                    "${VERSION_NAME()}-SNAPSHOT"
                } else {
                    VERSION_NAME()
                }
                // The implementations AAR
                artifact("${rootProject.projectDir}/InfrastructureAndroidImpl/build/outputs/aar/InfrastructureAndroidImpl-release.aar") {
                    classifier = "InfrastructureAndroidImpl"
                }
            }
            repositories {
                maven {
                    url = if (isSnapshot) {
                        logger.lifecycle("Performing snapshot build.")
                        uri("$NEXUS_URL$BOSS_SDK_SNAPSHOT")
                    } else {
                        logger.lifecycle("Performing release build.")
                        uri("$NEXUS_URL$BOSS_SDK_RELEASES")
                    }

                    credentials {
                        username = nexusUserName
                        password = nexusPassword
                    }
                }
            }
        }
    }
}

afterEvaluate {
    tasks.getByName("jacocoTestReport", type = JacocoReport::class) {
        reports {
            print("I am reporting" )
            xml.required.set(testCoverage)
            html.required.set(testCoverage)
        }

        val fileFilter = mutableSetOf(
            "**/R.class",
            "**/R\$*.class",
            "**/BuildConfig.*",
            "**/Manifest*.*",
            "**/*Test*.*",
            "android/**/*.*",
            "**/*\$Lambda$*.*", // Jacoco can not handle several "$" in class name.
            "**/*\$inlined$*.*" // Kotlin specific, Jacoco can not handle several "$" in class name.
        )

        val debugTree = fileTree("${buildDir}/tmp/kotlin-classes/debug") {
            exclude(fileFilter)
        }
        val mainSrc = "${project.projectDir}/src/main/java"

        sourceDirectories.from(files(mainSrc))
        classDirectories.setFrom(debugTree)

        if (androidCoverage) {
            executionData.setFrom(fileTree("$buildDir") {
                include(
                    setOf(
                        "jacoco/*.exec",
                        "outputs/code-coverage/debugAndroidTest/connected/*coverage.ec",
                        "outputs/unit_test_code_coverage/*.exec"
                    )
                )
            })
        } else if (testCoverage) {
            executionData.setFrom(fileTree("$buildDir") {
                include(
                    setOf(
                        "jacoco/*.exec",
                        "outputs/unit_test_code_coverage/*.exec"
                    )
                )
            })
        }

        configurations.all {
            resolutionStrategy {
                eachDependency {
                    if ("org.jacoco" == requested.group) {
                        useVersion("0.8.7")
                    }
                }
            }
        }
    }
}

tasks.getByName("sonarqube"){
    dependsOn(tasks.getByName("jacocoTestReport"))
}

fun String.runCommand(currentWorkingDir: File = file("./")): String {
    val byteOut = getByteOutput()

    project.exec {
        workingDir = currentWorkingDir
        commandLine = this@runCommand.split("\\s".toRegex())
        standardOutput = byteOut
    }
    return String(byteOut.toByteArray()).trim()
}```

【问题讨论】:

    标签: kotlin gradle gradle-kotlin-dsl


    【解决方案1】:

    我相信 AGP 已经做了一些让事情变得更容易的事情,请在此处查看:https://developer.android.com/studio/build/maven-publish-plugin

    release(MavenPublication) {
        // Applies the component for the release build variant.
        from components.release
    
        // You can then customize attributes of the publication as shown below.
        groupId = 'com.example.MyLibrary'
        artifactId = 'final'
        version = '1.0'
    }
    

    Android Gradle 插件为您的应用或库模块中的每个构建变体工件创建一个组件,您可以使用它来自定义发布到 Maven 存储库。

    您可能需要将 android 部分移动到该 android 库的 build.gradle(.kts)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多