Freemarker giving unresolved reference for "TheApp" object - kotlin

I'm following this guide but "TheApp" portion is highlighted red and I get a blank webpage as output. Do I need to import some library for this to get recognized?
Also if I'm not passing any data into the template can I leave the 'model' as a blank string?
My kotlin code:
import io.ktor.server.netty.*
import io.ktor.routing.*
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.server.engine.*
import freemarker.cache.*
import io.ktor.freemarker.*
import io.ktor.freemarker.FreeMarkerContent
fun main(args: Array<String>) {
embeddedServer(Netty, 3000) {
install(FreeMarker) {
templateLoader = ClassTemplateLoader(TheApp::class.java.classLoader, "templates")
}
routing {
get("/") {
call.respond(FreeMarkerContent("templ.ftl", "", "e"))
}
}
}.start(wait = true) }
gradle portion:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
}
group 'rest-group'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
ext.ktor_version = '1.1.3'
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "io.ktor:ktor:$ktor_version"
compile "io.ktor:ktor-gson:$ktor_version"
compile "io.ktor:ktor-freemarker:$ktor_version"
compile "io.ktor:ktor-html-builder:$ktor_version"
compile "io.ktor:ktor-server-netty:$ktor_version"
compile "io.ktor:ktor-auth:$ktor_version"
compile "io.ktor:ktor:$ktor_version"
compile "ch.qos.logback:logback-classic:1.2.3"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

'TheApp' needs to be replaced with 'this'. 'this' picks up the instance of the application being run for ktor.
'model' needs to be a mapOf reference type even if the values aren't being used.

Related

androidx.compose.ui.window.MenuBar cannot be found

I am trying to use the androidx.compose.ui.window.MenuBar from Jetbrain Tutorial but the problem is that when I try to use it in my project it does not show up.
Here is the problem:
It only offers me to import from those 2 locations but when I use the import it does not complain or resolve the error.
build.gradle.kts
import org.jetbrains.compose.compose
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.6.10"
id("org.jetbrains.compose") version "1.1.0"
}
group = "me.whate"
version = "1.0"
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
dependencies {
implementation(compose.desktop.currentOs)
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "Test"
packageVersion = "1.0.0"
}
}
}
I have tried to invalidate caches, etc but no success so far.
Anybody has any suggestion?
You have to modify it like this:
#OptIn(ExperimentalComposeUiApi::class)
fun main() = application {
Window(onCloseRequest = ::exitApplication) {
var text by remember { mutableStateOf("Hello, World!") }
DesktopMaterialTheme {
MenuBar {
// your menubar code here
}
Button(onClick = {
text = "Hello, Desktop!"
}) {
Text(text)
}
}
}
}
and completely remove the App() function
Prefix MenuBar with FrameWindowScope, e.g. FrameWindowScope.MenuBar

How to fix `Expression 'jar' cannot be invoked as a function` error in gradle file?

I have the following gradle file. I'm getting a Expression 'jar' cannot be invoked as a function error on the line where jar is used (jar {). How can I fix this?
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.6.21"
java
application
}
group = "me.talha"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
application {
mainClass.set("MainKt")
}
jar {
manifest {
attributes 'Main-Class': 'MainKt'
}
}
Turns out I did not need to add any of the jar stuff. I can simply use the gradle distZip task which generates the needed jars and executable scripts and all works fine.

How can I customize a KotlinCompile task with a Gradle Kotlin buildSrc plugin?

I'm trying to simplify some of my Gradle builds. A typical build.gradle.kts looks like
plugins {
base
idea
java
id("biz.aQute.bnd.builder")
kotlin("jvm")
id("org.jetbrains.dokka")
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.osgi:osgi.annotation:7.0.0")
implementation("org.osgi:osgi.cmpn:7.0.0")
implementation("org.osgi:osgi.core:7.0.0")
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
}
tasks.compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
tasks.compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
version = "0.0.0"
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks.jar {
archiveBaseName.set("project.name.api")
}
tasks.named<Test>("test") {
useJUnitPlatform()
}
I'm trying to reduce repetition, specifically with the tasks.compileKotlin and tasks.compileTestKotlin blocks.
I've read that I should use the buildSrc folder for defining simplifications like this. The structure of the folder is
buildSrc/
src/
main/
kotlin/
Example.kt
build.gradle.kts
Example.kt
import org.gradle.kotlin.dsl.KotlinBuildScript
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin
//import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
fun KotlinBuildScript.configureModule() {
tasks.named<KotlinCompile>("compileKotlin") {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
}
buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
But this setup just results in an Unresolved reference: kotlin error during the build.
I can't figure out what I need to change where in order to successfully reference the KotlinCompile class. I tried specifying the kotlin plugin, idea plugin, and tried adding dependencies to kotlin in the buildSrc/build.gradle.kts, but I haven't been able to find any examples on github or google on how to set this up.
I'd like to do the same for most of the build script as well, not just the KotlinCompile task, so any guidance on what or how I should search for these other task types would be appreciated.
Looks like you're creating an extension function on KotlinBuildScript which may or may not be the issue. Regardless, there's no need for extension functions since you can just use the DSL directly.
Gradle calls the shared build logic convention plugins: https://docs.gradle.org/current/samples/sample_convention_plugins.html#compiling_convention_plugins
So your Example.kt would become kotlin-conventions.gradle.kts:
// kotlin-conventions.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
}
Then inside your main Gradle build file, apply the convention plugin:
// build.gradle.kts
plugins {
`kotlin-conventions`
}
Credit goes to Francisco Mateo's answer, this exists only for a complete final product
One additional problem I was having, was that I was trying to use kotlin-gradle-plugin:1.4.31 in by buildSrc/build.gradle.kts file. There's some weird bug there, but downgrading slightly to 1.4.30 resolved the contributing issue.
My final configuration looks like
buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
dependencies {
implementation("biz.aQute.bnd.builder:biz.aQute.bnd.builder.gradle.plugin:5.3.0")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30")
implementation("org.jetbrains.dokka:dokka-gradle-plugin:1.4.20")
}
Each of the external plugins required in the subsequent plugin file need to be specified as dependencies in the buildSrc/build.gradle.kts file, as specified in the Applying an external plugin in precompiled script plugin section from the link in Francisco's answer.
The plugin file also needs the .gradle.kts extension (if using Kotlin), so my buildSrc/src/main/kotlin/hummingbird-conventions.gradle.kts file looks like
plugins {
base
idea
java
id("biz.aQute.bnd.builder")
id("org.jetbrains.dokka")
kotlin("jvm")
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.osgi:osgi.annotation:7.0.0")
implementation("org.osgi:osgi.cmpn:7.0.0")
implementation("org.osgi:osgi.core:7.0.0")
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
named<Test>("test") {
useJUnitPlatform()
}
}
And I could apply this in the subproject's build.gradle.kts file like
plugins {
`hummingbird-conventions`
}
where the plugin id is the file name in the buildSrc src folder.

