Skip to content

Instantly share code, notes, and snippets.

@mitchwongho
Last active November 6, 2023 15:03
Show Gist options
  • Save mitchwongho/770993086b8f6c951ce8c7051134e4af to your computer and use it in GitHub Desktop.
Save mitchwongho/770993086b8f6c951ce8c7051134e4af to your computer and use it in GitHub Desktop.
A trivial Gradle build file for a Kotlin application

Introduction

This project is the result of wanting to create a Kotlin application and using the Gradle build system, built from the ground up i.e not using an IDE's Gradle plugin.

References

This is a list of references used in compiling this recipe:

Ingredience

I was working on macOS 10.13 (High Sierra) at the time, so details may vary on the platform you're working on or version of software currently available.

  • Java JDK8
  • VS Code ++ Gradle Language Support plugin ++ Kotlin plugin ++ Kotlin Language plugin
  • Gradle

Recipe

  • Work through the Creating New Gradle Builds guide using the Kotlin examples.
  • Create the Kotlin source folder src/main/kotlin to your project and create the HelloWorld.kt Kotlin file in it.
  • Edit the build.gradle.kt build file used in the Gradle guide to this one to include the Kotlin and application plugins and dependencies.
  • (Optional) Create a local.properties file to include the configuration kotlin.incremental=true.

Usage

To build and run the simple application, run the following for the CLI (root of the project): $ ./gradlew clean build run

/*
* This file was generated by the Gradle 'init' task.
*
* This is a general purpose Gradle build.
* Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/
*/
import org.jetbrains.kotlin.gradle.dsl.Coroutines
kotlin.experimental.coroutines = Coroutines.ENABLE
description = "A trivial Gradle build"
version = "1.0.0"
repositories {
mavenCentral()
}
plugins {
id("base")
`application`
kotlin("jvm") version "1.2.61"
}
application {
mainClassName = "HelloWorldKt"
}
dependencies {
compile(kotlin("stdlib-jdk8"))
testCompile(kotlin("test"))
testCompile(kotlin("test-junit"))
}
tasks.create<Copy>("copy") {
description = "Copies sourtces to the destination dir"
group = "Custom"
from("src")
into("dest")
}
tasks.create<Zip>("zip") {
description = "Archives sources in to a zip file"
group = "Archive"
from("src")
setAppendix("demo")
setBaseName("basic")
setVersion("1.1")
setExtension("zip")
// or used setArchiveName("basic-demo-1.0.zip")
}
fun main(args: Array<String>) {
MyFirstClass("Hello, My First Class").out()
}
class MyFirstClass(val message: String) {
fun out() {
println(message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment