kotlingradlebuild.gradlegradle-kotlin-dsl

How to add artifact to a configuration in Gradle Kotlin DSL?


I have a java project that currently uses groovy + gradle. One of the sub-projects builds a test jar so that another sub-project can depend on and use the test jar. I want to migrate all the groovy gradle files to kotlin, and I can't figure out the equivalent kotlin code to add the test jar for the sub-project to the created configuration:

tasks.register('testJar', Jar) {
  archiveClassifier.set('tests')
  from sourceSets.test.output + sourceSets.test.allSource
}

configurations {
  tests
}

artifacts {
  tests testJar
}

My best attempt is something like this:

val testJar = tasks.register<Jar>("testJar") {
    archiveClassifier.set("tests")
    from(sourceSets["test"].output)
    from(sourceSets["test"].allSource)
}

configurations {
  create("tests") {}
}

artifacts {
  tests(testJar) // <--- Unresolved reference: tests
}

Solution

  • Named accessor functions within artifacts (such as tests) are not created in Kotlin unless the associated configurations are created in a plugin (see docs).

    Instead you can perform the same operation using the add function:

    artifacts {
        add("tests", testJar)
    }