Skip to content

Building Java and Kotlin projects

Estimated time to read: 2 minutes

Basic Java Projects

Creating a build

  • Create a build file
  • Add the appropriate plugins
  • Override tasks and properties

Java Plugins

java

This is the base plugin for Java in Gradle. It adds basic tasks to the project such as clean, build and jar. It also follows Java conventions and expects to find source code in standard locations by default.

java-library

This plugin is used for building libraries over applications. It is used heavily in multi-project builds.

application

This plugin extends the Java plugin and gives us the ability to run the application from inside of Gradle and implicitly applies the distribution plugin.

Standard File Structure

- src
 - main
  - java
  - kotlin
  - resources
 - test
  - java
  - kotlin
  - resources

Configuring the File Structure (sourceSets)

Groovy
sourceSets {
 main {
  java {
   srcDir 'src/main'
  }
  resources {
   srcDir 'src/resources'
  }
  test {
   srcDir 'src/test'
  }
 }
}
Kotlin
sourceSets {
 main {
  java {
   setSrcDirs(listOf("src/main"))
  }
  test {
   setSrcDirs(listOf("src/test"))
  }
 }
}

Piping output to a file

gradle build -i > build.txt

Version Configuration

Using the version element within the build file, we can specify the version of our output.

Groovy

plugins {
 id 'java'
}

version = "1.0-SNAPSHOT" //Note the double quotes!

Kotlin

plugins {
 java
}

version = "1.0-SNAPSHOT"

Application Plugin

Using the application plugin will add tasks to the project.

gradle run will treat the Java code as an application and will run any main method inside of the application. However, the mainClassName element needs to be set within the build file.

Gradle

plugins {
 id 'application'
}

mainClassName = "io.entityfour.fizzbuzz.runner"

Kotlin

plugins {
 application
}

mainClassName = "io.entityfour.fizzbuzz.runner"

Specifying Java versions / compatibility checks

The java plugin is highly configurable. This incudes targeting a specific build / version of the JDK / JRE.

java elements are agnostic to Groovy and Kotlin. The same elements and formatting will work for both.

java {
 sourceCompatability = JavaVersion.VERSION_1_8 // Set to a specific version of Java or can be set using a Constant.
 targetCompatability = JavaVersion.VERSION_1_8 // Same as a above, can be a specific version or Constant. If targetCompatability is not set, it is inferred by sourceCompatability. 
}

Javadoc

gradle javadoc will generate Javadocs for the current project

Creating extra Jar files

java {
 withJavadocJar() // Build the project and include a Javadoc jar file
 withSourcesJar() // Build the project and include a Sources jar file
}

Building Kotlin Code

If building Kotlin code, there are several changes that need to be made to the build file for it to be valid.

gradle.build.kts

plugins {
 application
 kotlin("jvm") version "1.3.31" // Enable the Kotlin plugin using the JVM to build
}

kotlin { // This replaces the java element
 sourceSets["main"].apply { // sourceSets are specified within the kotlin element
  kotlin.srcDir("src")
 }
 sourceSets["test"].apply {
  kotlin.srcDir("test/src")
 }
}

repositories { // Tell Gradle which repos to use to find any dependencies
 mavenCentral()
}

dependencies {
 implementation(kotlin("stblib-jdk8")) // Add the dependency for Kotlin's JDK8 Standard Library
}

// Tell Kotlin to use a specific version of JVM
tasks {
 compileKotlin {
  kotlinOptions.jvmTarget = "1.8"
 }
 compileTestKotlin {
  kotlinOptions.jvmTarget = "1.8"
 }
}