Search code examples
gradlekotlinkotlin-multiplatformkotlin-js

Depend on Kotlin Multiplatfrom JS Module from JVM


I have a Kotlin Multiplatform project with a common, a JS and a JVM module. The JVM module uses a JavaFX WebView to display a GUI. This GUI however shall be implemented as the JS module. How do I add the JS module as a dependency correctly? I tried

dependencies {
    compile project(":myproject-js")
}

however, this does not include the resulting JS files anywhere in the classpath. The JS module does indeed create a JAR file with the required dependencies, but I could not find a way to access them.

I also tried simply copying the JS files into my build output, but they are still ignored:

configurations {
    js
}

dependencies {
    js project(":myproject-js")
}

project.afterEvaluate {
    build.dependsOn copyResources
}

task copyResources(type: Copy) {
    dependsOn configurations.js

    into file("${project.buildDir}/resources")

    from {
        configurations.js.asFileTree.each {
            from (zipTree(it))
        }
        null
    }
}

Is there a way to achieve this?


Solution

  • Here's what should work:

    1. Create a configuration for the myproject-js dependency:

      configurations {
          js
      }
      
    2. Add the project dependency to that configuration:

      dependencies {
          js project(":myproject-js")
      }
      
    3. Add the configuration files to the processResources task with .from(...), and the corresponding build dependency:

      processResources {
          dependsOn configurations.js
          from(configurations.js.collect { zipTree(it) })
      }
      

    Then, whenever you build the JVM project, the JS module's files get unpacked into the resources output directory and then packed into the JAR.