如何在Kotlin DSL上使用Gradle创建附加的Kotlin SourceSet

时间:2019-03-14 19:53:35

标签: gradle kotlin gradle-kotlin-dsl

我想创建一个测试库源集src/tlib/kotlin,它“位于之间” main和test。我有这个,但是我不确定为什么我会为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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

插件可以很好地处理很多事情,因此,附加功能实际上是关于配置sourceSet类路径和接线配置。

下面是一个简短的答案,显示了classpath配置和一个配置扩展名:

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