Kotlin's kapt plugin for gradle does not work for custom source set (JMH)
Asked Answered
D

1

2

Having a Kotlin project with Gradle setup:

apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'

dependencies {
    kapt 'org.openjdk.jmh:jmh-generator-annprocess:1.18'
    ...
}

Putting benchmarks under src/main/kotlin works without problems.

But when i add a custom source-set for JMH:

sourceSets {
    jmh {
        compileClasspath += sourceSets.test.runtimeClasspath
        runtimeClasspath += sourceSets.test.runtimeClasspath
    }
}

And move the benchmarks from src/main/kotlin to src/jmh/kotlin, executing the benchmarks fails with:

Exception in thread "main" java.lang.RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList
    at org.openjdk.jmh.runner.AbstractResourceReader.getReaders(AbstractResourceReader.java:98)
    at org.openjdk.jmh.runner.BenchmarkList.find(BenchmarkList.java:122)
    at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:256)
    at org.openjdk.jmh.runner.Runner.run(Runner.java:206)
    at org.openjdk.jmh.Main.main(Main.java:71)

It looks like kaptJmhKotlin isn't doing anything:

kaptGenerateStubsJmhKotlin UP-TO-DATE
Skipping task ':kaptJmhKotlin' as it has no source files and no previous output files.
:kaptJmhKotlin NO-SOURCE
:compileJmhKotlin

Any idea how to resolve the problem ?

Doctor answered 13/7, 2017 at 15:58 Comment(1)
It doesn't look like a problem with jmh or gradle (i have used jmh in java-based gradle-based projects with a custom sourceset for jmh benchmarks without any problems). It is also not a problem with kotlin, as placing kotlin-based benchmarks into the default sourceset works. So, looks like a bug in kotlin-kapt and you should report it to JetBrains.Bossuet
P
7

kapt in this context defines a dependency for the main source set's kapt configuration, just like compile and runtime do.

dependencies {
  kaptJmh 'org.openjdk.jmh:jmh-generator-annprocess:1.18'
}

fixes the problem for me.

I expected it to be jmhKapt by analogy with jmhCompile, but this produces

Couldn't find method jmhCapt
Poe answered 19/12, 2017 at 9:2 Comment(1)
Thank you very much for solving this puzzle!Doctor

© 2022 - 2024 — McMap. All rights reserved.