Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JMH gradle without plugin for multi-module projects
...
subprojects {
...
apply plugin: 'java'
apply plugin: 'scala'
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceSets.main {
java.srcDirs = ['src/main/java']
scala.srcDirs = ['src/main/scala']
scala.include '**/*.*'
}
sourceSets.test {
java.srcDirs = ['src/test/java']
scala.srcDirs = ['src/test/scala']
scala.include '**/*.*'
}
// https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_source_sets
sourceSets {
jmh {
java.srcDirs = ['src/jmh/java']
scala.srcDirs = ['src/jmh/scala']
resources.srcDirs = ['src/jmh/resources']
compileClasspath += sourceSets.main.runtimeClasspath
}
}
dependencies {
...
jmhImplementation 'org.openjdk.jmh:jmh-core:1.21'
jmhImplementation 'org.openjdk.jmh:jmh-generator-annprocess:1.21'
}
// https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html
task jmh(type: JavaExec, dependsOn: jmhClasses) {
main = 'org.openjdk.jmh.Main'
classpath = sourceSets.jmh.compileClasspath + sourceSets.jmh.runtimeClasspath
// To enable the built-in stacktrace sampling profiler
// args = ['-prof', 'stack']
}
// to make sure benchmarks always get compiled
classes.finalizedBy(jmhClasses)
}
...
@CSchoel

This comment has been minimized.

Copy link

@CSchoel CSchoel commented Aug 23, 2019

Since Gradle 4.6 you have to replace

jmhImplementation 'org.openjdk.jmh:jmh-generator-annprocess:1.21'

with

jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.21'

@edumucelli

This comment has been minimized.

Copy link

@edumucelli edumucelli commented Jan 30, 2020

I have been struggling for a while with the correct build.gradle for a JMH project. Although this snippet shows interesting things I am still having a [ERROR] [system.err] Exception in thread "main" java.lang.RuntimeException: ERROR: Unable to find the resource: /META-INF/BenchmarkList.

@jlussagnet

This comment has been minimized.

@edumucelli

This comment has been minimized.

Copy link

@edumucelli edumucelli commented Feb 5, 2020

@jlussagnet thanks for the suggestion. Yes, I have tried that one too. No lucky yet. The project I am struggling with is at https://github.com/edumucelli/benchmark-xgboost-java if you are feeling generous and want to give it a try, that'd be greatly appreciated :-)

@CSchoel

This comment has been minimized.

Copy link

@CSchoel CSchoel commented Feb 9, 2020

@edumucelli Looking at your code I think the error may be that you only list dependencies for the compile task. A while ago I posted a minimal configuration for running JMH in Gradle 5 on my blog. This version uses the Gradle Kotlin DSL, but maybe it can still be of use for you. I would guess that you have to change this:

compile group: 'org.openjdk.jmh', name: 'jmh-generator-annprocess', version: '1.23'

to this

annotationProcessor group: 'org.openjdk.jmh', name: 'jmh-generator-annprocess', version: '1.23'

Maybe you also need to use implementation instead of compile.

@edumucelli

This comment has been minimized.

Copy link

@edumucelli edumucelli commented Feb 10, 2020

Thanks, @CSchoel! I have had checked your post before and I was not able to make it work as I am not very used to the the Gradle scripting, converting it from Kotlin DSL was also not straightforward for me. However, giving a second try it worked! I have then completely dropped the JMH gradle plugin! Here is the commit. Really appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.