Skip to content

Instantly share code, notes, and snippets.

@rubencaro
Last active April 9, 2021 19:13
Show Gist options
  • Save rubencaro/274e6c88434fd706002d54dd6b31c8fc to your computer and use it in GitHub Desktop.
Save rubencaro/274e6c88434fd706002d54dd6b31c8fc to your computer and use it in GitHub Desktop.
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 {
  asdf current java 2>&1 > /dev/null
  if [[ "$?" -eq 0 ]]
  then
      export JAVA_HOME=$(asdf where java)
  fi
}

function prompt_command {
  __vte_prompt_command  # put here whatever previous PROMPT_COMMAND was there before
  asdf_update_java_home
}

export PROMPT_COMMAND=prompt_command

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