如何使用Gradle与客户端模块一起编译Kotlin域模块?

时间:2017-07-21 12:15:30

标签: gradle kotlin

我有以下项目结构:
  - SELECT h.history_id, Date_format(From_unixtime(h.timestamp), '%d %m %Y') AS 'date', h.status, h.product_id, p.serial_number, p.product_name, p.site_name, p.site_postcode, Date_format(From_unixtime(i.timestamp), '%d %m %Y') AS 'last_update', i.feedback FROM history h LEFT JOIN products p ON h.product_id = p.product_id LEFT JOIN history_items i ON h.history_id = i.history_id WHERE i.timestamp = (SELECT Max(i.timestamp) FROM history_items) GROUP BY i.history_id ORDER BY h.timestamp DESC
  - parent(用Kotlin编写但编译成JS)
  - client(写在Kotlin)
  - server(写在Kotlin)
model模块依赖于client。因此,当我将model编译为JS时,它也应该使用它编译client。现在我有以下Gradle配置没有做到所需的东西:

项目/ parent.gradle

model

项目/ settings.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"
}

项目/模型/的build.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"
}

当我在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/")) } } } } 上运行gradle build命令时,我收到此消息:

client

这3个类在: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 中定义 那么,如何成功编译model而不是更改代码呢?

1 个答案:

答案 0 :(得分:1)

我为我的应用程序开发了以下架构: 我有clientservermodel模块,但我有一个单独的源目录,其中DTO名为dto(不是模块btw)。这是构建的依赖层次结构:

  • 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 build.gradle文件。对于Gradle Kotlin DSL:

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