Search code examples
gradlekotlingradle-kotlin-dsl

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?


Solution

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