I'm writing a simple console app which should to generate some kotlin code on execution. I faced a strange problem I can't add KotlinPoen dependency. My build.gradle:
plugins {
id 'java-library'
id 'kotlin'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation group: 'com.squareup', name: 'kotlinpoet', version: '1.7.2'
}
But in PoetApp.kt import failed with message Unresolved reference: squareup:
import com.squareup.kotlinpoet.FunSpec
object PoetApp {
#JvmStatic
fun main(vararg param: String) {
val main = FunSpec.builder("main")
.addCode("""
|var total = 0
|for (i in 0 until 10) {
| total += i
|}
|""".trimMargin())
.build()
}
}
You have declared Java compatibility with version 1.7, but this version of kotlinpoet will work only with 1.8 and higher, see the library gradle file and also some compatibility issue solved here.
So your build.gradle should look like this:
plugins {
id 'java-library'
id 'kotlin'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation group: 'com.squareup', name: 'kotlinpoet', version: '1.7.2'
}
Related
my team got a kotlin project with JavaFX as frontEnd. The problem we have is using jlink to produce a package.
Here is my build.gradle for the project.
Could anyone help me take a look at how to change this?
ps: I have already tried with
command-line javac
jlink from the intellij
create a new Main method and use the shadow plugin for jlink.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
}
}
plugins {
id "java"
id "application"
id "org.jetbrains.kotlin.jvm" version "1.6.10"
id "org.openjfx.javafxplugin" version "0.0.12"
id "org.beryx.jlink" version "2.25.0"
// Json
id "org.jetbrains.kotlin.plugin.serialization" version "1.6.10"
}
group "com.yyil"
version "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
ext {
junitVersion = "5.8.1"
}
sourceCompatibility = "16"
targetCompatibility = "16"
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
application {
mainClass = 'com.yyil.noteapp.NewMain'
}
[compileKotlin, compileTestKotlin].forEach {
it.kotlinOptions {
jvmTarget = "16"
}
}
javafx {
version = "17.0.2"
modules = ["javafx.controls", "javafx.fxml", "javafx.web", "javafx.swing", "javafx.graphics", "javafx.base"]
}
dependencies {
implementation("org.controlsfx:controlsfx:11.1.0")
implementation("net.synedra:validatorfx:0.1.13") {
exclude(group: "org.openjfx")
}
implementation("org.kordamp.ikonli:ikonli-javafx:12.2.0")
implementation("eu.hansolo:tilesfx:11.48") {
exclude(group: "org.openjfx")
}
// JSON
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
// testing JavaFX
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
testImplementation("org.testfx:testfx-core:4.0.16-alpha")
testImplementation("org.testfx:testfx-junit5:4.0.16-alpha")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
// database (sqlite)
implementation "org.xerial:sqlite-jdbc:3.30.1"
// svg support
implementation("org.apache.xmlgraphics:batik-transcoder:1.14")
}
test {
useJUnitPlatform()
}
When I try to build it, I got errors like:
I am developing a plugin for PyCharm which uses custom PythonConsoleRunnerFactory.
Currently am trying to write unit tests to cover the created class and I have found at source code of PyCharm that there is PyEnvTestCase class that is used for console testing but I don't know how to include this class in my project.
I am using Gradle as a build tool. Here is my gradle build script:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
kotlin("jvm") version "1.3.61"
id("org.jetbrains.intellij") version "0.6.5"
}
group = "com.uriyyo.evaluate_async_code"
version = "1.8"
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
testImplementation("junit:junit:4.13")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.test {
useJUnit()
}
intellij {
version = project.properties["ideaVersion"].toString()
type = "PC"
pluginName = "evaluate-async-code"
downloadSources = project.properties["downloadIdeaSources"] == "true"
updateSinceUntilBuild = false
setPlugins("terminal", "python-ce")
}
Is there a way to include PyEnvTestCase to be available for tests?
So I was working on converting my build.gradle (Which uses Groovy) to a build.gradle.kts (Which uses Kotlin) and this is what I got so far:
Groovy Original:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
id 'com.github.johnrengelman.shadow' version '5.2.0'
}
group 'com.smushytaco'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven {
url 'https://papermc.io/repo/repository/maven-public/'
}
}
dependencies {
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.3.72'
//https://papermc.io/javadocs/paper/1.16/overview-summary.html
compileOnly group: 'com.destroystokyo.paper', name: 'paper-api', version: '1.16.1-R0.1-SNAPSHOT'
}
shadowJar {
getArchiveClassifier().set('')
project.configurations.implementation.canBeResolved = true
configurations = [project.configurations.implementation]
relocate 'kotlin', 'com.smushytaco.src.main.kotlin.com.smushytaco.plugin'
}
build.dependsOn shadowJar
Kotlin Conversion:
plugins {
java
kotlin("jvm") version("1.3.72")
id("com.github.johnrengelman.shadow") version("5.2.0")
}
group = "com.smushytaco"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven("https://papermc.io/repo/repository/maven-public/")
}
dependencies {
implementation("org.jetbrains.kotlin", "kotlin-stdlib", "1.3.72")
//https://papermc.io/javadocs/paper/1.16/overview-summary.html
compileOnly("com.destroystokyo.paper", "paper-api", "1.16.1-R0.1-SNAPSHOT")
}
tasks {
shadowJar {
archiveClassifier.set("")
project.configurations.implementation.get().isCanBeResolved = true
// Type Mismatch:
// Required: (Mutable)List<Configuration!>!
//Found: Array<NamedDomainObjectProvider<Configuration>>
configurations = [project.configurations.implementation]
relocate("kotlin", "com.smushytaco.src.main.kotlin.com.smushytaco.plugin")
}
build {
dependsOn(shadowJar)
}
}
I commented the error I'm getting with the Kotlin rewrite, why does it work with the original Groovy one and not with this one? How do I fix this error?
Use this:
configurations = listOf(project.configurations.implementation.get())
Recently I've been trying to make a annotation processor through Kotlin but I can't seem to get it to work. Everything compiles and I don't get any errors but when I check the contents of my jar file I don't see the resource I'm trying to create.
I've tried everything for hours and I'm really stuck so just looking for help :/ I do have one random class annotated to see if it would work but no luck.
The annotation class
#Retention(AnnotationRetention.RUNTIME)
#Target(AnnotationTarget.CLASS)
annotation class TestAnnotation
The processor class
#AutoService(TestAnnotation::class)
class TestAnnotationProcessor : AbstractProcessor() {
override fun process(annotations: MutableSet<out TypeElement>, environment: RoundEnvironment): Boolean {
this.processingEnv.filer.createResource(StandardLocation.CLASS_OUTPUT, "", "test.txt")
return true
}
override fun getSupportedSourceVersion() = SourceVersion.RELEASE_8
override fun getSupportedAnnotationTypes() = setOf(TestAnnotation::class.java.canonicalName)
}
My build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
plugins {
java
kotlin("jvm") version "1.3.50"
kotlin("kapt") version "1.3.50"
id("com.github.johnrengelman.shadow") version "5.1.0"
}
group = "com.example.test"
version = "1.0"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1")
implementation("com.google.auto.service:auto-service:1.0-rc6")
kapt("com.google.auto.service:auto-service:1.0-rc6")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType<ShadowJar> {
dependencies {
exclude(dependency("com.google.auto.service:auto-service:1.0-rc6"))
}
}
In order for your custom annotation processor to work, you will have to use it in your build script dependencies
dependencies {
kapt project(':processor-module') // or what ever your processor's module is named.
//OR
kapt 'gourp:artifact:version' // if your processor is published into a maven repository.
}
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]'
}
}