如何使用 Gradle 和 Kotlin DSL 创建额外的 Kotlin SourceSet

How do I create an additional Kotlin SourceSet using Gradle with Kotlin DSL

我想创建一个测试库源集,src/tlib/kotlin "sits between" 主要和测试。我有这个,但我不确定为什么要使用 java kotlin 的源代码目录,我需要根据我的主要来源

获取它
sourceSets {
   create("tlib").java.srcDir("src/tlib/kotlin")
}

更新

Calebs-MBP:phg-entity calebcushing$ ./gradlew build
e: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
    class phg.entity.AbstractEntityBase, unresolved supertypes: org.springframework.data.domain.Persistable
> Task :compileTlibKotlin FAILED

关闭

sourceSets {
    val main by getting
    val tlib by creating {
        java {
            srcDir("src/tlib/kotlin")
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
    val test by getting {
        java {
            compileClasspath += tlib.output
            runtimeClasspath += tlib.output
        }
    }
}

configurations {
    val compile by getting
    val runtime by getting
    val tlibCompile by getting {
        extendsFrom(compile)
    }
    val tlibRuntime by getting {
        extendsFrom(runtime)
    }
    val testCompile by getting {
        extendsFrom(tlibCompile)
    }
    val testRuntime by getting {
        extendsFrom(tlibRuntime)
    }
}

dependencies {
    implementation("${project.group}:constant:[0.1,1.0)")
    api("javax.validation:validation-api")
    api("javax.persistence:javax.persistence-api")
    api("org.springframework.data:spring-data-commons") // has the missing dependency

Groovy
也有类似的问题 How do I add a new sourceset to Gradle?


sourceSets {
  val main by getting
  val test by getting
  val tlib by creating {
    java {
      srcDir("src/tlib/kotlin")
      compileClasspath += main.output + test.output
      runtimeClasspath += main.output + test.output
    }
  }
}

configurations {
  val testCompile by getting
  val testRuntime by getting
  val tlibCompile by getting {
    extendsFrom(testCompile)
  }
  val tlibRuntime by getting {
    extendsFrom(testRuntime)
  }
}

许多事情都由插件妥善处理,所以添加实际上是关于配置 sourceSet 类路径和连接配置。

这是一个简短的答案,显示了类路径配置和一个配置扩展:

sourceSets {
    val tlib by creating {
        // The kotlin plugin will by default recognise Kotlin sources in src/tlib/kotlin
        compileClasspath += sourceSets["main"].output
        runtimeClasspath += sourceSets["main"].output
    }
}

configurations {
    val tlibImplementation by getting {
        extendsFrom(configurations["implementation"])
    }
}