Shared code between Kotlin servers and client projects using Gradle

I want to share some code between the server (JVM) and the client (JS) using a separate shared project. I saw the solution using Maven, but I'm not sure how to convert it to a Gradle project. In addition, there are no examples of collaborative projects in the official guide . So, what would a minimal build.gradle look like with such an installation?

+7
kotlin gradle
source share
2 answers

Building a Kotlin2JS project with dependencies on other Kotlin projects seems to require copying the sources in some way. One way to do this is to add the sources of the Kotlin project project to the source set of sources of the Kotlin2JS project.

This can be done with the following lines in the project :

 sourceSets { main.kotlin.srcDirs += project(':shared').sourceSets.main.kotlin.srcDirs } 

Please review the entire project structure:

 root/ shared/ src/main/kotlin build.gradle client/ src/main/kotlin server/ src/main/kotlin build.gradle settings.gradle 

In settings.gradle :

 include 'shared', 'server', 'client' 

And in build.gradle :

 group 'com.example.multiproject' version '1.0-SNAPSHOT' buildscript { repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.0-beta-4589" } } subprojects { repositories { mavenCentral() } } project(':client') { evaluationDependsOn(':shared') apply plugin: 'kotlin2js' compileKotlin2Js.kotlinOptions.sourceMap = true compileKotlin2Js.kotlinOptions.outputFile = "${projectDir}/web/js/app.js" compileKotlin2Js.kotlinOptions.suppressWarnings = true compileKotlin2Js.kotlinOptions.verbose = true sourceSets { main.kotlin.srcDirs += project(':shared').sourceSets.main.kotlin.srcDirs } dependencies { compile 'org.jetbrains.kotlin:kotlin-js-library:1.0.0-beta-4589' } } project(':server') { apply plugin: 'kotlin' dependencies { compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.0-beta-4589' compile project(':shared') } } 

Note that the shared project must contain its own build.gradle .

+2
source share

For completeness, this is a setting that I created almost simultaneously with the hotkey :

Main settings.gradle : include 'shared', 'client', 'server'

Home build.gradle :

 buildscript { ext { kotlinVer = '1.0.0-beta-4589' } repositories { mavenCentral() maven { url 'http://oss.sonatype.org/content/repositories/snapshots' } } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVer" } } allprojects { apply plugin: 'idea' group = 'some.company' version = '0.0.1' ext { kotlinVer = '1.0.0-beta-4589' // Lib versions go there. } repositories { mavenLocal() mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://oss.sonatype.org/content/repositories/releases/" } } } project('shared') { apply plugin: 'kotlin' } project('server') { apply plugin: 'kotlin' apply plugin: 'application' mainClassName = 'some.company.Main' dependencies { compile project(':shared') } } project('client') { apply plugin: 'kotlin2js' dependencies { compile project(':shared') } } 

build.gradle client:

 apply plugin: 'kotlin2js' compileKotlin2Js { kotlinOptions.outputFile = "server/src/main/resources/static/js/app.js" kotlinOptions.sourceMap = true } sourceSets { main.kotlin.srcDirs += '../shared/src/main/kotlin' // hotkey solution: // main.kotlin.srcDirs += project(':shared').sourceSets.main.kotlin.srcDirs } dependencies { compile "org.jetbrains.kotlin:kotlin-js-library:$kotlinVer" } 

Server build.gradle :

 apply plugin: 'kotlin' sourceCompatibility = 1.7 targetCompatibility = 1.7 dependencies { // Libs go there. compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVer" testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVer" } 

The general build.gradle project contains only apply plugin: 'kotlin' and, well, I'm not sure that using shared libraries is simple (or necessary, in many cases), so I think it can even remain empty.

gradle client:build creates a JavaScript file in the serverโ€™s static resources folder. gradle idea creates an IntelliJ project with properly related dependencies and source folders (if the default names are src/main/kotlin , etc., see the official Kotlin Gradle manual for custom source dirs).

+3
source share

All Articles