gradle - 如何使用 Gradle 编译 Kotlin 域模块和客户端模块?

标签 gradle kotlin

我有以下项目结构:
-
- - 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 , servermodel模块,而且我还有一个带有 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

配置为clientmodel构建.gradle 文件。对于 Gradle Kotlin DSL:

the<JavaPluginConvention>().sourceSets {
    "main" {
        java {
            srcDirs("../dto/src/main/kotlin")
        }
    }
}

关于gradle - 如何使用 Gradle 编译 Kotlin 域模块和客户端模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45237452/

相关文章:

android - 在 Android Studio 中导入 scribe-1.2.1.jar

java - 是否可以在 android studio 中使用仅 java 的模块?

gradle - index.html找不到payara micro

elasticsearch - 在 Kotlin 中,如何将 Kovenant Promise 与 Elasticsearch 异步响应集成?

java - 如何在 Kotlin 中传递有界通配符类型参数?

gradle - gradle:将zip复制到mule应用

java - 错误: Gradle DSL method not found: 'compile()'

android - Room、Kotlin、Dagger 2.15 android.app.Application 不能在没有@Inject 构造函数或@Provides 注释方法的情况下提供

android - Arcore fragment 在启动时崩溃

java - onRestoreInstanceState 访问 kotlin 中的私有(private)变量