我有以下项目结构:
- 父
- - client
(用 Kotlin 编写,但编译为 JS)
- - 服务器
(用 Kotlin 编写)
- - 模型
(用 Kotlin 编写)
client
模块依赖于model
。因此,当我将 client
编译为 JS 时,它也应该用它来编译 model
。现在我有以下 Gradle 配置,但没有完成所需的操作:
project/parent.gradle
group 'com.vchernogorov.tycher'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.3-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile project(":model")
compile project(":client")
compile project(":server")
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
项目/settings.gradle
rootProject.name = 'parent'
include ':model'
include ':server'
include ':client'
项目/模型/build.gradle
group 'com.vchernogorov.tycher'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.3-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
project/client/build.gradle
group 'com.vchernogorov.tycher'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.3-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
compile project(":model")
}
build.doLast {
configurations.compile.each { File file ->
copy {
includeEmptyDirs = false
from zipTree(file.absolutePath)
into "${projectDir}/web"
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
}
}
}
}
当我在 client
上运行 gradle build
命令时,我收到以下消息:
:model:compileKotlin
Using kotlin incremental compilation
:model:compileJava UP-TO-DATE
:model:copyMainKotlinClasses
:model:processResources UP-TO-DATE
:model:classes UP-TO-DATE
:model:jar
:client:compileJava UP-TO-DATE
:client:compileKotlin2Js
e: project/client/src/main/kotlin/HelloWorld.kt: (30, 19): Unresolved reference: Position
e: project/client/src/main/kotlin/HelloWorld.kt: (50, 5): Unresolved reference: Test
e: project/client/src/main/kotlin/SocketHandler.kt: (10, 36): Unresolved reference: User
:client:compileKotlin2Js FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':client:compileKotlin2Js'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 13.891 secs
这3个类在model
中定义。
那么我应该怎么做才能成功编译 client
并且不更改代码呢?
最佳答案
我为我的应用程序开发了以下架构:
我有client
, server
和model
模块,而且我还有一个带有 DTO 的单独源目录,名为 dto
(顺便说一句,不是模块)。这是构建的依赖关系层次结构:
-
client
取决于dto
-
server
取决于model
-
model
取决于dto
有了这样的层次结构模型仍然可以使用stdlib
功能和server
将与 client
共享所有 DTO模块。同时dto
模块将使用stdlib
如果编译为 module
依赖性和stdlib-js
如果编译为 client
依赖性,因此记住您可以在那里使用哪些类很重要。
为了实现这一点,您需要添加
sourceSets.main.kotlin.srcDirs += '../dto/src/main/kotlin
配置为client
和model
构建.gradle 文件。对于 Gradle Kotlin DSL:
the<JavaPluginConvention>().sourceSets {
"main" {
java {
srcDirs("../dto/src/main/kotlin")
}
}
}
关于gradle - 如何使用 Gradle 编译 Kotlin 域模块和客户端模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45237452/