Search code examples
androidkotlingradlegradle-kotlin-dsl

Gradle Kotlin DSL Files - Separate Config Files


I'm currently in the process of migrating my groovy gradle files to kotlin. I have jacoco and sonarcloud integrated into my application. I have separate groovy gradle files for jacoco and sonar config and apply them to my build.gradle(:app) as follows:

apply from: '../jacoco/modules.gradle'
apply from: 'sonar.gradle'

The top of my sonar.gradle file looks like this:

apply plugin: "org.sonarqube"

sonarqube {
    properties {

I have tried converting this file to kotlin

e.g. apply(plugin = "org.sonarqube") but I get the error that sonarqube cannot be found. Is this possible to have configurations extracted to separate gradle kotlin dsl files the way it could be done with Groovy? If so, could someone shed some light on what to do next?

Thank you in advance.

EDIT I have added in the buildSrc folder structure which looks like this:

enter image description here and the contents of my build.gradle.kts as follows:

plugins {
    `kotlin-dsl`
    id("org.sonarqube") version "4.4.1.3373"
}

repositories {
    google()
    mavenCentral()
}

And the contents of my sonar.gradle.kts is as follows:

apply(plugin = "org.sonarqube")

sonarqube {
    
}

I know I have done something wrong here but I don't know what as sonarqube throws up an error.

If I reference sonarqube in build.gradle.kts it is picked up.

However I don't want that structure and if I try to add id("org.sonarqube") version "4.4.1.3373" into the sonar.gradle.kts file it give the following error:

Invalid plugin request [id: 'org.sonarqube', version: '4.4.1.3373']. 

Plugin requests from precompiled scripts must not include a version number. 
Please remove the version from the offending request and make sure the 
module containing the requested plugin 'org.sonarqube' is 
an implementation dependency of project ':buildSrc'.

Can someone please explain where I'm going wrong.


Solution

  • The way to do this now is to use a "precompiled script plugin".

    This is a standalone .gradle.kts file that you can include like a plugin in one of your regular build.gradle.kts files.

    This needs to be located in an included build, and the easiest way to set up an included build is to use the buildSrc folder, which is a default included build.

    To get this to work:

    1. Set up a buildSrc folder and place a build.gradle.kts file as follows:
    plugins {
        `kotlin-dsl`
    }
    
    repositories {
        mavenCentral()
    }
    

    This file should declare as dependencies any artifacts you need for your script plugin, including any JARs for external plugins (documentation), just as if you were including them for a regular Kotlin JVM application. (This also places them on the classpath of your whole build.)

    1. Write your script plugin in a file named my-first-kotlin-script-plugin.gradle.kts in the buildSrc/src/main/kotlin folder:
    plugins {
        // some useful plugins
    }
    
    tasks {
        // some fabulous tasks
    }
    
    1. Then you can include your new script plugin in regular build.gradle.kts files:
    plugins {
        `my-first-kotlin-script-plugin`
    }
    
    // use useful plugins and fabulous tasks