How to compile Kotlin domain module with client module with Gradle?

I have the following project structure:
 - parent


 - - client

(written in Kotlin, but compiled for JS)
 - - server

(written in Kotlin)
 - - model

(written in Kotlin)
The module client

has a dependency on model

. So when I compile client

to JS it has to compile model

with it too . At the moment I have the following Gradle configurations which don't do what I want:

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"
}

      

Project /settings.gradle

rootProject.name = 'parent'
include ':model'
include ':server'
include ':client'

      

Project / model / 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/"))
            }
        }
    }
}

      

When I run the command gradle build

on client

, I get this message:

: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

      

These 3 classes are defined in model

.
So what should I do to compile successfully client

and not modify the code?

+3


source to share


1 answer


I have developed the following architecture for my application: I have modules client

, server

and model

, but also I have a separate source directory with DTOs called dto

(not module btw). Here's the dependency hierarchy for the build:

  • client

    depends on dto

  • server

    depends on model

  • model

    depends on dto

With such a hierarchy model, the functionality can still be used stdlib

, but server

will share all the DTOs with the module client

. Meanwhile the module dto

will be used stdlib

if it is compiled like module

and stdlib-js

if it is compiled as a dependency client

, so it is important to remember which classes you can use there.

To achieve this you need to add



sourceSets.main.kotlin.srcDirs += '../dto/src/main/kotlin

      

config for files client

and model

build.gradle. For Gradle Kotlin DSL:

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

      

+1


source







All Articles