xenoterracide
xenoterracide

Reputation: 16865

How do I publish a second library from another sourceset in gradle?

I have this tlib sourceset

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

I am imagining something like this, but this is not complete

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

but I get

Could not create domain object 'mavenTLib' (MavenPublication)
> SoftwareComponentInternal with name 'tlib' not found.

How can I publish my test lib separately from my main lib?

Upvotes: 1

Views: 1349

Answers (2)

xenoterracide
xenoterracide

Reputation: 16865

this works to an extent but is probably not the best way to do it

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

Upvotes: 1

Thomas Hirsch
Thomas Hirsch

Reputation: 2348

I have an example here, unfortunately it is written in Groovy, and I am not yet familiar with the Kotlin way of doing it.
Maybe it still helps: https://github.com/thokari/gradle-workshop/blob/master/examples/09-multiple-artifacts/build.gradle
The most relevant part is probably this:

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

I also never figured out how to make use of this SoftwareComponent concept. I solved this by calling the artifact method on the archiving tasks that I created, instead of using from(component), I would think this could also be done in Kotlin.

Upvotes: 0

Related Questions