gpt4 book ai didi

gradle - 如何使用Webpack组织Kotlin SPA的前端和后端

转载 作者:行者123 更新时间:2023-12-02 13:23:16 26 4
gpt4 key购买 nike

我正在后端与Kotlin(Ktor)合作开发SPA。现在的项目结构为:

.
├── frontend/ <-- SPA frontend source project
│ ├── dist/ <-- current webpack dist
│ ├── node_modules/ <-- not to be included in the jar
│ ├── src/ <-- to be bundled by webpack
│ │ └── index.js
│ ├── index.html <-- to be served as static by backend
│ ├── package.json <-- not to be included in the jar
│ └── webpack.config.json <-- not to be included in the jar

├── src/ <-- SPA backend source
| └── main/
| ├── kotlin/
| | └── Main.kt <-- backend entry point
| └── resources/ <-- framework-specific configuration

├── build.gradle
└── settings.gradle

前端使用 cd frontend && npm run build成功编译,将生成的包存储到 frontend/dist中。

Ktor路由设置:
fun Application.main() {
install(DefaultHeaders)
install(CallLogging)

routing {
static("/") {
default("frontend/index.html") // to be replaced with index file
// from packaged frontend
}
// ... other dynamic routes for REST
}
}

build.gradle非常标准:
buildscript {
ext.kotlin_version = '1.2.41'
ext.ktor_version = '0.9.2'
ext.koin_ktor_version = '0.9.2'
ext.ktor_gson_version = '0.9.2'

repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

group 'org.root_talis'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'

mainClassName = 'MainKt'

kotlin { experimental { coroutines "enable" } }

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

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "io.ktor:ktor-server-netty:$ktor_version"
compile "io.ktor:ktor-gson:$ktor_gson_version"
compile "ch.qos.logback:logback-classic:1.2.1"
compile "org.koin:koin-ktor:$koin_ktor_version"
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

如何让Gradle通过 npm run build编译前端项目并将 dist打包到生成的JAR中,以便后端处理?

关于组织此类项目有什么建议吗?

最佳答案

如果您需要运行一些npm命令,则可以在正在使用的计算机上安装Node,声明一个Exec任务,并使其依赖于build任务。

val npmBuilder by tasks.creating(Exec::class){
// some stuff
}

tasks.getByName("build").dependsOn(npmBuilder)

看一下 Exec任务 here的文档。

否则,您可以应用 this Node plugin for Gradle,使您不必依赖机器设置。

此外,我建议不要用手处理SPA的路线,而要使用 this Ktor Feature

关于gradle - 如何使用Webpack组织Kotlin SPA的前端和后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50260174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com