Kotlin with gradle build system cant add JRAW compile "net.dean.jraw:JRAW:1.1.0"

I am all new to Kotlin and gradle and wanted to create a reddit scraper app for fun.
Cant get the guide from this page to work.
https://mattbdean.gitbooks.io/jraw/content/quickstart.html
The guide tells me to put this in the gradle file:
repositories {
jcenter()
}
dependencies {
compile "net.dean.jraw:JRAW:$jrawVersion"
}
But when i put that in my buid.gardle.kts file the ide reports an error
See image below of where the error occurs. The error message is Unexpected tokens.
Found the answer to my question, simply had to add in the variable in the gradle file...
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
var jrawVersion = "1.1.0" //needed to add this in obviously
plugins {
kotlin("jvm") version "1.4.21"
application
}
group = "me.jeppe"
version = "1.0"
repositories {
mavenCentral()
jcenter()
}
dependencies {
testImplementation(kotlin("test-junit"))
compile ("net.dean.jraw:JRAW:$jrawVersion")
}
tasks.test {
useJUnit()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "1.8"
}
application {
mainClassName = "MainKt"
}

Kotlin: How to create a runnable jar?

I'm trying to create a runnable jar with Kotlin.
My gradle.build is this:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
group 'com.github.dynamik'
version '1.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'kotlin'
mainClassName = "interpreter.Repl"
repositories {
mavenCentral()
maven { setUrl("https://dl.bintray.com/hotkeytlt/maven") }
}
configurations {
ktlint
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'com.github.h0tk3y.betterParse:better-parse-jvm:0.4.0-alpha-3'
// https://mvnrepository.com/artifact/junit/junit
testCompile group: 'junit', name: 'junit', version: '4.4'
ktlint "com.github.shyiko:ktlint:0.31.0"
implementation 'com.github.ajalt:clikt:1.7.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
run {
standardInput = System.in
}
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
(As it stands, when I do ./gradlew run, everything works as expected.)
I'm reading an article here on how to proceed, and it says to do: java -jar <MY_PROJECT_NAME>.jar.
I don't quite understand this -- where do we run this? I tried running it from my project root and I got an error:
Error: Unable to access jarfile <my_jarname>.jar
As of Gradle 5.4.1, a build.gradle.kts would need a section like this:
tasks.register<Jar>("uberJar") {
archiveClassifier.set("uber")
manifest {
attributes(
"Main-Class" to "mytest.AppKt",
"Implementation-Title" to "Gradle",
"Implementation-Version" to archiveVersion
)
}
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
Ok, I figured it out :)
So, the way to create a jar is to go: ./gradlew build. This creates a jar in build/libs.
The problem is, when running that jar, one would run into an exception about java.lang.intrinsics because the kotlin stdlib hasn't been packed into the jar.
While there is a way to manually accomplish that, I found the easiest solution is to simply use the shadowjar plugin.
My build.gradle ended up looking like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
group 'com.github.dynamik'
version '1.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'kotlin'
apply plugin: 'java'
mainClassName = "interpreter.Repl"
repositories {
mavenCentral()
jcenter()
maven { setUrl("https://dl.bintray.com/hotkeytlt/maven") }
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
configurations {
ktlint
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'com.github.h0tk3y.betterParse:better-parse-jvm:0.4.0-alpha-3'
// https://mvnrepository.com/artifact/junit/junit
testCompile group: 'junit', name: 'junit', version: '4.4'
ktlint "com.github.shyiko:ktlint:0.31.0"
implementation 'com.github.ajalt:clikt:1.7.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0'
}
apply plugin: 'com.github.johnrengelman.shadow'
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
run {
standardInput = System.in
}
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
When using a Kotlin main class you need to add a Kt at the end of the class name while referencing it on MANIFEST.
So, if your main class is called interpreter.Repl, use:
jar {
manifest {
attributes 'Main-Class': 'interpreter.ReplKt'
}
}
instead of
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}