Skip to content

Instantly share code, notes, and snippets.

@rubencaro rubencaro/setup_kotlin.md
Last active Dec 21, 2018

Embed
What would you like to do?
Kotlin/Java Installation Guide

Kotlin/Java installation guide

These are my notes, not a generic solution. They are not meant to work anywhere outside my machines. Update version numbers to whatever are the current ones while you do this.

Install asdf and some plugins, then install Java, Kotlin & Gradle

asdf lives in https://github.com/asdf-vm/asdf

Follow its installation instructions, which at the moment of writing were:

cd
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.6.0

# For Ubuntu or other linux distros
echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
echo '. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc

On a new terminal, install Java, Kotlin and Gradle plugins:

asdf plugin-add java
asdf plugin-add kotlin
asdf plugin-add gradle

Then install Java, Kotlin and Gradle:

asdf install java openjdk-11
asdf install kotlin 1.3.10
asdf install gradle 5.0

Set some global versions if you find yourself needing them:

asdf global java openjdk-11
asdf global kotlin 1.3.10
asdf global gradle 5.0

Set JAVA_HOME

If you use tools that depend on JAVA_HOME being set, you may need this into your .bashrc, right after any asdf related stuff:

function asdf_update_java_home {
  local current
  if current=$(asdf current java); then
    local version=$(echo $current | cut -d '(' -f 1) 
    export JAVA_HOME=$(asdf where java $version)
  else
    echo "No java version set. Type 'asdf list-all java' for all versions."
  fi
}
asdf_update_java_home

Use gradle to control dependencies

You may run gradle init on a fresh new project to get a base set of files to play with. A simple build.gradle.kts file including JDK8 extensions and JUnit5 may look like this:

/*
 * This file contains a sample Kotlin application project to get you started.
 */

plugins {
    // Apply the Kotlin JVM plugin to add support for Kotlin on the JVM
    kotlin("jvm") version "1.3.10"

    // Apply the application to add support for building a CLI application
    application
}

repositories {
    // Use jcenter for resolving your dependencies.
    jcenter()
}

dependencies {
    // Use the Kotlin JDK 8 extended standard library
    implementation(kotlin("stdlib-jdk8"))

    // Use the Kotlin JUnit 5 integration
    testImplementation(kotlin("test-junit5"))
}

application {
    // Define the main class for the application (note the ending 'Kt')
    mainClassName = "kt_sample.AppKt"
}

A more complex example, this time a build.gradle file including both JUnit4 and JUnit5, AkkaHttp and some TDD tricks:

plugins {
    id 'java'
    id 'application'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'com.typesafe.akka:akka-http_2.12:10.1.5'
    implementation 'com.typesafe.akka:akka-stream_2.12:2.5.12'

    testImplementation 'junit:junit:4.12'
    testRuntime "org.junit.vintage:junit-vintage-engine:5.3.2"
    testCompile "org.junit.jupiter:junit-jupiter-api:5.3.2"
    testRuntime "org.junit.jupiter:junit-jupiter-engine:5.3.2"

    testImplementation 'com.typesafe.akka:akka-http-testkit_2.12:10.1.5'
}

mainClassName = 'akka_java_sample.App'

test {
    dependsOn "cleanTest" // force test execution
    useJUnitPlatform()
    testLogging {
        exceptionFormat = 'full'
		events "passed", "skipped", "failed"
        showStandardStreams = true
	}
}

To know available tasks just run gradle tasks. To test run gradle test, to run gradle run, etc.

Sample setup

Take a look at some code samples of this configuration:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.