Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to use Lombok with Micronaut #218

Closed
4 tasks done
sescotti opened this issue May 28, 2018 · 10 comments
Closed
4 tasks done

Document how to use Lombok with Micronaut #218

sescotti opened this issue May 28, 2018 · 10 comments

Comments

@sescotti
Copy link

sescotti commented May 28, 2018

I tried to use Micronaut with Lombok and there seems to be some kind of conflict between the annotation processing and compile-time tasks that Micronaut executes to process DI and the class metadata, and the annotation processing that Lombok does.

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. Create a simple micronaut project with gradle-lombok plugin
  2. Create a Controller (or any other DI enabled class)
  3. Create a simple Dto with @Data or other annotation from Lombok
  4. Call one of the generated methods of the Dto from inside the Controller

An important point to highlight is that if I don't use annotations on the Controller, the build is successful (as it doesn't generate all the metadata classes).

Expected Behaviour

Compile successful

Actual Behaviour

Compile error: symbol not found

Environment Information

  • Operating System: Mac/bash
  • Micronaut Version: 1.0.0-SNAPSHOT
  • JDK Version: 1.8.0_121

Example Application

build.gradle

plugins {
    id 'io.franzbecker.gradle-lombok' version '1.14'
    id 'java'
}

GreetingDto.java

@Data
public class GreetingDto {
    private String greeting;
}

GreetingController.java

@Controller("/hello-world")
public class GreetingController {
    @Get
    public GreetingDto login(){
        GreetingDto dto = new GreetingDto();
        dto.setGreeting("Hello world!");
        return dto;
    }
}

Console output

> Task :compileJava FAILED
Note: Creating bean classes for 1 type elements
/Users/sebastian/development/hello-world/src/main/java/controller/GreetingController.java:17: error: cannot find symbol
        dto.setGreeting("Hello world!");
           ^
  symbol:   method setGreeting(String)
  location: variable dto of type GreetingDto
1 error
@graemerocher graemerocher self-assigned this May 28, 2018
@graemerocher
Copy link
Contributor

@graemerocher graemerocher added this to the 1.0.0-M2 milestone May 28, 2018
@graemerocher
Copy link
Contributor

I don't know what is different regarding what gradle-lombok does but removing gradle-lombok and adding the following dependencies allows me to compile:

    compileOnly 'org.projectlombok:lombok:1.16.20'
    
    annotationProcessor "org.projectlombok:lombok:1.16.20"

Does this work for you?

@sescotti
Copy link
Author

Hi Graeme, I still get the same error, in fact what the plugin does is adding the dependencies as compileOnly and annotationProcessor (among other things).
Here I created a sample project where it fails: https://github.com/sescotti/mn-hello-world-lombok. On master it has only the dependencies declared, and on using-plugin branch I replaced it with the plugin. Hope this helps!

@graemerocher
Copy link
Contributor

Seems if you define:

    compileOnly 'org.projectlombok:lombok:1.16.20'
    annotationProcessor "org.projectlombok:lombok:1.16.20"

Before

annotationProcessor "io.micronaut:inject-java"

Your example compiles. This does however seem fragile to me.

@sescotti
Copy link
Author

Yes, it works now! I tried a few combinations around the order of the dependencies but obviously didn't try this one, thanks for that. Agree, it's a good workaround by now but fragile indeed.

@graemerocher graemerocher changed the title Micronaut and Lombok compatibility issue Document how to use Lombok with Micronaut May 30, 2018
@rubensyltek
Copy link
Contributor

rubensyltek commented Jun 15, 2018

In maven, I had to add lombok processor to pom file explicitly and before micronaut processor

<annotationProcessorPaths>
  <path>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.0</version>
  </path>
  <path>
    <groupId>io.micronaut</groupId>
    <artifactId>inject-java</artifactId>
    <version>${micronaut.version}</version>
  </path>
</annotationProcessorPaths>

@andersmartini
Copy link
Contributor

I ran into these issues trying to debug using IntelliJ IDEA. I had to add lombok as a "compile" dependency for it to work. not sure exactly why it wouldn't run with lombok as "compileonly" but it didnt.
otherwise my solution is the same as above

thought I'd share in case anyone else runs into the same issue

@manavsah
Copy link

Same problem occur when you use lombok as gradle plugin:
plugins {
id "com.diffplug.eclipse.apt" version "3.22.0"
id "com.github.johnrengelman.shadow" version "6.0.0"
id "application"
id "io.freefair.lombok" version "5.1.1"
}

but resolve when you remove "io.freefair.lombok" as plugin and add in dependency as suggested above by @graemerocher .

Thanks Manav

@bossyahrul
Copy link

@rubensyltek's solution works, but it must be micronaut-inject-java instead of inject-java

@remal
Copy link

remal commented May 27, 2023

For those who are still experiencing this issue: try name.remal.lombok Gradle plugin. This plugin sorts annotation processors to prevent issues like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants