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

时间:2019-03-28 19:07:01

标签: 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.

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

2 个答案:

答案 0 :(得分:0)

我这里有一个示例,不幸的是它是用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中完成。

答案 1 :(得分:0)

这在一定程度上有效,但可能不是最好的方法

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 tlibJar by tasks.registering(Jar::class) {
            from(sourceSets["tlib"].output)
        }

        val tlibSourcesJar by tasks.registering(Jar::class) {
            archiveClassifier.set("sources")
            from(sourceSets["tlib"].allSource)
        }

        register("mavenTLib", MavenPublication::class) {
            artifactId = "phg-entity-tlib"
            artifact(tlibJar.get())
            artifact(tlibSourcesJar.get())
        }
    }
}