https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
l

Logan Knight

09/22/2020, 9:43 AM
I'm new to gradle and kotlin. I am struggling to understand how things interact. I have an MP project using ktor. I have successfully been able to build and run it using docker. I am not able to get my dev environment working. I am running the server using
run
and
installDist -t
, but the js is not served. I have found that if I manually copy the js files to
${buildDir}/processedResources/jvm/main
they are served. First I don't know that this is the 'correct' place for them when using
installDist
. Second for the life of me I can't seem to figure out how to get gradle to do a simple copy of the files to that dir. Are the js files supposed to be in that dir, and if so what would be a good or the proper way to make that happen?
Copy code
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

val kotlinVersion = "1.4.0"
val serializationVersion = "1.0.0-RC"
val ktorVersion = "1.4.0"

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath("com.github.jengelman.gradle.plugins:shadow:6.0.0")
    }
}

plugins {
    kotlin("multiplatform") version "1.4.0"
    application //to run JVM part
    kotlin("plugin.serialization") version "1.4.0"
}

apply(plugin = "com.github.johnrengelman.shadow")

repositories {
    maven { setUrl("<https://dl.bintray.com/kotlin/kotlin-eap>") }
    mavenCentral()
    jcenter()
    maven("<https://kotlin.bintray.com/kotlin-js-wrappers/>") 
}

kotlin {
    jvm {
        withJava()
    }
    js {
        browser {
            binaries.executable()
        }
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
                implementation("io.ktor:ktor-client-core:$ktorVersion")
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }

        val jvmMain by getting {
            dependencies {
                implementation("io.ktor:ktor-serialization:$ktorVersion")
                implementation("io.ktor:ktor-server-core:$ktorVersion")
                implementation("io.ktor:ktor-server-netty:$ktorVersion")
                implementation("ch.qos.logback:logback-classic:1.2.3")
                implementation("io.ktor:ktor-websockets:$ktorVersion")
                implementation("mysql:mysql-connector-java:8.0.21")
                implementation("org.jetbrains.exposed:exposed-core:0.24.1")
                implementation("org.jetbrains.exposed:exposed-jdbc:0.24.1")
            }
        }

        val jsMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-js:$ktorVersion") //include http&websockets

                implementation("io.ktor:ktor-client-json-js:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization-js:$ktorVersion")

                implementation("org.jetbrains:kotlin-react:16.13.1-pre.110-kotlin-1.4.0")
                implementation("org.jetbrains:kotlin-react-dom:16.13.1-pre.110-kotlin-1.4.0")
                implementation(npm("react", "16.13.1"))
                implementation(npm("react-dom", "16.13.1"))
            }
        }
    }
}

application {
    mainClassName = "io.ktor.server.netty.EngineMain"
}

tasks.withType<Jar> {
    manifest {
        attributes(
            mapOf(
                "Main-Class" to application.mainClassName
            )
        )
    }

    archiveBaseName.set("shoppingList")

    archiveClassifier.set("")

    archiveVersion.set("")

    val webpackTask = tasks.getByName<KotlinWebpack>("jsBrowserDevelopmentWebpack")

    dependsOn(webpackTask) 

    from(File(webpackTask.destinationDirectory, webpackTask.outputFileName))
}

tasks {
    withType<KotlinCompile> {
        kotlinOptions {
            jvmTarget = "1.8"
        }
    }
}

tasks.getByName<JavaExec>("run") {
    classpath(tasks.getByName<Jar>("jar")) 
}
3 Views