How to restrict kotlin from using java api which is announced above jdk1.6 - kotlin

My compilation environment is jdk1.8 and runtime environment is jdk1.6. java plugin of gradle has sourceCompatibility attribute. It is valid for java project.
For example: when sourceCompatibility=1.6, compiler will report error if I use the api such as Paths which is from jdk1.7.
but sourceCompatibility attribute doesn't work for kotlin project. I understand it is out of java plugin's scope. But I am strange whether kotlin plugin of gradle hasn't similar attribute. (jvmTarget attribute is 1.6 by default, it does't prevent me from using jdk1.7 api)
=== my code ===
kotlin code:
fun main(args: Array<String>) {
val pp = Paths.get("/tmp")
... ...
}
I want kotlin's compiler to report errors, but the compilation is successful,
parent build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.0"
}
}
subprojects {
apply plugin: "java"
compileJava.sourceCompatibility=1.6
compileJava.targetCompatibility=1.6
compileJava.options.encoding = 'UTF-8'
// For ubuntu
compileJava.options.bootClasspath = '/usr/lib/jvm/java-6-oracle/jre/lib/rt.jar'
}
child kotlin project build.gradle:
apply plugin: 'application'
apply plugin: 'kotlin'
mainClassName = 'net.cat.ApplictionKt'
version='1.0'
jar {
manifest {
attributes 'Implementation-Title': 'xxxx',
'Implementation-Version': version,
'Main-Class': mainClassName
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.0"
compile "org.jetbrains.kotlin:kotlin-reflect:1.1.0"
... ...
}

You can set the jdkHome property of the Kotlin compiler:
compileKotlin {
kotlinOptions {
jdkHome = '[path to 1.6 JDK]'
}
}

Related

Difficulty moving Gradle configuration to an external script

I am trying to move some parts of my Gradle build script to an external configuration file that can be shared among projects. Here is an example with the Detekt plugin:
Current Code
build.gradle.kts (condensed to only the relevant parts)
plugins{
id("io.gitlab.arturbosch.detekt").version("1.19.0-RC1")
}
...
detekt{
...
}
What I'm trying to do
build.gradle.kts
apply(File("common.gradle.kts"))
common.gradle.kts
plugins {
id("io.gitlab.arturbosch.detekt").version("1.19.0-RC1")
}
detekt{
...
}
but when I do this I get this error:
<my_project>\common.gradle.kts:7:1: Unresolved reference: detekt
So the plugin section doesn't appear to be doing anything. And just to be clear, this plugin does not need anything in dependencies section, it works fine inside build.gradle.kts with only the plugin declaration.
Why doesn't this work?
I am using it with normal Groovy Gradle scripts like this:
# detekt.gradle
apply plugin: 'io.gitlab.arturbosch.detekt'
tasks.detekt.jvmTarget = "1.8"
detekt {
buildUponDefaultConfig = true
input = files("${rootProject.projectDir}/app/src")
config = files("${rootProject.projectDir}/detekt.yml")
reports {
html.enabled = true
xml.enabled = true
}
}
# build.gradle
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath libs.android.gradle
classpath libs.kotlin.gradle
classpath libs.detekt
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
apply from: rootProject.file('detekt.gradle')
apply from: rootProject.file('ktlint.gradle')
apply from: rootProject.file('spotless.gradle')
...
}

kotlin-compiler-embeddable missing within gradle build

