3

I'm trying to migrate groovy build script to kotlin for my jvm/js - multiplatform project, but I have the following exception

org.gradle.api.UnknownTaskException: Task with name 'jsBrowserProductionWebpack' not found in root project 'TradeProject'

build.gradle.kts

buildscript {
    repositories {
        jcenter()
    }
}

plugins {
    id("org.jetbrains.kotlin.multiplatform") version "1.3.72"
    id("org.jetbrains.kotlin.plugin.serialization") version "1.3.72"
    id("distribution")
    id("war")
}

repositories {
    jcenter()
    maven("https://dl.bintray.com/kotlin/ktor")
    mavenCentral()
}

val ktorVersion = "1.3.2"
val logBackVersion = "1.2.3"

kotlin {
    jvm {
        compilations.named("main") {
            tasks.getByName<Copy>(processResourcesTaskName) {
                dependsOn("jsBrowserProductionWebpack")
                tasks.named<org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack>("jsBrowserProductionWebpack") {
                    from(entry?.name, destinationDirectory)
                }
            }
        }
    }
    js {
        browser {
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation(kotlin("stdlib-common"))
            }
        }

        commonTest {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }

        jvm().compilations["main"].defaultSourceSet {
            dependencies {
                implementation(kotlin("stdlib-jdk8"))
                implementation("io.ktor:ktor-server-netty:$ktorVersion")
                implementation("io.ktor:ktor-server-servlet:$ktorVersion")
                implementation("io.ktor:ktor-html-builder:$ktorVersion")
                implementation("ch.qos.logback:logback-classic:$logBackVersion")
                implementation("io.ktor:ktor-client-core:$ktorVersion")
            }
        }

        jvm().compilations["test"].defaultSourceSet {
            dependencies {
                implementation(kotlin("test"))
                implementation(kotlin("test-junit"))
            }
        }

        js().compilations["main"].defaultSourceSet {
            dependencies {
                implementation(kotlin("stdlib-js"))
                implementation(kotlin("io.ktor:ktor-client-js:$ktorVersion"))
            }
        }

        js().compilations["test"].defaultSourceSet {
            dependencies {
                implementation(kotlin("test-js"))
            }
        }
    }

}

tasks.register<JavaExec>("run") {
    dependsOn("jvmJar")
    group = "application"
    main = "sample.SampleJvmKt"
    val t = tasks.named<Jar>("jvmJar")

    classpath(configurations.named("jvmRuntimeClasspath"), t.get())
}

build.gradle (works fine)

buildscript {
    repositories {
        jcenter()
    } }

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.3.72'
    id 'distribution'
    id 'war' } repositories {
    jcenter()
    maven { url "https://dl.bintray.com/kotlin/ktor" }
    mavenCentral() }



//webAppDirName = 'webapp'

def ktor_version = '1.3.2' def logback_version = '1.2.3'

kotlin {
    jvm()
    js {
        browser {
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')

            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        jvmMain {
            dependencies {
                implementation kotlin('stdlib-jdk8')
                implementation "io.ktor:ktor-server-netty:$ktor_version"
                implementation "io.ktor:ktor-server-servlet:$ktor_version"
                implementation "io.ktor:ktor-html-builder:$ktor_version"
                implementation "ch.qos.logback:logback-classic:$logback_version"
                implementation "io.ktor:ktor-client-core:$ktor_version"

            }
        }
        jvmTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        jsMain {
            dependencies {
                implementation kotlin('stdlib-js')
                implementation "io.ktor:ktor-client-js:$ktor_version"
            }
        }
        jsTest {
            dependencies {
                implementation kotlin('test-js')
            }
        }
    } }

jvmJar {
    dependsOn(jsBrowserProductionWebpack)
    from(new File(jsBrowserProductionWebpack.entry.name, jsBrowserProductionWebpack.outputPath)) }

task run(type: JavaExec, dependsOn: [jvmJar]) {
    group = "application"
    main = "sample.SampleJvmKt"
    classpath(configurations.jvmRuntimeClasspath, jvmJar)
    args = [] }

settings.gradle.kts

rootProject.name = "TradeProject"

Project structure

enter image description here

2
  • What IDE and version are you using? Commented Aug 24, 2020 at 19:11
  • 1
    @NestorLedon Intellij IDEA 2020.1 Commented Aug 26, 2020 at 5:36

1 Answer 1

1

try this

// include JS artifacts in any JAR we generate

tasks.getByName<Jar>("jvmJar") {
    val taskName = if (project.hasProperty("isProduction")) {
        "jsBrowserProductionWebpack"
    } else {
        "jsBrowserDevelopmentWebpack"
    }
    val webpackTask = tasks.getByName<KotlinWebpack>(taskName)
    dependsOn(webpackTask) // make sure JS gets compiled first
    from(File(webpackTask.destinationDirectory, webpackTask.outputFileName)) // bring output file along into the JAR
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.