Search code examples
gradlekotlingradle-kotlin-dsl

How can I share build config between two build.gradle.kts files?


I have two projects (in a single git repository) that should have the same

repository {

}

section in their build.gradle.kts, but otherwise are completely unrelated.

Can I factor this common part out and include it in each respective build.gradle.kts? How?


Solution

  • Update In the 0.11.0 release, applyFrom(uri) was removed.

    You should now instead use:

    apply {
        from("dir/myfile.gradle")
    }
    

    Old answer

    With Groovy build scripts you can do something like apply from: 'dir/myfile.gradle' where dir/myfile.gradle is a file containing your shared repositories block.

    In a similar fashion with Gradle Script Kotlin (at least with 0.4.1), you can use the applyFrom(script: Any) method.

    build.gradle.kts

    applyFrom("dir/myfile.gradle")
    

    If you need to apply it from a subproject you could do something like:

    applyFrom("${rootProject.rootDir}/dir/myfile.gradle")