Reputation: 3225
Ok, so I'm new to Gradle and Kotlin and I am having a hard time understanding how things glue together here...
I need to configure a project that should run on Java 7 (client limitations -_-) and I want to use Kotlin with it.
Right now I have the following build.gradle
file that is working but I want to ask a few things that I couldn't find anywhere else:
buildscript {
ext {
springBootVersion = '1.5.15.RELEASE'
kotlin_version = '1.1.1'
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
group = 'com.springkotlin'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-security')
compile('com.onelogin:java-saml:2.3.0')
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
testCompile group: 'javax.inject', name: 'javax.inject', version: '1'
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.6"
}
}
Now the questions:
I have tried using kotlin_version = '1.2.70'
(released last few days!) and I got the error KotlinPluginWrapper : Unsupported major.minor version 52.0
. I'm guessing then this is due to Kotlin 1.2.X only being able to "compile" (is that the word?) with Java 8+. Is that right? Is 1.1.1
the right version to use here or is there a way to use 1.2.70
that would work with Java 7? Will I be missing a lot of stuff for using it?
I want to understand the 3 kotlin stuff I had to setup on the script. Correct me please:
kotlin-gradle-plugin
: Is used to define which version of Kotlin I will be using(?)apply plugin: 'kotlin'
: As far as I know from Gradle, this should add tasks to work with Kotlin but running gradle tasks
I didn't see anything different... So what is it really for?kotlin-stdlib-jdk7
: I'm guessing this is Kotlin lib of functions, classes, etc. What I don't understand though is the difference between stdlib
and stdlib-jdk7
. The documentation says it contains "addition extension functions". But which ones? Also, should I define a version for this guy? Or does it automatically picks up the kotlin-gradle-plugin
version?Thanks in advance,
Upvotes: 2
Views: 4723
Reputation: 23105
Currently the compiler of the Kotlin language requires JDK 8 to run. A project compiled with Kotlin can target any Java starting from Java 6.
A recipe to setup Gradle build of a project that runs on Java 7 is following:
jvmTarget = "1.6"
in kotlinOptions
jdkHome
in kotlinOptions
sourceCompatibility
, targetCompatibility
convention properties of the Java pluginoptions
of all java compile tasks:
isFork = true
forkOptions.javaHome = "<path to JDK 7>"
executable
as "<path to JDK 7>/bin/java"
The full sample:
sourceCompatibility = 1.7
targetCompatibility = 1.7
def JDK_7 = System.getenv("JDK_7") // or some other way to get path to JDK 7 installation
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
kotlinOptions {
jvmTarget = "1.6"
jdkHome = JDK_7
}
}
tasks.withType(JavaCompile) {
options.fork = true
options.forkOptions.javaHome = file(JDK_7)
}
test {
executable = "$JDK_7/bin/java"
}
Upvotes: 4
Reputation: 8361
Kotlin can target either Java 6 or Java 8 and I don't think this has changed. However, it is quite likely that the default has changed from Java 6 to Java 8, so try as suggested here:
compileKotlin {
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
kotlinOptions {
jvmTarget = "1.6"
apiVersion = "1.2"
languageVersion = "1.2"
}
}
The version of kotlin-gradle-plugin
is the version of the Kotlin compiler used to compile your code. The version of the stdlib is the version of your runtime library. It is highly recommended to use the same version here.
The apply plugin: kotlin
adds some tasks under the hood - just continue using the java tasks like gradle assemble
, gradle build
and gradle run
as they will invoke the kotlin specific tasks
kotlin-stdlib-jdk7
adds very little value - unless you use features of the java library that were introduced in java 7 and that have extensions from Kotlin's stdlib, you'll be fine to just use the default stdlib (which targets Java 6 and is a dependency of kotlin-stdlib-jdk7
anyways).
Upvotes: 2