Build and run Kotlin from command line using gradle
Install Kotlin if it’s not installed yet.
Install gradle if it’s not installed yet. This is for installing gradle on mac os.
brew install gradle
Create a root folder for this sample project, and create directories for Kotlin classes and Kotlin test classes.
mkdir gradle_example cd gradle_example mkdir -p src/test/kotlin/com/example mkdir -p src/main/kotlin/com/example/math
Create build.gradle at the root folder gradle_example, this is the configuration file to tell gradle command line tool how to build and run things in this gradle_example folder.
buildscript { ext.kotlin_version = '1.2.60' repositories { mavenCentral() jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() jcenter() } } apply plugin: 'kotlin' apply plugin: 'application' dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" testImplementation("junit:junit:4.12") } sourceSets { main.kotlin.srcDirs += 'src/main/kotlin' main.java.srcDirs += 'src/main/java' } // This tells gradle to execute src/test/kotlin/com/example/Hello.kt // when 'gradle run' is entered on the command line at the root folder mainClassName = 'com.example.HelloKt' // This tells gradle to execute src/test/kotlin/com/example/Double.kt // when 'gradle getDouble' entered on the command line at the root folder // If there are more than one args needed, enter them like this: args "one", "two", "3" // If the class name is Double.kt, it need to be specified as DoubleKt task getDouble(type: JavaExec) { classpath sourceSets.main.runtimeClasspath main = "com.example.DoubleKt" args "5" } // Another task, just like the getDouble above task getSquare(type: JavaExec) { classpath sourceSets.main.runtimeClasspath main = "com.example.SquareKt" args "5" } // Another task, just like the getDouble above task getRandom(type: JavaExec) { classpath sourceSets.main.runtimeClasspath main = "com.example.GetRandomKt" }
Try these gradle commands from the root folder gradle_example. We have defined the tasks but haven’t create any Kotlin code for these tasks. Hence most of them should fail until the following files are created.
gradle clean gradle build gradle tasks --all gradle run gradle test gradle getDouble gradle getSquare gradle getRandom gradle run getDouble getSquare getRandom
Create src/test/kotlin/com/example/Hello.kt
package com.example fun main(args: Array) { println("Hello Gradle!") println("Build : gradle build") println("Clean : gradle clean") println("List tasks : gradle tasks --all") println("Run main : gradle run") println("Run test : gradle test") println("Run getDouble : gradle getDouble") println("Run getSquare : gradle getSquare") println("Run getRandom : gradle getRandom") println("Run mulitasks : gradle run getDouble getSquare getRandom") }
Create src/test/kotlin/com/example/math/MyMathUtil.kt, this Kotlin class will be imported into the other Kotlin classes.
package com.example.math import java.util.Random class MyMathUtil { fun square(n : Int) : Int { return n*n } fun double(n : Int) : Int { return n*2 } fun randomInt() : Int { return Random().nextInt(); } }
Create src/test/kotlin/com/example/math/Double.kt
package com.example import com.example.math.MyMathUtil fun main(args : Array) { val num = if (args.isNotEmpty()) args[0].toIntOrNull() else null if (num == null) { println("Please provide a number!") return } val doubleOfNum = MyMathUtil().double(num) println("The double of $num is $doubleOfNum") }
Create src/test/kotlin/com/example/math/Square.kt
package com.example import com.example.math.MyMathUtil fun main(args : Array) { val num = if (args.isNotEmpty()) args[0].toIntOrNull() else null if (num == null) { println("Please provide a number!") return } val squareOfNum = MyMathUtil().square(num) println("The square of $num is $squareOfNum") }
Create src/test/kotlin/com/example/math/GetRandom.kt
package com.example import com.example.math.MyMathUtil fun main(args : Array) { println("Here is a random number: ${MyMathUtil().randomInt()}") }
Create src/test/kotlin/com/example/MyMathUtilTest.kt
package com.example import org.junit.Assert.assertEquals import org.junit.Test import com.example.math.MyMathUtil class MyMathUtilTest { @Test fun testSquare() { assertEquals(100, MyMathUtil().square(10)) } @Test fun testDouble() { assertEquals(20, MyMathUtil().double(10)) } @Test fun testRandom() { val randomInt1 = MyMathUtil().randomInt() val randomInt2 = MyMathUtil().randomInt() assertEquals(true, randomInt1 != randomInt2) } @Test fun failTest() { assertEquals(1000, MyMathUtil().square(10)) } }
Now, if you enter gradle run getDouble getSquare getRandom
from the command line, it should print the following. If you just want to run getDoble, enter gradle getDouble
. Any news changes in the corresponding Kotlin file should be reflected, no need to do gradle build
every time the Kotlin code is updated.
> Task :run Hello Gradle! Build : gradle build Clean : gradle clean List tasks : gradle tasks --all Run main : gradle run Run test : gradle test Run getDouble : gradle getDouble Run getSquare : gradle getSquare Run getRandom : gradle getRandom Run mulitasks : gradle run getDouble getSquare getRandom > Task :getDouble The double of 5 is 10 > Task :getSquare The square of 5 is 25 > Task :getRandom Here is a random number: -2032204043 BUILD SUCCESSFUL in 1s 5 actionable tasks: 4 executed, 1 up-to-date
Search within Codexpedia
Search the entire web