Caleb
Caleb

Reputation: 35

Ktor Application fatJar/shadowJar not including resources directory and static info

I have a ktor application and i want to package a fatJar so i can run it easily but when using Ktors built in fatJar gradle task or the shadow plugins shadowJar task they arent including the resources directory. I have looked into the jars with 7zip and there is no resources at all.

my build.gradle.kts:

val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project

plugins {
    kotlin("jvm") version "1.9.24"
    id("io.ktor.plugin") version "2.3.11"
    id("com.github.johnrengelman.shadow") version "8.1.1"
}

group = "dev.bluemethyst.web"
version = "0.0.2"

application {
    mainClass.set("dev.bluemethyst.kassword.ApplicationKt")

    val isDevelopment: Boolean = project.ext.has("development")
    applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.ktor:ktor-server-core:$ktor_version")
    implementation("io.ktor:ktor-server-netty:$ktor_version")
    implementation("io.ktor:ktor-client-core:$ktor_version")
    implementation("io.ktor:ktor-client-apache:$ktor_version")
    implementation("ch.qos.logback:logback-classic:$logback_version")
    implementation("com.google.code.gson:gson:2.11.0")
}

ktor {
    fatJar {
        archiveFileName.set("fat.jar")
    }
}

sourceSets {
    main {
        resources {
            srcDir("src/main/resources")
            include("**/*")
            include("static/html/*")
        }
    }
}

full repository is at https://github.com/Bluemethyst/Kassword

Upvotes: 0

Views: 206

Answers (1)

Caleb
Caleb

Reputation: 35

I had to remove some code that pointed towards an html file that didnt exist in the built jar

get("/") {
            val htmlContent = File("src/main/resources/static/html", "index.html").readText()
            call.respondText(htmlContent, ContentType.Text.Html)
        }

and changed/added this

staticResources("/", "static", index = "html/index.html")

have a looked at the above linked repo to see the full change

Upvotes: 0

Related Questions