I am trying to setup a multiproject gradle/kotlin build and I am getting following error:
Could not determine the dependencies of task ':compileKotlin'.
> Could not resolve all files for configuration ':kotlinCompilerClasspath'.
> Cannot resolve external dependency org.jetbrains.kotlin:kotlin-compiler-embeddable:1.4.10 because no repositories are defined.
Required by:
project :
Strange thing is the empty project :.
My simplifiged build.gradle.kts looks like this:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
base
kotlin("jvm") version "1.4.10"
}
buildscript {
extra["kotlin_version"] = "1.4.10"
repositories {
jcenter()
mavenCentral()
}
}
subprojects {
apply(plugin = "kotlin")
repositories {
jcenter()
mavenCentral()
}
tasks.compileKotlin {
kotlinOptions.jvmTarget = "11"
}
tasks.compileTestKotlin {
kotlinOptions.jvmTarget = "11"
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:${rootProject.extra["kotlin_version"]}")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${rootProject.extra["kotlin_version"]}")
}
}
Do I need to duplicate the repositories within the buildscript section?
What is that is causing the error above?
It seems like your repositories { ... } configuration done in the subproject section is sufficient, but it is only applied to the supbrojects but not the root project itself (which has the Kotlin plugin applied, too, and whose task :compileKotlin is failing).
There are two ways to fix this.
First, you could move the repositories { ... } section from subprojects { ... } to a new block allprojects { ... } (thus applied to the root project as well).
Or, if you don't actually need the Kotlin plugin in the root project (i.e. you don't have Kotlin code there), you can add .apply(false) to your plugin declaration:
plugins {
kotlin("jvm").version("1.4.10").apply(false)
}

taskdef class com.sun.tools.xjc.XJCTask cannot be found

I am trying to generate classes from an XSD file in a Springboot project with multiple modules. I tried to follow the guide given here.
I have the below config in my root build.gradle
buildscript {
ext {
springBootVersion = '1.3.5.RELEASE'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
maven { url "https://mvnrepository.com/artifact" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
//####################### XJC - JDK 1.7/1.8 ####################
classpath 'com.github.jacobono:gradle-jaxb-plugin:1.3.5'
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
//####################### XJC - JDK 1.7/1.8 ####################
apply plugin: 'com.github.jacobono.jaxb'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
maven { url "https://mvnrepository.com/artifact" }
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
}
and below config in my module's build.gradle
dependencies {
compile project(':appCommon')
compile("org.springframework.boot:spring-boot-starter")
}
//####################### XJC - JDK 1.7/1.8 ####################
jaxb {
xjc {
xsdDir = "schemas/v1.1"
generatePackage = "com.test.domain.v1_1"
}
}
when I run the xjc task from my IntelliJ on my module, I am getting an exception as below
taskdef class com.sun.tools.xjc.XJCTask cannot be found
using the classloader AntClassLoader[]
Any help with what is going wrong is appreciated
Looks like you're missing the jaxb dependencies. In your module's build.gradle add the following:
dependencies {
jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.7-b41'
jaxb 'com.sun.xml.bind:jaxb-impl:2.2.7-b41'
jaxb 'javax.xml.bind:jaxb-api:2.2.7'
}

Can't find spock inside of intellij

I am pretty new to intellij and spock.
I am adding spock testing to my spring boot project using gradle. Here's my build.gradle:
buildscript {
ext {
springBootVersion = '1.4.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
jar {
baseName = 'theta-server'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.spockframework:spock-core:1.0-groovy-2.4')
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
And so I added a spock test such:
package com.heavyweightsoftware.theta.server.controller
/**
*
*/
class ThetaControllerTest extends spock.lang.Specification {
def "Theta"() {
}
}
But I can't run the test inside my intellij environment because it says "cannot resolve symbol 'spock'"
The build runs fine directly.
At times, IJ may not update the project classpath after some changes. It seems that this happens for both Maven POM files as well as Gradle build files. Usually, this is easily solved by forcing a sync with the Reimport all gradle/maven projects button from the appropriate tool window (maven, gradle):

Kotlin cli application fails to work after updating to kotlin 1.0.0-beta

I boiled the problem down to the following minimal setup - just a hello world. This one works:
https://github.com/ligi/MinimalKotlinCommandLine
when I switch to kotlin:1.0.0-beta-4584 and run ( gradle clean run ) I am getting the following error:
Error: Could not find or load main class minimalkotlincommandline.MinimalkotlincommandlinePackage
with this build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.0-beta-4584'
}
}
apply plugin: "kotlin"
apply plugin: 'application'
mainClassName = "minimalkotlincommandline.MinimalkotlincommandlinePackage"
repositories {
mavenCentral()
}
dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.0-beta-4584'
}
and this Main.kt:
package minimalkotlincommandline
fun main(args: Array<String>) {
println("Hello world ")
}
Change mainClassName to
mainClassName = "minimalkotlincommandline.MainKt"
Since Kotlin version M14 top-level naming changed from ${Package}Package to ${File}Kt. More info in documentation.