melix / jmh-gradle-plugin

Integrates the JMH benchmarking framework with Gradle
Apache License 2.0
662 stars 88 forks source link

Unable to use Lombok annotator in JMH #213

Closed oldfishdk closed 2 years ago

oldfishdk commented 2 years ago

I am trying to run jmh but turns out it does not work well with the Lombok annotator and I am not sure if any specific tweaks needs to be applied. To reproduce you could use following build.gralde:

plugins {
    id 'java-library'
    id "me.champeau.gradle.jmh" version "0.5.3"
}

sourceSets {
    main {
        java {
            srcDirs = ['src']
        }
    }
    jmh {
        java {
            srcDirs = ['src']
        }
        compileClasspath += sourceSets.main.runtimeClasspath
    }
}

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor 'org.projectlombok:lombok:1.18.20'
    implementation 'org.projectlombok:lombok:1.18.20'
}

together following source code (or any code that uses the SneakyThrows):

import lombok.SneakyThrows;

import java.io.File;
import java.io.FileWriter;

public class FileLoader {

    @SneakyThrows
    public static void load() {
        FileWriter fileWriter = new FileWriter(new File("test.log"));
    }
}

And my gralde version is 7.1.1

------------------------------------------------------------
Gradle 7.1.1
------------------------------------------------------------

Build time:   2021-07-02 12:16:43 UTC
Revision:     774525a055494e0ece39f522ac7ad17498ce032c

Kotlin:       1.4.31
Groovy:       3.0.7
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          1.8.0_302 (Azul Systems, Inc. 25.302-b08)
OS:           Linux 4.4.0-19041-Microsoft amd64

Much thanks for any suggestions or comments!

pcimcioch commented 2 years ago

Hey! You just have to add lombok annotation processor to jmh sourceset dependencies:

dependencies {
    // this adds lombok to your "main" sourceset only
    annotationProcessor 'org.projectlombok:lombok:1.18.20'
    implementation 'org.projectlombok:lombok:1.18.20' // you may want to use compileOnly instead of implementation, althougt it works as well

    // this adds lombok to "jmh" sourceset as well
    jmhCompileOnly 'org.projectlombok:lombok:1.18.20'
    jmhAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
}
oldfishdk commented 2 years ago

Much thanks for the help and confirmed it works now. And hence closing the issue.