Java和Js中Kotlin多平台项目的Gradle配置

时间:2019-09-30 18:48:29

标签: gradle kotlin multiplatform gradle-kotlin-dsl kotlin-multiplatform

似乎一切正常!但是...

我用Js和Java创建了一个Kotlin多平台项目。我致力于为所有目标创建正确的测试,并为构建创建正确的配置。似乎一切顺利,我设法创建了构建并以正确的方式进行了设置。今天,我打开了该项目,但它停止了,构建成功完成,但是没有执行测试和Java编译。

那么我如何配置它?

有了代码和结果,一切将更加清晰

build.gradle.kts

import com.moowork.gradle.node.npm.NpmTask
import com.moowork.gradle.node.task.NodeTask
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url = uri("https://plugins.gradle.org/m2/")
        }
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50")
    }
}

plugins {
    kotlin("multiplatform") version "1.3.50"
    id("com.moowork.node") version "1.3.1"
}

repositories {
    mavenCentral()
    jcenter()
}

group = "com.example"
version = "0.0.1"


kotlin {
    jvm()
    js()
    jvm {
        withJava()
    }
    js {
        nodejs()
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        jvm {
            compilations["main"].defaultSourceSet {
                dependencies {
                    implementation(kotlin("stdlib-jdk8"))
                }
            }
            compilations["test"].defaultSourceSet {
                dependencies {
                    implementation(kotlin("test"))
                    implementation(kotlin("test-junit"))
                }
            }
        }
        js {
            sequenceOf("", "Test").forEach {
                tasks.getByName<KotlinJsCompile>("compile${it}KotlinJs") {
                    kotlinOptions {
                        moduleKind = "umd"
                        noStdlib = true
                        metaInfo = true
                    }
                }
            }
            compilations["main"].defaultSourceSet {
                dependencies {
                    implementation(kotlin("stdlib-js"))
                }
            }
            compilations["test"].defaultSourceSet {
                dependencies {
                    implementation(kotlin("test-js"))
                }
            }
        }
    }
}
val compileKotlinJs = tasks.getByName("compileKotlinJs")
val compileTestKotlinJs = tasks.getByName("compileTestKotlinJs")

val libDir = "$buildDir/lib"
val compileOutput = compileKotlinJs.getOutputs().getFiles()
val testOutput = compileTestKotlinJs.getOutputs().getFiles()

val populateNodeModules = tasks.create<Copy>("populateNodeModules") {
    afterEvaluate {
        from(compileOutput)
        from(testOutput)

        configurations["testCompile"].forEach {
            if (it.exists() && !it.isDirectory) {
                from(zipTree(it.absolutePath).matching { include("*.js") })
            }
        }
        for (sourceSet in kotlin.sourceSets) {
            from(sourceSet.resources)
        }
        into("$buildDir/node_modules")
    }
    dependsOn("compileKotlinJs")
}

node {
    download = true;
}

tasks.create<NpmTask> ("installJest") {
    setArgs(setOf("install", "jest"))
}

tasks.create<NodeTask> ("runJest") {
    setDependsOn(setOf("installJest", "populateNodeModules", "compileTestKotlinJs"))
    setScript(file("node_modules/jest/bin/jest.js"))
    setArgs(compileTestKotlinJs.outputs.files.toMutableList().map {projectDir.toURI().relativize(it.toURI())})
}

tasks.getByName("test").dependsOn("runJest")

看看有多少个任务被跳过了!以及如何创建目录Dir

buid result build dir

我用玩笑来测试js。 预先感谢您对我的支持

0 个答案:

没有答案