我正在尝试将groovy构建脚本迁移到我的jvm / js-多平台项目中的kotlin,但是我有以下例外情况
org.gradle.api.UnknownTaskException:在根项目'TradeProject'中找不到名称为'jsBrowserProductionWebpack'的任务
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 (工作正常)
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"
项目结构
答案 0 :(得分:0)
试试这个
// 在我们生成的任何 JAR 中包含 JS 工件
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
}