My experience with the Gralde Kotlin DSL is fairly limited - all of one build script which is the subject of this article.
If you want to simply see how a sample script looks, I have a sample github repo with just that here - https://github.com/bijukunjummen/cf-show-env
Just to compare:
1. Consider the way different plugins are applied with gradle:
1 2 3 4 5 6 7 8 | plugins { id "com.github.pivotalservices.cf-app" version "1.0.9" } apply plugin: 'kotlin" apply plugin: 'java' apply plugin: 'org.springframework.boot' apply from: 'gradle/gatling.gradle' |
An equivalent kotlin dsl is the following:
1 2 3 4 5 6 7 8 9 10 | plugins { id( "com.github.pivotalservices.cf-app" ).version( "1.0.9" ) } apply { plugin( "kotlin" ) plugin( "java" ) plugin( "org.springframework.boot" ) from( "gradle/gatling.gradle" ) } |
2. Adding project dependencies:
1 2 3 4 5 6 7 8 9 10 11 12 13 | dependencies { compile( 'org.springframework.boot:spring-boot-starter-actuator' ) compile( 'org.springframework.boot:spring-boot-devtools' ) compile( 'org.springframework.boot:spring-boot-starter-thymeleaf' ) compile( 'org.springframework.boot:spring-boot-starter-web' ) compile( 'com.google.guava:guava:19.0' ) compile( "org.webjars:bootstrap:3.3.7" ) compile( "org.webjars:jquery:3.1.1" ) compile( "io.prometheus:simpleclient:${prometheus_client_version}" ) compile( "io.prometheus:simpleclient_spring_boot:${prometheus_client_version}" ) compile( 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect' ) testCompile( 'org.springframework.boot:spring-boot-starter-test' ) } |
an equivalent code using kotlin DSL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | dependencies { val prometheus_client_version = "0.0.21" compile( "org.springframework.boot:spring-boot-starter-actuator" ) compile( "org.springframework.boot:spring-boot-devtools" ) compile( "org.springframework.boot:spring-boot-starter-thymeleaf" ) compile( "org.springframework.boot:spring-boot-starter-web" ) compile( "com.google.guava:guava:19.0" ) compile( "org.webjars:bootstrap:3.3.7" ) compile( "org.webjars:jquery:3.1.1" ) compile( "io.prometheus:simpleclient:${prometheus_client_version}" ) compile( "io.prometheus:simpleclient_spring_boot:${prometheus_client_version}" ) compile( "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect" ) testCompile( "org.springframework.boot:spring-boot-starter-test" ) } |
3. Configuring Plugins - I have a plugin which helps deploy applications to Cloud Foundry, and works off a configuration which looks like this, when expressed using normal gradle build:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | cfConfig { //CF Details ccHost = "api.local.pcfdev.io" ccUser = "admin" ccPassword = "admin" org = "pcfdev-org" space = "pcfdev-space" //App Details name = "cf-show-env" hostName = "cf-show-env" filePath = "build/libs/cf-show-env-0.1.3-SNAPSHOT.jar" path = "" domain = "local.pcfdev.io" instances = 2 memory = 1024 timeout = 180 //Env and services environment = [ "JAVA_OPTS" : "-Djava.security.egd=file:/dev/./urandom" , "SPRING_PROFILES_ACTIVE" : "cloud" ] cfService { name = "p-mysql" plan = "512mb" instanceName = "test-db" } cfUserProvidedService { instanceName = "mydb1" credentials = [ "jdbcUri" : "someuri1" ] } } |
this can now be configured in a typesafe way with full auto-completion support in IntelliJ the following way using Kotlin DSL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | configure< CfPluginExtension> { //CF Details ccHost = "api.local.pcfdev.io" ccUser = "admin" ccPassword = "admin" org = "pcfdev-org" space = "pcfdev-space" //App Details name = "cf-show-env" hostName = "cf-show-env" filePath = "build/libs/cf-show-env-1.0.0-M1.jar" path = "" domain = "local.pcfdev.io" instances = 2 memory = 1024 timeout = 180 //Env and services environment = mapOf( "JAVA_OPTS" to "-Djava.security.egd=file:/dev/./urandom" , "SPRING_PROFILES_ACTIVE" to "cloud" ) cfService(closureOf<CfService> { name = "p-mysql" plan = "512mb" instanceName = "test-db" }) cfUserProvidedService(closureOf<CfUserProvidedService> { instanceName = "myups" credentials = mapOf( "user" to "someuser" , "uri" to "someuri" ) }) } |
4. And finally a straight task:
1 2 3 4 5 6 7 8 9 | task "hello-world" { doLast { println( "Hello World" ) } } task showAppUrls(dependsOn: "cf-get-app-detail" ) << { print "${project.cfConfig.applicationDetail}" } |
looks more or less the same in Kotlin DSL:
1 2 3 4 5 6 7 8 9 10 | task( "hello-world" ) { doLast { println( "Hello World" ) } } task( "showAppUrls" ).dependsOn( "cf-get-app-detail" ).doLast { println(cfConfig); } |
I am excited about using Kotlin DSL to configure my gradle builds, there are a few quirks to keep in mind though - the Intellij support tends to be a little flaky, it took a few tries for the IDEA to start helping with the auto-completions, also I needed to google quite a bit and look at some of the sample projects in gradle kotlin dsl, all in all though this has an awesome potential.