7

I want to create a test lib source set, src/tlib/kotlin that "sits between" main and test. I have this, but I'm not sure why I would use java source dir for kotlin, and I need to get it depending on my main sources

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

update

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

close

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
xenoterracide
  • 16,274
  • 24
  • 118
  • 243
  • This test library code shared by the test code and other modules thing has been codified by Gradle now in the java-test-fixtures plugin. – Hakanai Aug 07 '22 at 03:53

2 Answers2

11

A number of things are properly taken care of by the plugins, so the addition is really about configuring the sourceSet classpath and wiring configurations.

Here is a short answer showing the classpath config and one configuration extension:

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"])
    }
}
Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
  • 2
    Newer versions of Gradle have typesafe accessors so now you can do `sourceSets.main.get().output` and `extendsFrom(configurations.implementation.get())`. – Hakanai Aug 07 '22 at 04:07
3

There a similar question for 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)
  }
}
Eugene Petrenko
  • 4,874
  • 27
  • 36