gradle - 如何在gradle中从另一个源集发布第二个库?

标签 gradle kotlin gradle-kotlin-dsl

我有这个tlib源集

sourceSets {
    val main by getting
    val tlib by creating {
        compileClasspath += main.output
        runtimeClasspath += main.output
    }
    val test by getting {
        compileClasspath += tlib.output
        runtimeClasspath += tlib.output
    }
}

configurations {
    val tlibCompile by getting {
        extendsFrom(configurations["implementation"])
    }
}

我在想像这样的事情,但这还不完整
publishing {
    publications {
        val tlibSourcesJar by tasks.registering(Jar::class) {
            classifier = "sources"
            from(sourceSets["tlib"].allSource)
        }

        register("mavenTLib", MavenPublication::class) {
            from(components["tlib"])
            artifact(tlibSourcesJar.get())
        }
    }
}

但我明白了
Could not create domain object 'mavenTLib' (MavenPublication)
> SoftwareComponentInternal with name 'tlib' not found.

如何与主库分开发布测试库?

最佳答案

我这里有一个示例,不幸的是,它是用Groovy编写的,我还不熟悉Kotlin的编写方式。
也许它仍然有帮助:
https://github.com/thokari/gradle-workshop/blob/master/examples/09-multiple-artifacts/build.gradle
最相关的部分可能是这样的:

outputArchives.each { outputArchive ->
    String logicalName = outputArchive.camelCase()

    // Add archiving tasks.
    // These could be anything with type AbstractArchiveTask (e.g. War, Zip).
    task("${logicalName}Jar", type: Jar) { from configurations."${logicalName}Compile" }
    task("${logicalName}SourceJar", type: Jar) { from sourceSets."${logicalName}".java }

    // Configure the publishing extension added by the 'maven-publish' plugin.
    // For every combination of publication and repository, a task with name
    // publish<publicationName>PublicationTo<repositoryName>Repository is created.
    // The task 'publish' is a shortcut, depending on each one of them.
    publishing {
        publications {

            // Create a publication by calling its name and type.
            "${logicalName}"(MavenPublication) {

                // Override the artifact id, which defaults to the project name.
                artifactId = outputArchive.dashSeparated()

                // Publish the artifacts created by the archiving tasks.
                artifact tasks."${logicalName}Jar"
                artifact(tasks."${logicalName}SourceJar") { classifier 'source' }
            }
        }
    }
}

我也从来没有想过如何利用SoftwareComponent概念。我通过在创建的归档任务上调用artifact方法解决了这一问题,而不是使用from(component),我认为这也可以在Kotlin中完成。

关于gradle - 如何在gradle中从另一个源集发布第二个库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55405162/

相关文章:

gradle - 如何跟踪泄漏的 Gradle API 依赖项

java - 子项目取决于Gradle中的根项目

kotlin -::property.isInitialized 无法区分同名的方法和属性

java - Java/Kotlin 中 XML 解码为对象

使用 wsdl2java 的 Gradle Kotlin DSL

java - 如何将 JUnit 5 与 build.gradle.kts 和 kotlin 一起使用?

google-app-engine - GWT appstats + Gradle错误

java - 从 gradle java 项目构建的 jar 中排除所有已编译的类

java - jvm 将 String 与 StringBuffer.reverse() 进行比较总是失败

gradle - 如何在Kotlin DSL中的 `run`上设置系统属性