Search code examples
gradlegradle-kotlin-dsl

Build and publish test classes with gradle.kts


We have a multiproject gradle repository with several subprojects. One of the subprojects is supposed to generate separate testJar and publish it to the local maven repository:

// build a repositories-testJar.jar with test classes for services tests
tasks.register<Jar>("testJar") {
    archiveClassifier.set("testJar")
    from(sourceSets["test"].output)
}

// the testJar must be built within the build phase
tasks.build {
    dependsOn(tasks.withType<Jar>())
}

// needed for publishing plugin to be aware of the testJar
configurations.create("testJar") {
    extendsFrom(configurations["testCompile"])
}

artifacts {
    add("testJar", tasks.named<Jar>("testJar"))
}

This works and I can see -testJar.jar is generated within the /build/libs.

Then, within the rootProject gradle.kts we set up a publishing extension:

allprojects {
    configure<PublishingExtension> {
        publications {
            create<MavenPublication>(project.name) {
                groupId = "foo.bar"
                artifactId = project.name.toLowerCase().replace(":", "-")
                version = "ci"
                from(components["kotlin"])
            }
        }

        repositories {
            maven {
                url = uri("file://${rootProject.rootDir}/../m2")
            }
        }
    }
}

This also normally works and I can see modules being published in the m2 directory. However the testJar is not published. I couldn't find a way how to attach the testJar artifact to the publication defined in root project.


Solution

  • Solved with addition of:

    publishing {
        publications {
            create<MavenPublication>(project.name + "-" + testArtifact.name) {
                groupId = "my.group"
                artifactId = project.name.toLowerCase().replace(":", "-")
                version = "version"
                artifact(testArtifact)
            }
        }
    }