0

I build my ktor project with this command: gradle shadowJar --no-daemon, but it doesn't add my resources folder to the fat jar.

My build.gradle is this:

val ktor_version = "2.0.2"
val kotlin_version = "1.6.10"
val logback_version = "1.2.11"

plugins {
    application
    kotlin("jvm") version "1.6.10"
    id("com.github.johnrengelman.shadow") version "7.1.2"
}

group = "com.cstcompany"
version = "0.0.1"
application {
    mainClass.set("com.cstcompany.ApplicationKt")

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

repositories {
    mavenCentral()
    maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") }
}

dependencies {
    implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
    implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
    implementation("ch.qos.logback:logback-classic:$logback_version")
    testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")

    //FreeMarker
    implementation("io.ktor:ktor-server-freemarker:$ktor_version")
}

In IntelliJ my code works, but when I deploy it to a fat jar it doesn't find my files in my resources folder.

  • If you extract files from the resulting JAR do you see your resource files there? – Aleksei Tirman Jul 14 '22 at 13:20
  • Oh yes, the files are there. Can the problem be these 2 lines? `val path = object {}.javaClass.getResource("/pages/")?.path!! File("$path/tutorials").walk().forEach {` – Patrik Csikós Jul 14 '22 at 21:18
  • Can I use the walk function in a jar file? – Patrik Csikós Jul 14 '22 at 21:21
  • I think the problem is that the `File("$path/tutorials")` creates a reference to a file in a local filesystem, not inside a JAR file. You may find answers in https://stackoverflow.com/questions/749533/how-to-walk-through-java-class-resources useful. – Aleksei Tirman Jul 15 '22 at 04:39

1 Answers1

0

In order to have the same url that work for both Jar or in local, the url (or path) needs to be a relative path from the repository root.

..meaning, the location of your file or folder from your src folder.

could be "/main/resources/your-folder/" or "/client/notes/somefile.md"

Whatever it is, in order for your JAR file to find it, the url must be a relative path from the repository root.

it must be "src/main/resources/your-folder/" or "src/client/notes/somefile.md"

Now you get the drill, and luckily for Intellij Idea users, you can get the correct path with a right-click on the folder or file -> copy Path/Reference.. -> Path From Repository Root (this is it)

Last, paste it and do your thing.

Koch
  • 555
  • 4
  • 15